设计模式速查版 结构型(上)

结构型模式用来处理类或者对象的组合,主要包含以下7种设计模式:
1)适配器模式(Adapter Pattern)
2)桥接模式(Bridge Pattern)
3)组合模式(Composite Pattern)
4)装饰者模式(Decorator Pattern)
5)外观模式(Facade Pattern)
6)享元模式(Flyweight Pattern)
7)代理模式(Proxy Pattern)

1.适配器模式

1)简介:

将一个类的接口转换成客户希望的另外一个接口.主要是用来解决原本的接口不兼容问题.或者用来统一接口命名.

2)结构图:

Adapter

3)代码:

View Code
 1 /// 目标类(客户端直接打交道的类) 
2 public class Target
3 {
4 /// <summary></summary>
5 public void Request()
6 {
7 }
8 }
9
10 /// 适配器类
11 public class Adapter : Target
12 {
13 /// <summary></summary>
14 private Adaptee _adaptee = new Adaptee();
15 /// <summary></summary>
16 public void Request()
17 {
18 //实际调用Adaptee的方法,这就起到适配作用
19 _adaptee.SpecificRequest();
20 }
21 }
22
23 /// 需要适配的类
24 public class Adaptee
25 {
26 /// <summary></summary>
27 public void SpecificRequest()
28 {
29 }
30 }
31
32 class Program
33 {
34 static void Main(string[] args)
35 {
36
37 Target taget = new Adapter();
38 taget.Request();
39
40 //TODO
41 Console.ReadLine();
42 }
43 }



4)适用:

通常用于对一个已经存在的类的改造上.用适配器可以不用去改动原有类或原有类难以改动.这时候可以通过适配器去间接调用原有类的方法.从而使得外部看来统一了调用接口.

2.桥接模式

1)简介:

将抽象部分与它的实现部分分离,使他们都可以独立地变化.通常是实现系统具有多个角度分类,而且每个分类都有可能会发生变化,于是将变化抽象封装起来.

具体做法是1.通过一个抽象的桥类聚合另一个接口,接口方法的具体实现延迟到由子类实现. 2.让具体操作类去继承抽象桥类,具体操作由子类控制,这样具体操作也是基于接口的,隔离其具体实现 3.客户端只依赖抽象桥类,即只依赖于两个抽象,无需理会具体实现(这样即客户端只需要和抽象"桥"打交道,通过桥去连接'两岸'的具体实现)这样封装了'两岸'的变化,方便扩展

2)结构图:

Bridge

3)代码:

   

View Code
 1 /// 实现接口 
2 public interface Implementor
3 {
4 /// <summary></summary>
5 void OperationImp();
6 }
7
8 /// 具体实现A
9 public class CImplementorA : Implementor
10 {
11 /// <summary></summary>
12 public void OperationImp()
13 {
14 Console.Write("具体实现A的方法");
15 }
16 }
17
18 /// 具体实现B
19 public class CImplementorB : Implementor
20 {
21 /// <summary></summary>
22 public void OperationImp()
23 {
24 Console.Write("具体实现B的方法");
25 }
26 }
27
28 /// 抽象桥(用来连接两个抽象,同时供客户端调用)
29 public abstract class AbstractBridge
30 {
31 /// <summary></summary>
32 protected Implementor _implementor;
33 /// <summary></summary>
34 public void SetImplementor(Implementor implementor)
35 {
36 this._implementor = implementor;
37 }
38 /// <summary></summary>
39 public virtual void Operation()
40 {
41 this._implementor.OperationImp();
42 }
43 }
44
45 /// 具体操作A
46 public class OperatorA : AbstractBridge
47 {
48 /// <summary></summary>
49 public override void Operation()
50 {
51 _implementor.OperationImp();
52 //TODO其他具体逻辑A操作
53 }
54 }
55
56 /// 具体操作B
57 public class OperatorB : AbstractBridge
58 {
59 /// <summary></summary>
60 public override void Operation()
61 {
62 _implementor.OperationImp();
63 //TODO其他具体的逻辑B操作
64 }
65 }
66
67 class Program
68 {
69 static void Main(string[] args)
70 {
71 AbstractBridge bridgeA = new OperatorA();
72
73 AbstractBridge bridgeB = new OperatorB(); //若要扩展实现另外一种具体操作,只要新添加一个Operator类即可
74
75 bridgeA.SetImplementor(new CImplementorA());
76 bridgeA.Operation();
77
78 bridgeA.SetImplementor(new CImplementorB());
79 bridgeA.Operation();
80
81 //TODO
82 Console.ReadLine();
83 }
84 }

4)适用:

适用于一个系统中几个操作对象都可能有多种实现,由隔离变化知道,将这几个对象都抽象出来.这样用户在操作时候,就只依赖于抽象.方便了扩展.

3.组合模式

1)简介:

将对象组合成树形结构以表示'部分-整体'的层次结构.组合模式可以让各对象之间以任意的方式组合起来.然后向用户提供一个统一的调用接口,用户处理对象时不用去理会处理的是单一对象(叶子)还是组合对象.

2)结构图:

Composite

3)代码:

View Code
 1 /// 抽象组件 
2 public abstract class Component
3 {
4 /// <summary>添加组件</summary>
5 public abstract void Add(Component c);
6
7 /// <summary>删除组件</summary>
8 public abstract void Remove(Component c);
9
10 /// <summary>展示组件</summary>
11 public abstract void Display(Component c);
12
13 }
14
15 /// 叶子组件
16 public class LeafComponent : Component
17 {
18 /// <summary></summary>
19 public override void Display(Component c)
20 {
21 }
22 }
23
24 /// 枝干组件
25 public class NodeComponent : Component
26 {
27 /// 聚合组件(可以是继承抽象部件的任何子组件,包括叶子组件和枝干组件.这样可以使得子组件任意组合[组合模式的意义在此])
28 private List<Component> children = new List<Component>();
29
30 /// <summary></summary>
31 public override void Add(Component c)
32 {
33 children.Add(c);
34 }
35 /// <summary></summary>
36 public override void Remove(Component c)
37 {
38 children.Remove(c);
39 }
40 /// <summary></summary>
41 public override void Display(Component c)
42 {
43 foreach (Component component in children)
44 {
45 //TODO..
46
47 }
48 }
49 }
50
51 class Program
52 {
53 static void Main(string[] args)
54 {
55 //根节点
56 Component root = new NodeComponent();
57 root.Add(new LeafComponent());
58 root.Add(new NodeComponent());
59
60
61
62 //枝干节点
63 Component nodeComp = new NodeComponent();
64 nodeComp.Add(new NodeComponent());
65
66 root.Add(nodeComp);
67
68
69
70 //TODO
71 Console.ReadLine();
72 }
73 }



4)适用:

1.当需求中体现部分与整体的层次结构关系时 2.希望对象之间可以任意组合形成一个新的组合对象,而且用户感觉不到组合对象与单个对象的差别时,即可以对外界提供统一的调用接口.

4.装饰者模式

1)简介:

动态地给一个对象添加一些额外的职责.  具体做法就是继承被装饰的对象并将其聚合到装饰对象内部,然后再调用被装饰对象的方法,接着再调用额外的方法.这样便起到装饰的作用.

2)结构图:

Decorator

3)代码:

View Code
 1 /// 抽象被装饰类 
2 public abstract class Decoratee
3 {
4 /// <summary></summary>
5 public abstract void Operation();
6 }
7
8 /// 具体被装饰类
9 public class DecorateeA : Decoratee
10 {
11 /// <summary></summary>
12 public override void Operation()
13 {
14 Console.Write("我是eeA...");
15 }
16 }
17
18 /// 抽象装饰类
19 public abstract class Decorator : Decoratee
20 {
21 /// <summary></summary>
22 protected Decoratee _decoratee;
23 /// <summary></summary>
24 public void SetDecoratee(Decoratee decoratee)
25 {
26 this._decoratee = decoratee;
27 }
28 /// <summary></summary>
29 public override void Operation()
30 {
31 _decoratee.Operation();
32
33 }
34 }
35
36 /// 具体装饰者类A
37 public class DecoratorA : Decorator
38 {
39
40 /// <summary></summary>
41 public override void Operation()
42 {
43 //调用父类操作
44 base.Operation();
45
46 //TODO其他操作
47 Console.Write("eeA,你不知道我在装饰你.嘿嘿..");
48
49 }
50 }
51
52 /// 具体装饰者类B
53 public class DecoratorB : Decorator
54 {
55
56 /// <summary></summary>
57 public override void Operation()
58 {
59 }
60 }
61
62 class Program
63 {
64 static void Main(string[] args)
65 {
66 DecorateeA ee = new DecorateeA();
67 DecoratorA or = new DecoratorA();
68
69 or.SetDecoratee(ee);
70 or.Operation();
71
72
73
74 //TODO
75 Console.ReadLine();
76 }
77 }



4)适用:

适用于想扩展原有类的功能,且不想改动原有类时.

原文地址:https://www.cnblogs.com/Quains/p/2388216.html