外观模式-C#实现

本文介绍了外观模式在软件开发中的应用,尤其在WPF项目中,通过提供一个统一接口来降低访问复杂系统内部子系统的难度,同时阐述了其优缺点和适用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

意图:为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
主要解决:降低访问复杂系统的内部子系统时的复杂度,简化客户端之间的接口。
何时使用: 1、客户端不需要知道系统内部的复杂联系,整个系统只需提供一个"接待员"即可。 2、定义系统的入口。
如何解决:客户端不与系统耦合,外观类与系统耦合。
关键代码:在客户端和复杂系统之间再加一层,这一层将调用顺序、依赖关系等处理好。

优点: 1、减少系统相互依赖。 2、提高灵活性。 3、提高了安全性。
缺点:不符合开闭原则,如果要改东西很麻烦,继承重写都不合适。

使用场景: 1、为复杂的模块或子系统提供外界访问的模块。 2、子系统相对独立。 3、预防低水平人员带来的风险。
注意事项:在层次化结构中,可以使用外观模式定义系统中每一层的入口。

该实例基于WPF实现,直接上代码,下面为三层架构的代码。

一 Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.外观模式
{
    internal class Circle : Sharp
    {
        public string Name { get; set; }

        public void Draw()
        {
            Name = "圆"; 
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.外观模式
{
    internal class Line : Sharp
    {
        public string Name { get; set; }

        public void Draw()
        {
            Name = "线";
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.外观模式
{
    internal class Rect : Sharp
    {
        public string Name { get; set; }

        public void Draw()
        {
            Name = "矩形";
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.外观模式
{
    //绘画的接口方法
    public interface  Sharp
    {
        public string Name { get; set; }


        public void Draw();
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式练习.Model.外观模式
{
    internal class SharpLook
    {
        public string Name { get; set; }

        private Line line = new Line();
        private Circle circle = new Circle();
        private Rect rect = new Rect();

        public void DrawLine()
        {
            line.Draw();
            Name = line.Name;
        }

        public void DrawCircle()
        {
            circle.Draw();
            Name = circle.Name;
        }

        public void DrawRect()
        {
            rect.Draw();
            Name = rect.Name;
        }
    }
}

二 View

<Window x:Class="设计模式练习.View.外观模式.Look"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:设计模式练习.View.外观模式"
        mc:Ignorable="d"
        Title="Look" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Label Content="{Binding Res}" Grid.Row="0" Grid.Column="0"/>
        <Label Content="{Binding Res2}" Grid.Row="1" Grid.Column="0"/>
        <Label Content="{Binding Res3}" Grid.Row="2" Grid.Column="0"/>

        <Button Content="画线" Grid.Row="0" Grid.Column="1" Command="{Binding lineCommand}"/>
        <Button Content="画圆" Grid.Row="1" Grid.Column="1" Command="{Binding circleCommand}"/>
        <Button Content="画矩形" Grid.Row="2" Grid.Column="1" Command="{Binding rectCommand}"/>
    </Grid>
</Window>

三 ViewModel

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;
using 设计模式练习.Model.外观模式;

namespace 设计模式练习.ViewModel.外观模式
{
    partial class Look_ViewModel : ObservableObject
    {
        SharpLook sharpLook = new SharpLook();

        [ObservableProperty]
        private string res;

        [ObservableProperty]
        private string res2;

        [ObservableProperty]
        private string res3;

        [RelayCommand]
        private void line()
        {
            sharpLook.DrawLine();
            Res = sharpLook.Name;
        }

        [RelayCommand]
        private void circle()
        {
            sharpLook.DrawCircle();
            Res2 = sharpLook.Name;
        }

        [RelayCommand]
        private void rect()
        {
            sharpLook.DrawRect();
            Res3 = sharpLook.Name;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_shenbing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值