设计模式之外观模式代码实现

外观模式通过定义一个一致的接口,用以屏蔽内部子系统的细节,使得调用端只需和这个接口发生调用,而无需关心这个子系统的内部细节

代码实现:

public class DVDPlayer {
//使用单例模式
//使用饿汉式
private static DVDPlayer instance = new DVDPlayer();
public static DVDPlayer getInstance() {
   return instance;
 
}
public void on() {
   System.out.println("dvd打开了");
}
public void off() {
   System.out.println("dvd close 了");
}
public void play() {
   System.out.println("dvd is palying");
}
 
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打开了");
}
public void off() {
   System.out.println("Popcorn close 了");
}
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打开了");
}
public void off() {
   System.out.println("Projector close 了");
}
public void focus() {
   System.out.println("Projector is focusing");
}
}
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 Steroe {
private static Steroe instance = new Steroe();
 
public static Steroe getInstance() {
   return instance;
}
public void on() {
   System.out.println("Steroe on");
}
public void off() {
   System.out.println("Steroe off ");
}
public void up() {
   System.out.println("Steroe 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 HomeThreaterFacade {
//定义各个子系统对象
private TheaterLight threaterLight;
private Popcorn popcorn;
private Steroe steroe;
private Projector projector;
private Screen screen;
private DVDPlayer dvdplayer;
//构造器
public HomeThreaterFacade() {
   super();
   this.threaterLight = TheaterLight.getInstance();
   this.popcorn = Popcorn.getInstance();
   this.steroe = Steroe.getInstance();
   this.projector = Projector.getInstance();
   this.screen = Screen.getInstance();
   this.dvdplayer = DVDPlayer.getInstance();
}
public void ready() {
   popcorn.on();
   popcorn.pop();
   screen.down();
   projector.on();
   steroe.on();
   dvdplayer.on();
}
public void play() {
   dvdplayer.play();
}
 
public void pause() {
   dvdplayer.pause();;
}
 
public void end() {
   popcorn.off();;
   threaterLight.bright();
   screen.up();
   projector.off();
   steroe.off();
   dvdplayer.off();
}
 
}
//调用方
public class Clients {
public static void main(String[] args) {
   //直接调用,很麻烦
   HomeThreaterFacade h = new HomeThreaterFacade();
   h.ready();
   System.out.println("--------------");
   h.play();
   System.out.println("--------------");
   h.pause();
   System.out.println("--------------");
   h.end();
}
}

模式优缺点

优点

• 对客户屏蔽子系统组件,减少了客户处理的对象数目并使得子系统使用起来更加容易。通过引入外观模式,客户代码将变得很简单,与之关联的对象也很少。

• 实现了子系统与客户之间的松耦合关系,这使得子系统的组件变化不会影响到调用它的客户类,只需要调整外观类即可。

• 降低了大型软件系统中的编译依赖性,并简化了系统在不同平台之间的移植过程,因为编译一个子系统一般不需要编译所有其他的子系统。一个子系统的修改对其他子系统没有任何影响,而且子系统内部变化也不会影响到外观对象。

• 只是提供了一个访问子系统的统一入口,并不影响用户直接使用子系统类。

缺点

• 不能很好地限制客户使用子系统类,如果对客户访问子系统类做太多的限制则减少了可变性和灵活性。

• 在不引入抽象外观类的情况下,增加新的子系统可能需要修改外观类或客户端的源代码,违背了“开闭原则”。

模式适用环境

在以下情况下可以使用外观模式:

• 当要为一个复杂子系统提供一个简单接口时可以使用外观模式。该接口可以满足大多数用户的需求,而且用户也可以越过外观类直接访问子系统。

• 客户程序与多个子系统之间存在很大的依赖性。引入外观类将子系统与客户以及其他子系统解耦,可以提高子系统的独立性和可移植性。

• 在层次化结构中,可以使用外观模式定义系统中每一层的入口,层与层之间不直接产生联系,而通过外观类建立联系,降低层之间的耦合度。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chuxuezhe_987

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值