设计模式总结之适配器模式(adapter)

适配器模式定义:将一个类的接口,转化成客户期望的另一个接口,适配器让原本接口不兼容的类可以合作无间。

最常见的例子是三孔插座和两孔插座的适配:

适配器模式的对象:

1.请求对象(手机)

2.适配器对象(带有两孔和三孔的插座)

3.需要适配的对象(三孔插座)

4.请求对象所需要的接口。(插座要有两孔)

 

class Program 
 2     { 
 3         static void Main(string[] args) 
 4         { 
 5             Mobile mobile = new Mobile(); 
 6             ThreeHole threeHole = new ThreeHole(); 
 7             LineWithTwoHole lineWithTwoHole = new LineWithTwoHole(threeHole); 
 8             mobile.Charge(lineWithTwoHole); 
 9             Console.ReadKey(); 
10         } 
11     }
12 
13     /// <summary> 
14     /// 手机类 
15     /// </summary> 
16     public class Mobile 
17     { 
18         public void Charge(ITwoHole twoHole) 
19         { 
20             twoHole.Connect(); 
21             AddPower(); 
22         } 
23         public void AddPower() 
24         { 
25             Console.WriteLine("电量增加中。。。。"); 
26         } 
27     }
28 
29     /// <summary> 
30     /// 两孔插座接口 
31     /// </summary> 
32    public interface ITwoHole 
33     { 
34         void Connect(); 
35     }
36 
37     /// <summary> 
38     /// 三孔插座 
39     /// </summary> 
40    public class ThreeHole 
41     { 
42         public void Connect() 
43         { 
44             LeftConnect(); 
45             RightConnect(); 
46             ExtraConnect(); 
47         }
48 
49         public void LeftConnect() 
50         { 
51             Console.WriteLine("零线接通中。。。"); 
52         } 
53         public void RightConnect() 
54         { 
55             Console.WriteLine("火线接通中。。。。。"); 
56         } 
57         public void ExtraConnect() 
58         { 
59             Console.WriteLine("底线接通中。。。。"); 
60         } 
61     }
62 
63    public class LineWithTwoHole implements ITwoHole 
64    { 
65       private ThreeHole threeHole; 
66       public LineWithTwoHole(ThreeHole threeHole) 
67       { 
68           this.threeHole = threeHole; 
69       } 
70       public void Connect() 
71       { 
72           threeHole.LeftConnect(); 
73           threeHole.RightConnect(); 
74       } 
75    }

外观模式(facade)定义:外观模式提供了一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用。

外观模式就像照大头贴的设备操作。其是由电脑,打印机,白炽灯,相机组成,基本的操作是:打开电脑,打印机,白炽灯,相机设备,然后按一下拍照开关,接着点击打印,照片就出来了,最终关闭所有装置。

class Program 
  2   { 
  3       static void Main(string[] args) 
  4       { 
  5           
  6            ITakePicture takephoto = new TakePicture(); 
  7            takephoto.Open(); 
  8            takephoto.TakePictures(); 
  9            takephoto.Printing(); 
 10            takephoto.Close(); 
 11            Console.ReadKey(); 
 12       } 
 13   }
 14 
 15   public class Computer 
 16   { 
 17       public void Open() 
 18       { 
 19           Console.WriteLine("电脑开了"); 
 20       } 
 21       public void Close() 
 22       { 
 23           Console.WriteLine("电脑已经关闭了"); 
 24       } 
 25   }
 26 
 27   public class Light 
 28   { 
 29       public void Open() 
 30       { 
 31           Console.WriteLine("灯开了"); 
 32       } 
 33       public void Close() 
 34       { 
 35           Console.WriteLine("灯关闭了"); 
 36       } 
 37   }
 38 
 39   public class Print 
 40   { 
 41       public void Open() 
 42       { 
 43           Console.WriteLine("打印机打开了"); 
 44       } 
 45       public void Printing() 
 46       { 
 47           Console.WriteLine("打印完成"); 
 48       } 
 49       public void Close() 
 50       { 
 51           Console.WriteLine("打印机已关闭"); 
 52       } 
 53   }
 54 
 55   public class Cinema 
 56   { 
 57       public void Open() 
 58       { 
 59           Console.WriteLine("照相机打开了"); 
 60       } 
 61       public void Close() 
 62       { 
 63           Console.WriteLine("照相机已关闭"); 
 64       } 
 65       public void TakePictures() 
 66       { 
 67           Console.WriteLine("已经拍完了"); 
 68       } 
 69   }
 70 
 71   public interface ITakePicture 
 72   { 
 73        void Open(); 
 74        void TakePictures(); 
 75        void Printing(); 
 76        void Close(); 
 77   }
 78 
 79   public class TakePicture implements ITakePicture 
 80   { 
 81       Computer computer = new Computer(); 
 82       Light light = new Light(); 
 83       Print print = new Print(); 
 84       Cinema cinema = new Cinema(); 
 85       public TakePicture() 
 86       { 
 87          
 88       } 
 89       public void Open() 
 90       { 
 91           light.Open(); 
 92           computer.Open(); 
 93           print.Open(); 
 94           cinema.Open(); 
 95       }
 96 
 97       public void TakePictures() 
 98       { 
 99           cinema.TakePictures(); 
100       }
101 
102       public void Printing() 
103       { 
104           print.Printing(); 
105       }
106 
107       public void Close() 
108       { 
109           light.Close(); 
110           computer.Close(); 
111           print.Close(); 
112           cinema.Close(); 
113       } 
114   } 

 

适配器模式,主要是对适配对象进行调整,以便适合消费者的需求。从而达到消费者和被适配者解耦的目的。

外观模式的特点主要是简化接口,以及减少客户端对外观组件的耦合。因为如果客户端变化来,组件的子系统变化了,不用影响客户端。除此之外,在封装组件时,适当的在外观类中添加一些自己想要的规则。如上面例子中各设备的开关顺序,或者拍照和打印之前其设备是否开启等。

猜你喜欢

转载自408599764.iteye.com/blog/2292888