意图:为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。
主要解决:降低访问复杂系统的内部子系统时的复杂度,简化客户端之间的接口。
何时使用: 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;
}
}
}