设计模式--外观模式(Facade Pattern)

1、概念理解

    外观模式:提供了一个统一的接口,用来访问子系统中的一群接口。外观模式定义了一个高层接口,让子系统更容易使用。

    软件设计原则:要减少对象之间的交互,只留下几个“密友”。这个原则被成为“最少知识(Least Knowledge)原则”,它告诉我们只和自己的密友谈话。

    *外观模式在简化接口的同时,依然将系统完整的功能暴露出来,一共需要的人使用。

    *外观模式不仅简化了接口,也将客户从组件的子系统中解耦。

    *适配器(Adapter)模式和外观(Facade)模式都可以包装多个类,前者的目的是将接口重新组织后提供新接口,后者的目的是简化接口。由于它们的差异很细微,所以两者常常同时出现。

 

2、例子computer facade

 

电源power

public class Power {
	public void connect() {
		System.out.println("The power is connected.");  
	}  
		  
	public void disconnect() {  
		 System.out.println("The power is disconnected.");  
	}  
}

 主板MainBoard

public class MainBoard {
	public void on() {  
        System.out.println("The mainboard is on.");  
    }  
  
    public void off() {  
        System.out.println("The mainboard is off.");  
    }
}

 硬盘HardDisk

public class HardDisk {
	public void run() {  
        System.out.println("The harddisk is running.");  
    }  
  
    public void stop() {  
        System.out.println("The harddisk is stopped.");  
    }
}

 操作系统OperationSystem

public class OperationSystem {
	public void startup() {  
        System.out.println("The opertion system is startup.");  
    }  
  
    public void shutdown() {  
        System.out.println("The operation system is shutdown.");  
    }
}

 定义computer facade 来管理computer

public class Computer {
	private Power power;

	private MainBoard board;

	private HardDisk disk;

	private OperationSystem system;

	public Computer(Power power, MainBoard board, HardDisk disk, OperationSystem system) {
		this.power = power;
		this.board = board;
		this.disk = disk;
		this.system = system;
	}

	public void startup() {
		this.power.connect();
		this.board.on();
		this.disk.run();
		this.system.startup();
	}

	public void shutdown() {
		this.system.shutdown();
		this.disk.stop();
		this.board.off();
		this.power.disconnect();
	}
}

测试类TestComputer

public class TestComputer {
	public static void main(String[] args) {
		Power power = new Power();
		MainBoard board = new MainBoard();
		HardDisk disk = new HardDisk();
		OperationSystem system = new OperationSystem();

		Computer computer = new Computer(power, board, disk, system);
		computer.startup();
		computer.shutdown();
	}
}

 运行结果:

The power is connected.
The mainboard is on.
The harddisk is running.
The opertion system is startup.
The operation system is shutdown.
The harddisk is stopped.
The mainboard is off.
The power is disconnected.

 

3、内容总结

外观模式的优点:
(1)、对接口进行了简化,方便客户使用。
(2)、可以实现客户与子系统解耦,易于维护。
缺点:
(1)、多了一个包装类,可能导致复杂度和开发时间的增加,并降低运行时的性能。
值得注意的地方:
(1)、外观模式对接口进行了简化,但这并不意味着对子系统进行彻底封装。如果有必要,这些子系统的接口还可以继续暴露给客户,这就是所谓的高级功能(或称为自定义)。
(2)、外观模式不能新增功能,但他可以将某些功能按次序执行。例如先打开DVD,后播放DVD。
(3)、子系统与外观不是一对一关系,是多对多关系。一个子系统可以拥有多个外观,一个外观可以调用多个子系统。
(4)、外观模式与适配器模式的区别不是包装的类的多少,而是意图不一样。适配器模式也可以包装很多类,但他的意图是改变接口,符合客户的期望;外观模式是将接口进行简化,方便使用。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值