夜光序言:
时间没有等我,是你忘了带我走,我左手过目不忘的的萤火,右手里是十年一个漫长的打坐。
正文:
以道御术 / 以术识道
我们可以结合 HashMap看组合模式
1、抽象构件:总的抽象类或者接口,定义一些通用的方法,比如新增、删除。
2、中间构件:继承或者实现抽象构件,定义存储方式,并针对特殊需要重写抽象构件的方法。
3、叶子节点:继承或者实现抽象构件,并针对特殊需要重写抽象构件的方法。
package 组合模式.JDK源码分析;
import java.util.HashMap;
import java.util.Map;
public class Composite {
public static void main(String[] args) {
Map<Integer,String> hashMap = new HashMap<Integer,String>();
hashMap.put(0,"战神");
Map<Integer,String> map = new HashMap<Integer,String>();
map.put(1,"海贼王");
map.put(2,"火影忍者");
hashMap.putAll(map);
System.out.println(hashMap);
}
}
下面讲解一下
外观模式:
定义
提供了一个统一的接口,用来访问子系统中一群接口
适用场景
- 子系统复杂,增加外观模式提供简单调用接口
- 构建多层系统结构,用外观对象作为每层入口
package 外观模式.影院管理;
public class DVDPlayer {
//这里,我们使用单例模式,使用饿汉式
private static DVDPlayer instance = new DVDPlayer();
/**
* @Description: 夜光
* @Param: []
* @return: 外观模式.影院管理.DVDPlayer
* @Author: Hy
* @Date: 2019/12/9
*/
public static DVDPlayer getInstance(){
//饿汉式
return instance;
}
public void on(){
System.out.println(" dvd on ");
}
public void off(){
System.out.println(" dvd off ");
}
public void play(){
System.out.println(" dvd is playing ");
}
}
package 外观模式.影院管理;
//影院里面的灯光
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 ");
}
}
package 外观模式.影院管理;
//爆米花
public class PopCorn {
//首先引入一个饿汉式
private static PopCorn instance = new PopCorn();
public static PopCorn getInstance(){
//饿汉式
return instance;
}
public void on(){
System.out.println(" pop on ");
}
public void off(){
System.out.println(" pop off ");
}
public void pop(){
System.out.println(" pop is poping ");
}
}
package 外观模式.影院管理;
//立体声类
public class Stereo {
//首先引入一个饿汉式
private static Stereo instance = new Stereo();
public static Stereo getInstance(){
//饿汉式
return instance;
}
public void on(){
System.out.println(" Stereo on ");
}
public void off(){
System.out.println(" screen off ");
}
//立体声调大
public void up(){
System.out.println(" voice up ");
}
}
package 外观模式.影院管理;
//投影仪
public class Projector {
//首先引入一个饿汉式
private static Projector instance = new Projector();
public static Projector getInstance(){
//饿汉式
return instance;
}
public void up(){
System.out.println(" screen up ");
}
public void down(){
System.out.println(" screen down ");
}
public void focus(){
System.out.println(" Projector is focusing ");
}
}
package 外观模式.影院管理;
public class DVDPlayer {
//这里,我们使用单例模式,使用饿汉式
private static DVDPlayer instance = new DVDPlayer();
/**
* @Description: 夜光
* @Param: []
* @return: 外观模式.影院管理.DVDPlayer
* @Author: Hy
* @Date: 2019/12/9
*/
public static DVDPlayer getInstance(){
//饿汉式
return instance;
}
public void on(){
System.out.println(" dvd on ");
}
public void off(){
System.out.println(" dvd off ");
}
public void play(){
System.out.println(" dvd is playing ");
}
//..........
public void pause(){
System.out.println(" dvd pause ");
}
}
package 外观模式.影院管理;
/*
*
* @title HomeTheaterFacade
* @describe 夜光:这里我们写一个外观类
* @author hy
* @version
* @date 2019/12/9
*
*/
public class HomeTheaterFacade {
//首先我们定义各个子系统的对象
private TheaterLight theaterLight;
private Projector projector;
private Stereo stereo;
private PopCorn popCorn;
private DVDPlayer dvdPlayer;
//构造器
public HomeTheaterFacade() {
super();
this.theaterLight = TheaterLight.getInstance();
this.projector = Projector.getInstance();
this.stereo = Stereo.getInstance();
this.popCorn = PopCorn.getInstance();
this.dvdPlayer = DVDPlayer.getInstance();
}
//操作分成四步
public void ready(){
popCorn.on();
popCorn.pop();
stereo.on();
dvdPlayer.on();
theaterLight.dim();
}
public void play(){
dvdPlayer.play();
}
public void end(){
popCorn.off();
theaterLight.bright();
stereo.off();
dvdPlayer.off();
}
public static void main(String[] args) {
//统一调用
//这里就不写了,放到client里面
}
}