(Design mode) Adapter mode, design mode adapter Mode

Source: Internet
Author: User

(Design mode) Adapter mode, design mode adapter Mode
In the adapter mode, similar to the remote control, other different button functions (interfaces) are combined, and the remote control is used for unified encapsulation to adapt to all buttons. 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 incompatibility of interfaces to work together. An existing class, but its interface does not meet my needs, is not compatible. You want to create a reusable class that can work together with other unrelated classes or unforeseen classes (that is, classes whose interfaces may not be compatible. If you want to use existing subclasses, but it is impossible to subclass each of them to match their interfaces, the Object Adapter can adapt to its parent class interface. Existing subclass, applicable object

Public class Adaptee {public void adapteeMethod () {System. out. println ("adaptation method ");}}
Adapter Interface
Public interface Target {/*** adaptive interface */void adapteeMethod ();/*** new interface */void adapterMethod ();}
Interface implementation
Public class Adapter implements Target {private Adaptee adaptee; public Adapter (Adaptee adaptee) {this. adaptee = adaptee;} @ Override public void adapteeMethod () {this. adaptee. adapteeMethod () ;}@ Override public void adapterMethod () {System. out. println ("New Interface ");}}
Test
public static void main(String[] args) {    Target target = new Adapter(new Adaptee());    target.adapteeMethod();    target.adapterMethod();}

 

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.