Design Mode-adapter mode, design mode Adapter

Source: Internet
Author: User

Design Mode-adapter mode, design mode Adapter

Definition of the adapter mode: converts an interface of a class to another interface that the customer expects. The adapter allows classes that are not compatible with the original interface to work together.

The adapter mode can also be called the converter mode. We can see from the definition that the adapter is actually packaging some objects so that they can open a few ports rather than themselves. Here is a simple example:

Assume that you already have a software system and want it to work with a new vendor class library, but the interfaces designed by the new vendor are different from those of the old vendor, as shown in:

  

If you do not want to change the existing code to solve this problem, you can re-write a class, which is responsible for transferring the new vendor interface to the interface you expect. This class is an adapter:

  

This adapter implements the interface that your class expects, and can communicate with the vendor's interface. This adapter works like a man-in-the-middle, it converts a customer's request into a request that can be understood by the manufacturer. This is the adapter mode.

The code example shows the mode in detail. Now there is an interface declaration for duck and Turkey:

public interface Duck {

    public void quack();

    public void fly_long();
}
public interface Turkey {
    public void gobble();

    public void fly_short();
}

Implement a specific duck and Turkey class

public class MallardDuck implements Duck {    @Override    public void quack() {        // TODO Auto-generated method stub        System.out.println("Quack!");    }    @Override    public void fly_long() {        // TODO Auto-generated method stub        System.out.println("I'm flying!");    }}
public class WildTurkey implements Turkey {    @Override    public void gobble() {        // TODO Auto-generated method stub        System.out.println("Gobble!");    }    @Override    public void fly_short() {        // TODO Auto-generated method stub        System.out.println("I'm flying a short distance");    }}

Now, if you lack a duck object and want to impersonate it with some turkey objects, it is obvious that the interface of the turkey is different and cannot be used directly. Therefore, write an adapter class to complete the conversion process:

Public class TurkeyAdapter implements Duck {Turkey turkey; public TurkeyAdapter (Turkey turkey) {this. turkey = turkey;} @ Override public void quack () {// TODO Auto-generated method stub turkey. gobble () ;}@ Override public void fly_long () {// TODO Auto-generated method stub for (int I = 0; I <5; I ++) {// turn it into a turkey capable of long-distance flight. fly_short ();}}}

Test:

Public class DuckTestDriver {public static void main (String [] args) {// TODO Auto-generated method stub MallardDuck duck = new MallardDuck (); WildTurkey turkey = new WildTurkey (); duck turkeyAdapter = new TurkeyAdapter (turkey); System. out. println ("The Turkey says... "); turkey. gobble (); turkey. fly_short (); System. out. println ("\ nThe Duck says... "); testDuck (duck); System. out. println ("\ nThe TurkeyAdapter says... "); testDuck (turkeyAdapter); // The Duck here is actually a turkey impersonate} static void testDuck (duck) {Duck. quack (); duck. fly_long ();}}

Print result:

The Turkey says...Gobble!I'm flying a short distanceThe Duck says...Quack!I'm flying!The TurkeyAdapter says...Gobble!I'm flying a short distanceI'm flying a short distanceI'm flying a short distanceI'm flying a short distanceI'm flying a short distance

In short:

(1) When you need to use an existing class and its interface does not meet your needs, use the adapter. The adapter changes the interface to meet the customer's expectations.

(2) It may take some effort or effort to implement an adapter, depends on the size and complexity of the Target Interface (why is this because the adapter does not only wrap one class, although the above example does only wrap one turkey class, the adapter can be used to adapt to many classes to provide an interface for the customer to encode ).

 

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.