Design Mode-adapter mode, design mode adapter Mode

Source: Internet
Author: User

Design Mode-adapter mode, design mode adapter Mode

In real life, we also often use adapters, which are similar to the commonly used computer adapters. You don't need to charge the adapters, but we found that the computer adapters are also qualified, first, you have to have electricity. If there is no electricity, there will be no Adapter. Second, you have to have a computer. How can you use it without a computer adapter. In this simple example, we find that we are using an adapter because I cannot charge my computer without an adapter. So some people say I disagree with the adapter mode before the project starts. Because at that time your project has not started yet. You just need to inherit the interface you specified. Why use the adapter. Below we can summarize the three features of the adapter

 Target role:This is the expected interface. That is, the plug-in required by the computer.

 Source role:The interface to be adapted. That is, the existing electricity

 Adapter role:The adapter class is the core of this mode. That is to say, we need to convert the V power for our computers.

Next, I will use a simple demo to illustrate the adapter mode and first look at a picture (not to implement the above example)

Class adapter code

1: Define the Target Interface we need

1 // <summary> 2 // target class for the client to call 3 /// </summary> 4 public interface ITarget 5 {6 // <summary> 7 // /charge 8 /// </summary> 9 /// <returns> </returns> 10 bool Charging (int isFinish ); 11}Target Interface

2: the source code of our existing Samsung and Apple chargers is as follows:

1 /// <summary> 2 /// Apple charger class 3 /// </summary> 4 public class AppleCharger {5 /// <summary> 6 // whether the process is complete charge 7 /// </summary> 8 /// <param name = "isFinish"> 0: 1: incomplete </param> 9 // <returns> </returns> 10 public int Charging (int isFinish) 11 {12 return isFinish; 13} 14} 15 16 /// <summary> 17 // Samsung charger class 18 /// </summary> 19 public class SamsungCharger {20 // <summary> 21 /// whether the charge is completed 22 /// </summary> 23 // <param name = "isFinish"> "0 ": completed "1": not completed </param> 24 // <returns> </returns> 25 public string Charging (string isFinish) 26 {27 return isFinish; 28} 29}Inherent class

3: We found that the current interface we need is bool, but the original interfaces are not uniform, but I still need to use the previous functions. At this time, we need to use the adapter to match.

1 /// <summary> 2 // Apple charger 3 /// </summary> 4 public class AppleChargerAdapter: AppleCharger, ITarget 5 {6 public new bool Charging (int isFinish) 7 {8 return base. charging (isFinish) = 0; 9} 10}Apple adapter 1 // <summary> 2 // Samsung charger 3 /// </summary> 4 public class SamsungChargerAdapter: SamsungCharger, ITarget {5 public bool Charging (int isFinish) {6 return base. charging (isFinish. toString () = "0"; 7} 8}Samsung adapter Object Adapter code 1 // <summary> 2 // Apple charger 3 /// </summary> 4 public class AppleChargerAdapter: ITarget 5 {6 private AppleCharger _ appleCharger; 7 8 public bool Charging (int isFinish) 9 {10 _ appleCharger = new AppleCharger (); 11 return _ appleCharger. charging (isFinish) = 0; 12} 13}Apple adapter 1 /// <summary> 2 // Samsung charger 3 /// </summary> 4 public class SamsungChargerAdapter: ITarget 5 {6 7 private SamsungCharger _ samsungCharger; 8 9 public bool Charging (int isFinish) 10 {11 _ samsungCharger = new SamsungCharger (); 12 return _ samsungCharger. charging (isFinish. toString () = "0"; 13} 14}Samsung Adapter

Finally, let's take a look at the client call.

1 ITarget apple = new AppleChargerAdapter (); 2 ITarget samsung = new SamsungChargerAdapter (); 3 4 if (apple. charging (0) 5 {6 Console. writeLine ("Apple adapter completed charging"); 7} 8 if (samsung. charging (0) 9 {10 Console. writeLine ("Samsung adapter completed charging"); 11}Client call

Effect:

Adapter use cases:

You have:

1: A domain-specific interface (used by clients)

2: A class needs to be used through an unmatched interface.

You want:

1: Create a reusable class to cooperate with unfinished classes (a bit like decoration Mode)

2: Change the name of a method when it is called or implemented. (delete is used to delete your interface, but delete is the class to be reused)

3: a set of methods supported for different purposes

Select your adapter

Class adapter:

1: The inheritance relationship belongs to the static definition method.

2: it is more flexible and simple to use subclasses that require adaptive classes.

Object Adapter:

1: dynamic combination

2: You can use subclass with high scalability to adapt to classes.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.