→23种设计模式大纲
设计模式之结构型
1)桥接模式
桥接模式:Bridge Pattern
- 将抽象部分和实现部分分离,使得二者可以独立变化。
- 可以通过扩展使得实现部分被选择、切换、调整。
- Bridge持有对象实现部分的引用,通过扩展Bridge调整实现部分,而不必修改实现部分的代码
public abstract class Bridge {
protected SourceAble sourceAble;
public abstract void operate();
}
class ConcreteBridge extends Bridge{
@Override
public void operate() {
//other operation
getSourceAble().operate();
}
}
interface SourceAble {
void operate();
}
class SourceSubA implements SourceAble {
@Override
public void operate() {
System.out.println("服务A 操作...");
}
}
class SourceSubB implements SourceAble {
@Override
public void operate() {
System.out.println("服务B 操作...");
}
}
class Test{
public static void main(String[] args) {
Bridge bridge=new ConcreteBridge();
bridge.setSourceAble(new SourceSubA());
bridge.operate();
bridge.setSourceAble(new SourceSubB());
bridge.operate();
}
}
-
代码实现中,Bridge持有的Source接口仅有一个operate方法,实际上可以有多个方法。
-
通过扩展Bridge,可以调整Source接口的方法顺序和添加额外的逻辑,不用修改Source实现类。
2)组合模式
组合模式:Composite Pattern
- 将对象组合成树形结构,统一叶子对象和组合对象(叶节点和非叶节点)
public abstract class Component {
void addChild(Component child) {
throw new RuntimeException("不支持的方法");
}
void removeChild(Component child) {
throw new RuntimeException("不支持的方法");
}
List<Component> getChild() {
throw new RuntimeException("不支持的方法");
}
public abstract void operate();
}
@AllArgsConstructor
class Leaf extends Component {
private String name;
@Override
public void operate() {
System.out.println("调用Leaf operate 方法");
}
}
@ToString(exclude = "name")
class Composite extends Component {
@Override
public void addChild(Component child) {
children.add(child);
}
@Override
public void removeChild(Component child) {
children.remove(child);
}
@Override
public List<Component> getChild() {
return children;
}
@Override
public void operate() {
System.out.println("调用 Composite operate 方法");
}
public Composite(String name) {
this.name = name;
}
private List<Component> children = new ArrayList<>();
private String name;
}
class Test {
public static void main(String[] args) {
Component component = new Composite("composite root");
Composite levelSecondComposite = new Composite("composite level 1");
levelSecondComposite.addChild(new Leaf("leaf level 2:A"));
levelSecondComposite.addChild(new Leaf("leaf level 2:B"));
component.addChild(levelSecondComposite);
component.addChild(new Leaf("leaf level 1:A"));
component.addChild(new Leaf("leaf level 1:B"));
System.out.println("#####################");
List<Component> child = component.getChild();
child.forEach(System.out::println);
System.out.println("#####################");
child.forEach(Component::operate);
System.out.println("#####################");
}
}
例如菜单表,有些菜单可能有子菜单,有些菜单不能包含子菜单。
3)享元模式
享元模式:Flyweight Pattern
享元模式是各种池技术的基本理念。通过构造一个包容对象(集合、数组等)实现对象的复用。
public interface FlyWeight {
void operation();
}
@AllArgsConstructor
class ConcreteFlyWeight implements FlyWeight {
private String name;
@Override
public void operation() {
System.out.println(name + "操作...");
}
}
//享元工厂
class FlyWeightFactory {
//保存对象的容器
private static Map<String, ConcreteFlyWeight> map = new HashMap<>();
public static ConcreteFlyWeight getFlyWeight(String name) {
if (map.get(name) != null) {
return map.get(name);
} else {
ConcreteFlyWeight concreteFlyWeight = new ConcreteFlyWeight(name);
map.put(name, concreteFlyWeight);
return concreteFlyWeight;
}
}