创建型模式-(单例、工厂、原型、建造者)
结构型模式-(代理、适配器、桥接)
结构型模式-(装饰、外观、享元、组合)
行为型模式-(模板方法、策略、命令、责任链)
行为型模式-(状态、观察者、中介者、迭代器)
行为型模式-(访问者、备忘录、解释器)
学习笔记,仅供参考,如有错误,恳请纠正
五、装饰模式
在不改变对象原有结构的情况下,动态的给对象增加一些方法即为装饰模式
装饰模式可以比继承更为方便的扩展对象功能,并且可以创建多个装饰类为对象扩展多个不同的功能,但会为程序增加很多子类,增加程序复杂度
装饰类和具体构件都实现同一个接口,接口中带有 operate() 方法在具体构件中执行具体业务逻辑而在装饰类中调用具体构件的operate方法并调用自己的增强方法。
上图代码设计如下
抽象构件:
//抽象构件接口
interface Component {
void operate();
}
//抽象装饰类
class AbstractDecorate implements Component {
//引入具体构件
private Component component;
public AbstractDecorate(Component component) {
this.component = component;
}
@Override
public void operate() {
component.operate();
}
}
具体实现
class ConcreteDecorate extends AbstractDecorate {
public ConcreteDecorate(Component component) {
super(component);
}
//调用具体构件方法,使用addFunction增强
@Override
public void operate() {
super.operate();
addFunction();
}
private void addFunction() {
System.out.println("装饰类对具体构件增强");
}
}
Main
public static void main(String[] args) {
Component component = new ConcreteDecorate(new ConcreteComponent());
component.operate();
}
输出
六、外观模式
外观模式定义了一个高层接口,里面包含了对多个子系统的引用,客户可以通过这个接口访问各个子系统。
外观模式降低了子系统和客户端之间的耦合度,并对客户屏蔽了子系统,客户只通过高层接口访问子系统,但增删子系统会修改高层接口,违背开闭原则,可构建数据结构动态增删子系统。
子系统
interface ISubSystem {
void operate();
}
class SubSystem1 implements ISubSystem {
@Override
public void operate() {
System.out.println("子系统1执行");
}
}
class SubSystem2 implements ISubSystem {
@Override
public void operate() {
System.out.println("子系统2执行");
}
}
class SubSystem3 implements ISubSystem {
@Override
public void operate() {
System.out.println("子系统3执行");
}
}
高层接口
class Facade {
private List<ISubSystem> subSystems;
public Facade() {
subSystems = new ArrayList<>();
}
//给高层接口添加子系统
public void addSubSystem(ISubSystem system) {
subSystems.add(system);
}
//执行所有子系统
public void operate() {
for (ISubSystem system : subSystems) {
system.operate();
}
}
}
//Main
public static void main(String[] args) {
Facade facade = new Facade();
facade.addSubSystem(new SubSystem1());
facade.addSubSystem(new SubSystem2());
facade.addSubSystem(new SubSystem3());
facade.operate();
}
输出
七、享元模式
享元模式主要对对象进行复用,减少对象的创建数量,解决支持大量细粒度对象的使用。
享元模式对相同的对象只保存一份,减少了内存的开销和对象的频繁创建,提高系统响应速度
但不得不将一些不能共享的状态(非享元)外部化,增加了系统的复杂度
享元模式类似于一个基本的对象池
享元组件
//抽象享元
interface FlyWeight {
void operate(UnFlyWeight unFlyWeight);
}
//具体享元
class ConcreteFlyWeight implements FlyWeight {
private String key;
public ConcreteFlyWeight(String key) {
this.key = key;
}
@Override
public void operate(UnFlyWeight unFlyWeight) {
System.out.println("具体享元中的operate调用,其中非享元info为: " + unFlyWeight.getInfo());
}
}
//非享元
class UnFlyWeight {
private String info;
public UnFlyWeight(String info) {
this.info = info;
}
public String getInfo() { return info; }
public void setInfo(String info) { this.info = info; }
}
享元工厂
class FlyWeightFactory {
Map<String, FlyWeight> map;
public FlyWeightFactory() {
map = new HashMap<>();
}
public FlyWeight getFlyWeight(String key) {
FlyWeight res = map.get(key);
if (res != null) {
System.out.println("享元对象存在,直接返回。");
} else {
res = new ConcreteFlyWeight(key);
map.put(key, res);
System.out.println("享元对象不存在,创建后返回。");
}
return res;
}
}
Main
public static void main(String[] args) {
FlyWeightFactory factory = new FlyWeightFactory();
FlyWeight flyWeight1 = factory.getFlyWeight("1");
flyWeight1.operate(new UnFlyWeight("info1"));
FlyWeight flyWeight2 = factory.getFlyWeight("1");
flyWeight2.operate(new UnFlyWeight("info2"));
}
输出
八、组合模式
组合模式是一种将对象组合成树状层次结构的模式,使得用户对单一对象和整体对象的访问具有一致性
优点:
- 可以一致性地处理单一对象和组合对象,无需关系处理的是单一对象还是整体对象
- 容易在组合体中插入新的对象,无需修改代码,符合开闭原则
但组合模式结构设计复杂且不容易通过继承来增加构件新功能
组合模式的结构类似一棵树,分为枝节点和叶节点,枝节点可以连接枝节点,也可以连接叶节点。
枝节点和叶节点都实现一个抽象接口,实现接口方法operate
枝节点维护一个包含所有子节点的List,且在operate方法中调用所有子节点的operate
叶节点中operate方法处理实际的业务逻辑
代码设计:
//所有节点的抽象接口
interface TreeComponent {
void operate();
boolean addChild(TreeComponent t);
}
class Leaf implements TreeComponent {
private String tag;
public Leaf(String tag) {
this.tag = tag;
}
//叶节点处理业务逻辑
@Override
public void operate() {
System.out.println(tag + "调用");
}
@Override
public boolean addChild(TreeComponent t) { return false; }
}
class Trunk implements TreeComponent {
private String tag;
private List<TreeComponent> child;
public Trunk(String tag) {
this.tag = tag;
child = new ArrayList<>();
}
@Override
public void operate() {
//调用所有子节点的operate
System.out.println(tag + "调用");
for (TreeComponent c : child) {
c.operate();
}
}
@Override
public boolean addChild(TreeComponent t) {
if (t == this) return false;
return child.add(t);
}
}
Main
public static void main(String[] args) {
TreeComponent trunk1 = new Trunk("枝节点1");
TreeComponent trunk2 = new Trunk("枝节点2");
TreeComponent leaf1 = new Leaf("叶节点1");
TreeComponent leaf2 = new Leaf("叶节点2");
TreeComponent leaf3 = new Leaf("叶节点3");
TreeComponent leaf4 = new Leaf("叶节点4");
trunk1.addChild(leaf1);
trunk1.addChild(leaf2);
trunk1.addChild(trunk2);
trunk2.addChild(leaf3);
trunk2.addChild(leaf4);
trunk1.operate();
}
上图结构
输出
如有错误,恳请纠正
下接: 23种设计模式简述-行为型模式-(模板方法、策略、命令、责任链)