• 9 外观模式(Facade)


    外观模式是为了解决类与类之间的依赖关系的,像spring一样,可以将类和类之间的关系配置到配置文件中,而外观模式就是将他们的关系放在一个Facade类中,降低了类类之间的耦合度,该模式中没有涉及到接口,看下类图:(我们以一个计算机的启动过程为例)

     
     
     
    1. package com.liubo.patterns.facade;
      /**
       * 
       * @author liubo
       *
       */
      public class CPU {
      	public void startup() {
      		System.out.println("cpu startup");
      	}
      	public void shutdown() {
      		System.out.println("cpu shutdown");
      	}
      }
      

        


    1. package com.liubo.patterns.facade;
      /**
       * @author liubo
       *
       */
      public class Memory {
      	public void startup() {
      		System.out.println("memory startup");
      	}
      	public void shutdown() {
      		System.out.println("memory shutdown");
      	}
      }
      

        

    2. package com.liubo.patterns.facade;
      /**
       * @author liubo
       *
       */
      public class Disk {
      	public void startup() {
      		System.out.println("disk startup");
      	}
      	public void shutdown() {
      		System.out.println("disk shutdown");
      	}
      }
      

        

      /**
       * 
       */
      package com.liubo.patterns.facade;
      /**
       * @author liubo
       *
       */
      public class Computer {
      	private CPU cpu;
      	private Memory memory;
      	private Disk disk;
      	/**
      	 * @param cpu
      	 * @param memory
      	 * @param disk
      	 */
      	public Computer() {
      		this.cpu = new CPU();
      		this.memory = new Memory();
      		this.disk = new Disk();
      	}
      	public void startup() {
      		System.out.println("start the computer");
      		cpu.startup();
      		memory.startup();
      		disk.startup();
      		System.out.println("start computer finished");
      	}
      	public void shutdown() {
      		System.out.println("begin to shutdown");
      		cpu.shutdown();
      		memory.shutdown();
      		disk.shutdown();
      		System.out.println("computer closed");
      	}
      

        

      /**
       * 
       */
      package com.liubo.patterns.facade;
      /**
       * @author liubo
       *
       */
      public class Computer {
      	private CPU cpu;
      	private Memory memory;
      	private Disk disk;
      	/**
      	 * @param cpu
      	 * @param memory
      	 * @param disk
      	 */
      	public Computer() {
      		this.cpu = new CPU();
      		this.memory = new Memory();
      		this.disk = new Disk();
      	}
      	public void startup() {
      		System.out.println("start the computer");
      		cpu.startup();
      		memory.startup();
      		disk.startup();
      		System.out.println("start computer finished");
      	}
      	public void shutdown() {
      		System.out.println("begin to shutdown");
      		cpu.shutdown();
      		memory.shutdown();
      		disk.shutdown();
      		System.out.println("computer closed");
      	}
      

        


     
    //output
    start the computer
    cpu startup
    memory startup
    disk startup
    start computer finished
    begin to shutdown
    cpu shutdown
    memory shutdown
    disk shutdown
    computer closed
     
    如果我们没有Computer类,那么,CPU、Memory、Disk他们之间将会相互持有实例,产生关系,这样会造成严重的依赖,修改一个类,可能会带来其他类的修改,这不是我们想要看到的,有了Computer类,他们之间的关系被放在了Computer类里,这样就起到了解耦的作用,这,就是外观模式!





  • 相关阅读:
    扩展JSON
    字符串格式化---- String.prototype.format
    HigntChats应用举例--报表
    django Form组件之解决数据无法动态显示之BUG----以博客园添加新随笔页为主
    报错:jquery3.1.1报错Uncaught TypeError: a.indexOf is not a function
    HDU 6166 Senior Pan(多校第九场 二进制分组最短路)
    HDU 6069 Counting Divisors(区间素数筛法)
    hdu 6058 Kanade's sum (计算贡献,思维)
    HDU 6052 To my boyfriend(容斥+单调栈)
    HDU 6041 I Curse Myself(点双联通加集合合并求前K大) 2017多校第一场
  • 原文地址:https://www.cnblogs.com/liubo6/p/4563429.html
Copyright © 2020-2023  润新知