Java桥接模式(思维导图)

图1 桥接模式【点击查看图片】

    学习设计模式发现,很多设计模式都是很像但是侧重点不同。这些设计模式有很多实现流程相同,但是目的却不同。

1,抽象化角色&修正抽象化角色

package com.cnblogs.mufasa.demo1;

public abstract class Abstraction {
    protected Implementor impl;
    public Abstraction(Implementor impl){
        this.impl = impl;
    }
    //示例方法
    public void operation(){
        impl.operationImpl();
    }
}

class RefinedAbstraction extends Abstraction {
    public RefinedAbstraction(Implementor impl) {
        super(impl);
    }
    //其他的操作方法
    public void otherOperation(){

    }
}

2,实现化角色&具体实现化角色

package com.cnblogs.mufasa.demo1;

public abstract class Implementor {
    /**
     * 示例方法,实现抽象部分需要的某些具体功能
     */
    public void operationImpl(){
        System.out.println("实体操作");
    }
}

class ConcreteImplementorA extends Implementor {
    @Override
    public void operationImpl() {
        System.out.println("实体A操作");
    }
}

class ConcreteImplementorB extends Implementor {
    @Override
    public void operationImpl() {
        System.out.println("实体B操作");
    }
}

3,客户实现类

package com.cnblogs.mufasa.demo1;

public class Client {
    public static void main(String[] args) {
        RefinedAbstraction ra=new RefinedAbstraction(new ConcreteImplementorA());
        ra.operation();

        RefinedAbstraction rb=new RefinedAbstraction(new ConcreteImplementorB());
        rb.operation();
    }
}

猜你喜欢

转载自www.cnblogs.com/Mufasa/p/11390924.html