Design Mode Mode Adapter

Source: Internet
Author: User

Adapter Pattern Definition: The interface of a class is transformed into a client's expectation of an interface. Adapters allow classes that are incompatible with the original interface to work together.

Simple definition: encapsulates an object. and provide different interfaces .

Simple examples:

Duck Interface:

Package Headfirst.adapter.ducks;public interface Duck {public void quack ();p ublic void Fly ();

Detailed Ducks:

Package Headfirst.adapter.ducks;public class Mallardduck implements Duck {public void quack () {System.out.println (" Quack ");} public void Fly () {System.out.println ("I ' M Flying");}}

Turkey interface:

Package Headfirst.adapter.ducks;public interface Turkey {public void gobble ();p ublic void Fly ();

Detailed Turkey:

Package Headfirst.adapter.ducks;public class Wildturkey implements Turkey {public void gobble () {System.out.println (" Gobble gobble ");} public void Fly () {System.out.println ("I ' m flying a short distance");}}

To fit a turkey into a duck:

Package Headfirst.adapter.ducks;public class Turkeyadapter implements Duck {Turkey Turkey, public turkeyadapter (Turkey Turkey) {This.turkey = turkey;}    public void Quack () {turkey.gobble ();}  public void Fly () {for (int i=0; i < 5; i++) {turkey.fly ();}}}
Test:
Package Headfirst.adapter.ducks;public class Ducktestdrive {public static void main (string[] args) {Mallardduck duck = new Mallardduck (); Wildturkey Turkey = new Wildturkey ();D uck turkeyadapter = new Turkeyadapter (Turkey);   System.out.println ("The Turkey says ..."); turkey.gobble (); Turkey.fly (); System.out.println ("\nthe Duck says ..."); Testduck (Duck);  System.out.println ("\nthe turkeyadapter says ..."); Testduck (Turkeyadapter);} static void Testduck (Duck Duck) {duck.quack ();d uck.fly ();}}
Output:
The Turkey says ... Gobble Gobblei ' m flying a short distancethe Duck says ... Quacki ' m flyingthe turkeyadapter says ... Gobble Gobblei ' 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


Copyright notice: This article Bo Master original article. Blog, not reproduced without consent.

Design Mode Mode Adapter

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.