Java Learning Note-design mode 7 (adapter mode)

Source: Internet
Author: User

Intent to convert the interface of one class into another interface that the client wants.   The Adapter mode makes it possible for those classes that would otherwise not work together because of incompatible interfaces to work together. Mainly divided into three categories:The adapter mode of the class, the adapter mode of the object, the adapter mode of the interface。 1. The adapter mode of the class has a source class, has a method, is ready to be fitted, the target interface is targetable, through the adapter class, the function of the source extension to targetable.
public class Source {public void method1 () {System.out.println ("This is original method!");}}
Public interface Targetable {//and the original class method is the same as public void method1 ();//new public void Method2 ();}
public class Adapter extends Source implements targetable{@Overridepublic void Method2 () {System.out.println ("This is tar Getable method! ");}}

2. Adapter mode for objects

The basic idea is the same as the adapter pattern of the class, only the adapter class is modified, this time does not inherit the source class, but holds an instance of the source class to solve the compatibility problem.

public class Wrapper implements Targetable{private source Source;public Wrapper (source source) {super (); This.source = SOURCE;} @Overridepublic void Method1 () {source.method1 ();} @Overridepublic void Method2 () {System.out.println ("This is the targetable method!");}

3. Adapter mode for the interface

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 the other class, inherit the abstract class.

Public abstract class Wrapper2 implements targetable{@Overridepublic void Method1 () {} @Overridepublic void Method2 () {}}
public class Sourcesub extends wrapper2{@Overridepublic void Method1 () {System.out.println (' This is first method! ');}}

Transferred from: http://blog.csdn.net/zhangerqing/article/details/8239539

Java Learning Note-design mode 7 (adapter mode)

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.