Design mode: Manually write an adapter and appearance mode yourself

Source: Internet
Author: User

Adapter mode: the interface of a class is transformed into the interface that the client expects, and the adapter makes the incompatible classes work together. There are two kinds of forms: Class adapters and Object adapters. The former requires multiple inheritance (Java does not support), which uses a combination.

appearance mode: provides a unified and simplified interface for accessing a group of interfaces in a subsystem. The appearance defines a high-level interface that makes the subsystem easier to use.


Class diagram of the adapter



A simple source for the adapter:

Package adapter;/** * Duck interface * @author Arvon * */public interface Duck {public void quack ();p ublic void Fly ();

Package adapter;/** * Turkey interface * @author Administrator * */public interface Turkey {public void Fly ();p ublic void Goggle ();}

Package adapter;/** * A duck disguised as a turkey  * @author Administrator * */public class Turkeyadapter implements Duck {private Turkey Turkey;public Turkeyadapter (Turkey Turkey) {super (); this.turkey = Turkey;} @Overridepublic void Quack () {//TODO auto-generated method Stubturkey.goggle ();} @Overridepublic void Fly () {//TODO auto-generated method stubfor (int i=0;i<5;i++) turkey.fly ();}}


Package Adapter;public class Malladduck implements Duck {@Overridepublic void Quack () {//TODO auto-generated method stubs Ystem.out.println ("I am quacking ..."); @Overridepublic void Fly () {//TODO auto-generated method StubSystem.out.println ("I am flying ...");}


Package Adapter;public class Wildturkey implements Turkey {@Overridepublic void Fly () {//TODO auto-generated method stubs Ystem.out.println ("I am flying short distance ...");} @Overridepublic void Goggle () {//TODO auto-generated method StubSystem.out.println ("I am goggling ....");}


Package Adapter;public class Adaptertest {public static void main (string[] args) {//TODO auto-generated method Stubduck D UCK = new Malladduck (); System.out.println (Duck.getclass (). Getsimplename () + "says ..."); Testduck (duck);//Convert Duck interface to Turkey interface Duck Fakeduck = new Turkeyadapter (New Wildturkey ()); System.out.println (Fakeduck.getclass (). Getsimplename () + "says ..."); Testduck (Fakeduck);} private static void Testduck (Duck Duck) {duck.quack ();d uck.fly ();}}

Program output:

Malladduck says ...
I am quacking ...
I am flying ...
Turkeyadapter says ...
I am goggling ....
I am flying short distance ...
I am flying short distance ...
I am flying short distance ...
I am flying short distance ...
I am flying short distance ...


Appearance Pattern class diagram



Core code:

Package facade;/** * Simplified interface * @author Administrator * */public class Hometheaterfacade {amplifier amp; Tuner Tuner;dvdplayer DVD; CDPlayer cd; Projector projector; Theaterlights lights; Screen screen; Popcornpopper popper;public hometheaterfacade (amplifier amp, Tuner Tuner, Dvdplayer DVD, CDPlayer CD, projector Projector , theaterlights lights, screen screen, Popcornpopper Popper) {super (); this.amp = Amp;this.tuner = Tuner;this.dvd = Dvd;this . cd = Cd;this.projector = projector;this.lights = Lights;this.screen = Screen;this.popper = Popper;} public void Watchmovie (String movie) {System.out.println ("Get ready to watch a movie ...");p Opper.on ();p opper.pop (); Lights.dim, Screen.down ();p rojector.on ();p rojector.widescreenmode (); Amp.on (); Amp.setdvd (DVD);  Amp.setsurroundsound (); Amp.setvolume (5);d Vd.on ();d vd.play (movie); public void Endmovie () {System.out.println ("shutting movie theater down ...");p Opper.off (); Lights.on (); Screen.up (); Projector.off (); Amp.off ();d vd.stop ();d vd.eject ();d Vd.off ();}


Package Facade;public class Facadetest {public static void main (string[] args) {//TODO auto-generated method Stubamplifie R amp = new Amplifier ("top-o-line amplifier"); Tuner Tuner = new Tuner ("Top-o-line AM/fm Tuner", AMP);D vdplayer DVD = new Dvdplayer ("Top-o-line DVD Player", AMP); CDPlayer cd = new CDPlayer ("Top-o-line cd Player", AMP); Projector projector = new Projector ("Top-o-line projector", DVD); Theaterlights lights = new Theaterlights ("Theater Ceiling Lights"); Screen screen = new screens ("Theater screen"); Popcornpopper popper = new Popcornpopper ("Popcorn popper"); Hometheaterfacade hometheater = new Hometheaterfacade (AMP, tuner, DVD, CD, Projector, lights, screen, Popper); Hometheater.watchmovie ("Raiders of the Lost Ark"); Hometheater.endmovie ();}}


The other classes are subsystems of the class that are not posted here.

Output of the program:

Get ready to watch a movie ...
Popcorn Popper on
Popcorn Popper Popping popcorn!
Theater Ceiling Lights dimming to 10%
Theater screen going down
Top-o-line Projector on
Top-o-line Projector in widescreen mode (16x9 aspect ratio)
Top-o-line amplifier on
Top-o-line Amplifier setting DVD player to top-o-line DVD player
Top-o-line amplifier surround sound on (5 speakers, 1 subwoofer)
Top-o-line Amplifier Setting Volume to 5
Top-o-line DVD Player on
Top-o-line DVD Player playing "Raiders of the Lost Ark"
Shutting movie theater down ...
Popcorn Popper Off
Theater Ceiling Lights on
Theater screen going up
Top-o-line Projector Off
Top-o-line amplifier off
Top-o-line DVD Player stopped "Raiders of the Lost Ark"
Top-o-line DVD Player Eject
Top-o-line DVD Player off








Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Design mode: Manually write an adapter and appearance mode yourself

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.