结构型设计模式之外观模式
3.1问题的解决方法
package test; public class Problem2 { public static void main(String[] args) { // 结构型模式之外观模式 SwitchFacade switchFacade=new SwitchFacade(); switchFacade.onDn(); switchFacade.offDn(); switchFacade.onJxq(); switchFacade.offJxq(); switchFacade.onKt(); switchFacade.offKt(); switchFacade.onTyy(); switchFacade.offTyy(); } } //电器接口 子系统 interface dq{ public void on(); public void off(); } //电脑实体类 class Dn implements dq{ @Override public void on() { // TODO Auto-generated method stub System.out.println("电脑开"); } @Override public void off() { // TODO Auto-generated method stub System.out.println("电脑关闭"); } } //集线器实体类 class Jxq implements dq{ @Override public void on() { // TODO Auto-generated method stub System.out.println("集线器开"); } @Override public void off() { // TODO Auto-generated method stub System.out.println("集线器关闭"); } } //空调实体类 class Kt implements dq{ @Override public void on() { // TODO Auto-generated method stub System.out.println("空调开"); } @Override public void off() { // TODO Auto-generated method stub System.out.println("空调关闭"); } } //投影仪实体类 class Tyy implements dq{ @Override public void on() { // TODO Auto-generated method stub System.out.println("投影仪开"); } @Override public void off() { // TODO Auto-generated method stub System.out.println("投影仪关闭"); } } //外观 总系统 class SwitchFacade{ Dn dn=null; Jxq jxq=null; Kt kt=null; Tyy tyy=null; public SwitchFacade() { dn=new Dn(); jxq=new Jxq(); kt=new Kt(); tyy=new Tyy(); } //先打开集线器,后打开电脑 public void onDn() { jxq.on(); dn.on(); } //先关闭电脑,后关闭集线器 public void offDn() { jxq.off(); dn.off(); } public void onJxq() { jxq.on(); } public void offJxq() { jxq.off(); } public void onKt() { kt.on(); } public void offKt() { kt.off(); } public void onTyy() { tyy.on(); } public void offTyy() { tyy.off(); } }