设计模式:结构型——桥接模式、组合模式、享元模式

→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;
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值