Design pattern summary adapter pattern (adapter)

Adapter pattern definition: Convert the interface of a class into another interface expected by the client, and the adapter allows classes with incompatible interfaces to work together seamlessly.

The most common example is the adaptation of a three-hole socket and a two-hole socket:

Adapter pattern objects:

1. Request object (mobile phone)

2. Adapter objects (sockets with two and three holes)

3. Objects to be adapted (three-hole socket)

4. The interface required by the request object. (The socket should have two holes)

 

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 /// Mobile phone
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("The battery is increasing...");
26         }
27     }
28
29     /// <summary>
30 /// Two-hole socket interface
31     /// </summary>
32    public interface ITwoHole
33     {
34         void Connect();
35     }
36
37     /// <summary>
38 /// Three-hole socket
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("The zero line is connected...");
52         }
53         public void RightConnect()
54         {
55 Console.WriteLine("The live wire is connected...");
56         }
57         public void ExtraConnect()
58         {
59 Console.WriteLine("The bottom line is connected...");
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 definition: Facade provides a unified interface for accessing a group of interfaces in a subsystem. Facades define a high-level interface that makes the subsystem easier to use.

Appearance mode operates like a photo-taking device. It is composed of a computer, a printer, an incandescent lamp, and a camera. The basic operations are: turn on the computer, printer, incandescent lamp, and camera equipment, then click the photo switch, then click print, the photo will come out, and finally turn off all devices.

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("The computer is on");
 20       }
 21       public void Close()
 22       {
 23 Console.WriteLine("The computer has been shut down");
 24       }
 25   }
 26
 27   public class Light
 28   {
 29       public void Open()
 30       {
 31 Console.WriteLine("The light is on");
 32       }
 33       public void Close()
 34       {
 35 Console.WriteLine("The light is off");
 36       }
 37   }
 38
 39   public class Print
 40   {
 41       public void Open()
 42       {
 43 Console.WriteLine("The printer is turned on");
 44       }
 45       public void Printing()
 46       {
 47 Console.WriteLine("Print completed");
 48       }
 49       public void Close()
 50       {
 51 Console.WriteLine("Printer is off");
 52       }
 53   }
 54
 55   public class Cinema
 56   {
 57       public void Open()
 58       {
 59 Console.WriteLine("Camera is turned on");
 60       }
 61       public void Close()
 62       {
 63 Console.WriteLine("Camera is off");
 64       }
 65       public void TakePictures()
 66       {
 67 Console.WriteLine("Finished");
 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   }

 

 

The adapter mode is mainly to adjust the adaptation object to suit the needs of consumers. So as to achieve the purpose of decoupling consumers and adaptees.

The features of the Facade pattern are mainly to simplify the interface and reduce the coupling of the client to the Facade component. Because if the client changes, the subsystem of the component changes without affecting the client. In addition, when encapsulating components, appropriately add some rules you want to the appearance class. Such as the switching sequence of each device in the above example, or whether the device is turned on before taking pictures and printing.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327034200&siteId=291194637