标签:构造 pre 统一 家庭 mamicode turn 打开 简单 vat
public class DVDPlayer { //使用单例模式, 使用饿汉式 private static DVDPlayer instance = new DVDPlayer(); public static DVDPlayer getInstanc() { return instance; } public void on() { System.out.println(" dvd on "); } public void off() { System.out.println(" dvd off "); } public void play() { System.out.println(" dvd is playing "); } public void pause() { System.out.println(" dvd pause .."); } }
爆米花机:
public class Popcorn { private static Popcorn instance = new Popcorn(); public static Popcorn getInstance() { return instance; } public void on() { System.out.println(" popcorn on "); } public void off() { System.out.println(" popcorn ff "); } public void pop() { System.out.println(" popcorn is poping "); } }
投影仪:
public class Projector { private static Projector instance = new Projector(); public static Projector getInstance() { return instance; } public void on() { System.out.println(" Projector on "); } public void off() { System.out.println(" Projector ff "); } public void focus() { System.out.println(" Projector is Projector "); } }
屏幕:
public class Screen { private static Screen instance = new Screen(); public static Screen getInstance() { return instance; } public void up() { System.out.println(" Screen up "); } public void down() { System.out.println(" Screen down "); } }
立体声:
public class Stereo { private static Stereo instance = new Stereo(); public static Stereo getInstance() { return instance; } public void on() { System.out.println(" Stereo on "); } public void off() { System.out.println(" Screen off "); } public void up() { System.out.println(" Screen up.. "); } }
灯光:
public class TheaterLight { private static TheaterLight instance = new TheaterLight(); public static TheaterLight getInstance() { return instance; } public void on() { System.out.println(" TheaterLight on "); } public void off() { System.out.println(" TheaterLight off "); } public void dim() { System.out.println(" TheaterLight dim.. "); } public void bright() { System.out.println(" TheaterLight bright.. "); } }
组合成一个家庭影院:
public class HomeTheaterFacade { //定义各个子系统对象 private TheaterLight theaterLight; private Popcorn popcorn; private Stereo stereo; private Projector projector; private Screen screen; private DVDPlayer dVDPlayer;
//构造器 public HomeTheaterFacade() { super(); this.theaterLight = TheaterLight.getInstance(); this.popcorn = Popcorn.getInstance(); this.stereo = Stereo.getInstance(); this.projector = Projector.getInstance(); this.screen = Screen.getInstance(); this.dVDPlayer = DVDPlayer.getInstanc(); } //操作分成 4 步 public void ready() { popcorn.on(); // 打开爆米花机 popcorn.pop(); // 开始制作爆米花 screen.down(); // 屏幕放下来 projector.on(); // 打开投影仪 stereo.on(); // 打开立体声 dVDPlayer.on(); // 打开DVD theaterLight.dim(); // 灯光调暗 } public void play() { // 播放 dVDPlayer.play(); } public void pause() { // 暂停 dVDPlayer.pause(); } public void end() { popcorn.off(); theaterLight.bright(); screen.up(); projector.off(); stereo.off(); dVDPlayer.off(); } }
测试:
public class Client { public static void main(String[] args) { // TODO Auto-generated method stub //这里直接调用。。 很麻烦 HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade(); homeTheaterFacade.ready(); homeTheaterFacade.play(); homeTheaterFacade.end(); } }
这个类里有ReflectorFactory、ObjectFactory、ObjectWapperFactory 这三个工厂作为属性:
下面有一个newMetaObject 一个方法,它把那三个工厂放进来了:
进入这个forObject方法:它根据object 判断,如果为空,就是空,否则就new 一个MetaObject
看这个MetaObject的构造方法:初始化时候传入三个工厂,它根据object的不同,就给不同的返回
原理类图:
标签:构造 pre 统一 家庭 mamicode turn 打开 简单 vat
原文地址:https://www.cnblogs.com/abiu/p/14155785.html