ToB企服应用市场:ToB评测及商务社交产业平台

标题: 外观模式 [打印本页]

作者: 锦通    时间: 2023-9-27 14:34
标题: 外观模式
外观模式

案例引入

要求,组建一个家庭影院,DVD播放器,投影仪,自动屏幕,环绕立体声,爆米花机,实现家庭影院的功能。过程为,直接用遥控器,统筹各设备开关,开爆米花机,放下屏幕,开投影仪,开音响,开dvd,选dvd,取爆米花,调暗灯光,播放电影,观影结束后,关闭各设备。
传统方式实现案例


无具体代码
传统方式实现案例分析

基本介绍

外观模式原理类图


类图说明

外观模式实现案例

代码
  1. /**
  2. * @author 长名06
  3. * @version 1.0
  4. * 界面类
  5. */
  6. public class HomeTheaterFacade {
  7.     private DVDPlayer dvdPlayer = DVDPlayer.getDvdPlayer();
  8.     public Popcorn popcorn = Popcorn.getPopcorn();
  9.     public Projector projector = Projector.getProjector();
  10.     public Stereo stereo = Stereo.getStereo();
  11.     public Screen screen = Screen.getScreen();
  12.     public TheaterLight theaterLight = TheaterLight.getTheaterLight();
  13.     public void ready(){
  14.         popcorn.on();
  15.         popcorn.pop();
  16.         dvdPlayer.on();
  17.         projector.on();
  18.         screen.down();
  19.         stereo.on();
  20.         theaterLight.dim();
  21.     }
  22.     public void play(){
  23.         dvdPlayer.play();
  24.     }
  25.     public void end(){
  26.         popcorn.off();
  27.         dvdPlayer.off();
  28.         projector.off();
  29.         screen.up();
  30.         stereo.off();
  31.         theaterLight.off();
  32.     }
  33.     public void pause(){
  34.         dvdPlayer.pause();
  35.         stereo.setVolume();
  36.     }
  37. }
  38. /**
  39. * @author 长名06
  40. * @version 1.0
  41. * 爆米花机
  42. */
  43. public class Popcorn {
  44.     private static Popcorn popcorn = new Popcorn();
  45.     public static Popcorn getPopcorn(){
  46.         return popcorn;
  47.     }
  48.     private Popcorn(){
  49.     }
  50.     public void on(){
  51.         System.out.println("爆米花机 打开");
  52.     }
  53.     public void off(){
  54.         System.out.println("爆米花机 关闭");
  55.     }
  56.     public void pop(){
  57.         System.out.println("爆米花机 爆米花中");
  58.     }
  59. }
  60. /**
  61. * @author 长名06
  62. * @version 1.0
  63. * 投影仪
  64. */
  65. public class Projector {
  66.     private static Projector projector = new Projector();
  67.     public static Projector getProjector(){
  68.         return projector;
  69.     }
  70.     private Projector() {
  71.     }
  72.     public void on(){
  73.         System.out.println("投影仪 打开");
  74.     }
  75.     public void off(){
  76.         System.out.println("投影仪 关闭");
  77.     }
  78.     public void focus(){
  79.         System.out.println("投影仪 聚焦");
  80.     }
  81. }
  82. /**
  83. * @author 长名06
  84. * @version 1.0
  85. * 投影屏幕
  86. */
  87. public class Screen {
  88.     private static Screen screen = new Screen();
  89.     public static Screen getScreen(){
  90.         return screen;
  91.     }
  92.     private Screen() {
  93.     }
  94.     public void down(){
  95.         System.out.println("放下 投影屏幕");
  96.     }
  97.     public void up(){
  98.         System.out.println("升起 投影屏幕");
  99.     }
  100. }
  101. /**
  102. * @author 长名06
  103. * @version 1.0
  104. * 立体声
  105. */
  106. public class Stereo {
  107.     private static Stereo stereo = new Stereo();
  108.     public static Stereo getStereo() {
  109.         return stereo;
  110.     }
  111.     private Stereo() {
  112.     }
  113.     public void on() {
  114.         System.out.println("立体声 打开");
  115.     }
  116.     public void off() {
  117.         System.out.println("立体声 关闭");
  118.     }
  119.     public void setVolume(){
  120.         System.out.println("立体声 设置音量");
  121.     }
  122. }
  123. /**
  124. * @author 长名06
  125. * @version 1.0
  126. * 灯光
  127. */
  128. public class TheaterLight {
  129.     private static TheaterLight theaterLight = new TheaterLight();
  130.     public static TheaterLight getTheaterLight(){
  131.         return theaterLight;
  132.     }
  133.     private TheaterLight() {
  134.     }
  135.     public void on(){
  136.         System.out.println("打开灯光");
  137.     }
  138.     public void off(){
  139.         System.out.println("关闭灯光");
  140.     }
  141.     public void dim(){
  142.         on();//先打开再调暗
  143.         System.out.println("调暗灯光");
  144.     }
  145.     public void bright(){
  146.         System.out.println("调量灯光");
  147.     }
  148. }
  149. /**
  150. * @author 长名06
  151. * @version 1.0
  152. * dvd播放器
  153. */
  154. public class DVDPlayer {
  155.     private static DVDPlayer dvdPlayer = new DVDPlayer();
  156.     public static DVDPlayer getDvdPlayer(){
  157.         return dvdPlayer;
  158.     }
  159.     private DVDPlayer(){
  160.     }
  161.     public void on(){
  162.         System.out.println(" dvd播放器 打开");
  163.     }
  164.     public void off(){
  165.         System.out.println(" dvd播放器 关闭");
  166.     }
  167.     public void pause(){
  168.         System.out.println(" dvd播放器 暂停");
  169.     }
  170.     public void play(){
  171.         System.out.println(" dvd播放器 播放");
  172.     }
  173. }
  174. //调用者
  175. public class Client {
  176.     public static void main(String[] args) {
  177.         HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade();
  178.         homeTheaterFacade.ready();
  179.         System.out.println("--------");
  180.         homeTheaterFacade.play();
  181.         System.out.println("--------");
  182.         homeTheaterFacade.pause();
  183.         System.out.println("--------");
  184.         homeTheaterFacade.end();
  185.     }
  186. }
复制代码
外观模式在Mybatis框架的源码分析
  1.   //org.apache.ibatis.session.Configuration类的方法
  2.   public MetaObject newMetaObject(Object object) {
  3.     return MetaObject.forObject(object, objectFactory, objectWrapperFactory, reflectorFactory);
  4.   }
  5. //MetaObject的forObject方法
  6. public static MetaObject forObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
  7.     if (object == null) {
  8.       return SystemMetaObject.NULL_META_OBJECT;
  9.     } else {
  10.       return new MetaObject(object, objectFactory, objectWrapperFactory, reflectorFactory);
  11.     }
  12.   }
  13. //会调用到MetaObject类中的该方法
  14. private MetaObject(Object object, ObjectFactory objectFactory, ObjectWrapperFactory objectWrapperFactory, ReflectorFactory reflectorFactory) {
  15.     this.originalObject = object;
  16.     this.objectFactory = objectFactory;
  17.     this.objectWrapperFactory = objectWrapperFactory;
  18.     this.reflectorFactory = reflectorFactory;
  19.     if (object instanceof ObjectWrapper) {
  20.       this.objectWrapper = (ObjectWrapper) object;
  21.     } else if (objectWrapperFactory.hasWrapperFor(object)) {
  22.       this.objectWrapper = objectWrapperFactory.getWrapperFor(this, object);
  23.     } else if (object instanceof Map) {
  24.       this.objectWrapper = new MapWrapper(this, (Map) object);
  25.     } else if (object instanceof Collection) {
  26.       this.objectWrapper = new CollectionWrapper(this, (Collection) object);
  27.     } else {
  28.       this.objectWrapper = new BeanWrapper(this, object);
  29.     }
  30.   }
复制代码
外观模式的注意事项和细节

只是为了记录自己的学习历程,且本人水平有限,不对之处,请指正。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4