Various adapters for the design pattern

Source: Internet
Author: User

7 Structural modes: Adapter mode, decoration mode, proxy mode, appearance mode, bridging mode, combined mode, and enjoy meta mode. Where the object's adapter pattern is the origin of various patterns, let's look at the following figure:


The adapter pattern transforms the interface of a class into another interface that the client expects, in order to eliminate compatibility issues with classes caused by mismatched interfaces. There are three main categories: The adapter mode of the class, the adapter mode of the object, and the adapter mode of the interface.

Class Adapter Mode:


Code:

public class Source {public        void Method1 () {          System.out.println ("This is original method!");      }  }  Public interface Targetable {/        * is the same as the method in the original class *      /public void method1 ();        /* Method for new class *      /public void method2 ();  }  public class Adapter extends Source implements targetable {        @Override public      void Method2 () {          System.out.println ("This is the targetable method!");      }  The adapter class inherits the source class, implements the Targetable interface, and the following is the test class: public class Adaptertest {public        static void Main (string[] args) {          targetable target = new Adapter ();          Target.method1 ();          TARGET.METHOD2 ();      }  }  Thus, the implementation class of the Targetable interface has the function of the source class.


Object Adapter Mode:


public class Wrapper implements targetable {        private source source;            Public Wrapper (source source) {          super ();          This.source = source;      }      @Override public      void Method2 () {          System.out.println ("This is the targetable method!");        @Override public      void Method1 () {          source.method1 ();      }  }  Test class:


Interface Adapter Mode:

Scenario: Sometimes we write an interface with multiple abstract methods, when we write the implementation class of the interfaces, we must implement all the methods of the interface, which is obviously sometimes wasteful, because not all methods are what we need, sometimes only need some, here in order to solve this problem, We introduced an interface adapter pattern, with an abstract class that implements the interface, implements all the methods, and we do not deal with the original interface, only with the abstract class, so we write a class, inherit the abstract class, rewrite the method we need. Look at the class diagram:


Code:

Public interface sourceable {public void method1 ();  public void method2 (); } Abstract class Wrapper2:public abstract class Wrapper2 implements sourceable{public void Method1 () {} public void M Ethod2 () {}} public class SourceSub1 extends Wrapper2 {public void method1 () {System.out.println ("the"   Urceable interface ' s first sub1! ");}} public class SourceSub2 extends Wrapper2 {public void Method2 () {System.out.println ("the sourceable Interfa   Ce ' s second sub2! ");}}           public class Wrappertest {public static void main (string[] args) {sourceable source1 = new SourceSub1 ();                    sourceable source2 = new SourceSub2 ();          Source1.method1 ();          SOURCE1.METHOD2 ();          Source2.method1 ();      SOURCE2.METHOD2 ();          }} public class Adaptertest {public static void main (string[] args) {Source Source = new Source (); targetable target = new Wrapper (SOURCE);          Target.method1 ();      TARGET.METHOD2 ();   }  }

Class's adapter pattern: When you want to convert a class to a class that satisfies another new interface , you can use the class's adapter pattern, create a new class, inherit the original class, and implement the new interface.

object's adapter pattern: When you want to convert an object to an object that satisfies another new interface, you can create a wrapper class that holds an instance of the original class, and in the method of the wrapper class, the method that invokes the instance is the line.

Interface Adapter Mode: When you do not want to implement all the methods in an interface, you can create an abstract class wrapper, implement all the methods, we write other classes, when we inherit the abstract class.











Various adapters for the design pattern

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.