package H_Appearance;
/**
* 外观模式:提供一个统一的接口,用来访问子系统中的一群接口。外观定义了一个高层接口,让子系统更容易使用
* 简化接口时,依然将系统完整的功能暴露出来,供给需要的人用。将客户从组件的子系统中解耦
* 如果变得复杂,有太多的朋友牵涉其中,可以增加更多的外观,将子系统分成几个层次
* 设计原则:最少知识原则,只和你的密友谈话(减少对象之间交互,只留下几个”密友”)
* 要求:在该对象的方法内,只应该调用属于如下范围的方法
* 如果某对象是调用其他方法返回结果,不要调用该对象的方法
* 1:该对象本身
* 2:被当做方法的参数所传递的对象
* 3:此方法所创建或实例化的任何对象
* 4:对象的任何组件(被实例引用的任何对象,想象成has-a关系)
* 缺点:导致更多包装类被制造出来,已处理和其他组件的沟通
*
*
*/
public class Main {
public static void main(String[] args) {
Amplifier amp = new Amplifier();
Tuner tuner = new Tuner();
DvdPlayer dvd = new DvdPlayer();
CdPlayer cd = new CdPlayer();
Projector projector = new Projector();
TheaterLights lights = new TheaterLights();
Screen screen = new Screen();
PopcornPopper popper = new PopcornPopper();
HomeTheaterFacade homeTheaterFacade = new HomeTheaterFacade(amp, tuner,
dvd, cd, projector, lights, screen, popper);
homeTheaterFacade.watchMovie("看电影");
homeTheaterFacade.endMovie();
}
}
package H_Appearance;
import java.util.ArrayList;
/**
* 遵守最少知识原则,案例
*/
public class Car {
Engine engine;
public void start(Key key) {
Doors doors = new Doors();
boolean authorized = key.turns();// 被当做参数传递进来的对象,其方法可以被调用
if (authorized) {
engine.start();// 调用组件方法
updateDashboardDisplay();// 调用同一个对象内的本地方法
doors.lock();// 调用所创建或实例化的对象的方法
}
// 这样不行,没有遵守最小知识原则,因为在此调用的方法属于另一次调用的返回对象
// key.getObject().size();
// 但是可以再key里面添加一个显示size()结果的方法,这样可以减少所依赖的类的数目
int size = key.getSize();
}
private void updateDashboardDisplay() {
}
}
class Engine {
public void start() {
}
}
class Doors {
public void lock() {
}
}
class Key {
public boolean turns() {
return false;
}
public ArrayList getObject() {
return new ArrayList();
}
public int getSize() {
return new ArrayList().size();
}
}
package H_Appearance;
class Amplifier {
public void on() {
// TODO Auto-generated method stub
}
public void setDvd(DvdPlayer dvd) {
// TODO Auto-generated method stub
}
public void setSurroundSound() {
// TODO Auto-generated method stub
}
public void setVolume(int i) {
// TODO Auto-generated method stub
}
public void off() {
// TODO Auto-generated method stub
}
}
class Tuner {
}
class DvdPlayer {
public void play(String movie) {
// TODO Auto-generated method stub
}
public void on() {
// TODO Auto-generated method stub
}
public void sto() {
// TODO Auto-generated method stub
}
public void eject() {
// TODO Auto-generated method stub
}
public void off() {
// TODO Auto-generated method stub
}
}
class CdPlayer {
}
class Projector {
public void on() {
// TODO Auto-generated method stub
}
public void wideScreenMode() {
// TODO Auto-generated method stub
}
public void off() {
// TODO Auto-generated method stub
}
}
class TheaterLights {
public void dim(int i) {
// TODO Auto-generated method stub
}
public void on(int i) {
// TODO Auto-generated method stub
}
}
class Screen {
public void down() {
// TODO Auto-generated method stub
}
public void up() {
// TODO Auto-generated method stub
}
}
class PopcornPopper {
public void on() {
// TODO Auto-generated method stub
}
public void pop() {
// TODO Auto-generated method stub
}
public void off() {
// TODO Auto-generated method stub
}
}
public class HomeTheaterFacade {
// 组合
Amplifier amp;
Tuner tuner;
DvdPlayer dvd;
CdPlayer cd;
Projector projector;
TheaterLights lights;
Screen screen;
PopcornPopper popper;
public HomeTheaterFacade(Amplifier amp, Tuner tuner, DvdPlayer dvd,
CdPlayer cd, Projector projector, TheaterLights lights,
Screen screen, PopcornPopper popper) {
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) {
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() {
popper.off();
lights.on(10);
screen.up();
projector.off();
amp.off();
dvd.sto();
dvd.eject();
dvd.off();
}
}