Design Mode: manually write an adapter and appearance mode.

Source: Internet
Author: User

Design Mode: manually write an adapter and appearance mode.

Adapter mode: converts an interface of a class to the interface that the customer expects. The adapter allows the incompatible classes to work together. There are two forms: Class adapter and Object Adapter. The former requires multiple inheritance (not supported by java), and the latter requires a combination.

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

 

Adapter class diagram

 

A simple source code for the adapter:

 

Package adapter;/*** Duck interface ** @ author Arvon **/public interface Duck {public void quack (); public void fly ();}

Package adapter;/*** Turkey interface ** @ author Administrator **/public interface Turkey {public void fly (); public 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 stubSystem.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 stubSystem.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 duck = new MalladDuck (); System. out. println (duck. getClass (). getSimpleName () + says ...); testDuck (duck); // converts the Duck interface to the 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 (); duck. 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; audio 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 ...); popper. on (); popper. pop (); lights. dim (10); screen. down (); projector. on (); projector. wideScreenMode (); amp. on (); amp. setDvd (dvd); amp. setSurroundSound (); amp. setVolume (5); dvd. on (); dvd. play (movie);} public void endMovie () {System. out. println (Shutting movie theater down ...); popper. off (); lights. on (); screen. up (); projector. off (); amp. off (); dvd. stop (); dvd. eject (); dvd. off ();}}


 

 

package facade;public class FacadeTest {public static void main(String[] args) {// TODO Auto-generated method stubAmplifier amp = new Amplifier(Top-O-Line Amplifier);Tuner tuner = new Tuner(Top-O-Line AM/FM Tuner, amp);DvdPlayer 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 Screen(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();}}


 

Other classes are sub-system classes.

Program output:

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

 




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.