Design Mode note (7)-adapter mode (Structural)

Source: Internet
Author: User
Document directory
  • Gof Definition
  • Motivation
  • Key Points of adapter Mode
Gof Definition

Converts an interface of a class to another interface that the customer wants. The adapter mode allows the classes that cannot work together due to interface incompatibility to work together.

Motivation

In software systems, due to changes in the application environment, we often need to put "some existing objects" in the central environment for application, however, the interfaces required by the new environment are not met by these existing objects.

Adaptation means converting incompatible interfaces into compatible interfaces without changing the original implementation. There are many examples of adapters in our life, such as laptop power adapters and some card USB card readers.

In the gof23 book, The adapter mode is divided into two types: Object Adapter and class Adapter

Object Adapter

Target is the class or interface that the customer expects to use. Adaptee is an adaptive object. Adapter is an adaptive object. First, restore the structure to code, as shown below:

/// <Summary> // class to be used by the customer // </Summary> public abstract class target {public abstract void request ();} /// <summary> /// adapted class /// </Summary> public class adaptee {public void specificrequest () {}}/// <summary> /// adaption object // </Summary> public class adapter: Target {private adaptee adapee = new adaptee (); public void request () {adapee. specificrequest () ;}/// <summary> // client program /// </Summary> public class app {static void main (string [] ARGs) {Target target = new adapter (); // for the customer, the request is called, and the specificrequest target is actually called. request ();}}

Next, let's look at a practical example based on the above ideas. We want to implement a stack operation, there is an istact interface, which contains three methods to push (into the stack) pop (out-of-stack) and gettopitem (taking the top-level element), this istact interface will be equivalent to the above target, and you want to implement the stack-out operations, if you need to implement the data structure by yourself, you can use the arraylist class provided by net here. The arraylist class is the adapted object, which is equivalent to the adaptee above. Write an adaptation class stactadapter class to complete the function.

/// <Summary> /// stack interface /// </Summary> Public interface istack {void push (Object item); void POP (); object gettopitem ();} // <summary> // Object Adapter // </Summary> public class stactadapter: istack {arraylist list; /// <summary> /// instantiate arraylist In the constructor // </Summary> Public stactadapter () {list = new arraylist ();} /// <summary> /// stack entry // </Summary> /// <Param name = "item"> elements pushed into the stack </param> Public void push (Object item) {list. add (item) ;}/// <summary> // output stack /// </Summary> Public void POP () {list. removeat (list. count-1 );} /// <summary> /// obtain the top-level element /// </Summary> /// <returns> </returns> Public object gettopitem () {return list [list. count-1] ;}}
/// <Summary> /// call by the customer /// </Summary> public class app {static void main (string [] ARGs) {istack mystack = new stactadapter (); mystack. push ("oec2003"); mystack. push ("oec2004"); mystack. push ("oec2005"); mystack. pop (); console. writeline (mystack. gettopitem ());}}

The adapter mode is also very simple to understand. The push and pop methods in istact are the functions of adding and removing elements. The system class arraylist provides the ADD and removeat methods, so we can use it for borrow. Through the conversion of the adapter class (stactadapter), the push and pop methods can support the function of adding and removing elements, just like the notebook we often use, the voltage in reality is not consistent with the voltage required by the notebook. The voltage in reality is converted to the voltage required by the notebook through the conversion of the power adapter.

Class Adapter

The class adapter uses implicit inheritance. It can be seen that the adapter inherits the target and adaptee, but the C # language does not support multi-inheritance. If you must use the class adapter in C, one of the target and adapter must be an interface, which has certain limitations. Furthermore, after the adapter inherits the target and adapter, it has these two responsibilities, which violates the single responsibility principle. Therefore, we usually use the Object Adapter. The following code is used together, target is designed as an interface.

/// <Summary> /// class to be used by the customer /// </Summary> Public interface itarget {public void request ();} /// <summary> /// adapted class /// </Summary> public class adaptee {public void specificrequest () {}}/// <summary> /// adaption object // </Summary> public class adapter: adaptee, itarget {public void request () {This. specificrequest () ;}/// <summary> // client program /// </Summary> public class app {static void main (string [] ARGs) {itarget target = new adapter (); // The request called by the customer is actually the specificrequest target called. request ();}}
Key Points of adapter Mode

The adapter mode is mainly used in English to "reuse some existing classes, but the interfaces are inconsistent with the requirements of the Reuse Environment". It is very useful in the reuse of legacy code and the migration of class libraries.

Gof23 defines the implementation structure of two adapter modes: Object Adapter and class adapter. However, class adapters use the "Multi-inheritance" implementation method, resulting in poor high coupling, so it is generally not recommended. The Object Adapter adopts the "Object combination" method, which is more loosely coupled.

The adapter mode can be very flexible and does not have to stick to the two structures defined in gof23. For example, the "existing object" in the adapter mode can be used as a new interface method parameter to achieve adaptation.

The adapter mode requires us to use the "interface-oriented programming" style as much as possible to facilitate later adaptation.

Return to the beginning (INDEX)

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.