Adapter mode (Adapter pattern)

Source: Internet
Author: User

Adapter Mode: Converts a class's interface into another interface that the customer wants, so that classes that are inherently incompatible with each other and cannot work together can work together.

There is a duck interface:

 Public Interface Duck {    publicvoid  quack ();      Public void fly ();}

The green-headed Duck is a subclass of a duck:

 Public class Implements Duck {    publicvoid  quack () {        System.out.println ("quack");     }             publicvoid  Fly () {        System.out.println ("I ' m Flying");}    }

There is a turkey interface, will not be a quack, will only cluck, unlike Ducks, no quack (), but Gobble ():

 Public Interface Turkey {    publicvoid  gobble ();      Public void fly ();}

A wild turkey that implements the Turkey interface:

 Public class Implements Turkey {    publicvoid  gobble () {        System.out.println ("Gobble Gobble") ;    }      Public void Fly () {        System.out.println ("I ' m flying a short distance");}     }

How to impersonate a duck with a turkey and the customer does not know?

We write an adapter that implements the Duck interface, and it actually uses turkeys.

 Public class Implements Duck {    Turkey Turkey;      Public Turkeyadapter (Turkey Turkey) {        this. Turkey = Turkey;    }       Public void Quack () {        turkey.gobble ();    }      Public void () {        turkey.fly ();    }}

The adapter passes in a turkey instance, and when the quack method is called, the Gobble method is actually called.

 Public ducktest {       publicstaticvoid  main (string[] args) {            new  Mallarduck ();                         New Wildturkey ();             New Turkeyadapter (Turkey);            Duck.quack ();            Duck.fly ();                        Turkeyadapter.quack ();               Turkeyadapter.fly ();       }}

Through the adapter, the Turkey successfully disguised as a duck, for the customer called the Duck's quack and fly method, the Gobble method is not visible to the customer.

This adapter is called an object adapter.

The duck interface equates to target,turkeyadapter equivalent to Adapter,turkey equivalent to Adaptee.

If we do not have duck interface, only Mallardduck class, then adapter inherit Mallardduck class, others are the same, this becomes the class adapter.

If our Duck interface contains the Fly method is not turkey, we do not want to rewrite this method, then write an abstract adapter,turkey inherit this adapter, then only need to rewrite the Quck method, which is the interface adapter.

Adapter mode (Adapter 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.