java 桥接模式(大话设计模式)

桥接模式:将抽象部分与它的实现部分分离,使它们都可以独立地变化。使得每种实现的变化不会影响其他实现,从而达到应对变化的目的。【组合】

在这里插入图片描述

/**
 * 
 * @author administrator
 *
 */
public abstract class Abstraction {
    protected Implementor implementor;// 桥接模式的关键,使得Abstraction聚合Implementor
    private String name;

    public Abstraction(String name) {
	this.setName(name);
    }

    public void setImplementor(Implementor implementor) {
	this.implementor = implementor;
    }

    public void operation() {
	System.out.print("Abstraction-" + this.getName() + ": ");
	implementor.operation();
    }

    public String getName() {
	return name;
    }

    public void setName(String name) {
	this.name = name;
    }
}

class AbstractionA extends Abstraction {

    public AbstractionA(String name) {
	super(name);
    }

    @Override
    public void operation() {
	super.operation();
    }

}

class AbstractionB extends Abstraction {

    public AbstractionB(String name) {
	super(name);
    }

    @Override
    public void operation() {
	super.operation();
    }

}
/**
 * 
 * @author administrator
 *
 */
public abstract class Implementor {

    public abstract void operation();

}

class ConcreteImplemtorA extends Implementor {

    @Override
    public void operation() {
	System.out.println("ConcreteImplemtorA的方法执行");

    }

}

class ConcreteImplemtorB extends Implementor {

    @Override
    public void operation() {
	System.out.println("ConcreteImplemtorB的方法执行");

    }

}
/**
 * 客户端
 * 
 * @author administrator
 *
 */
public class BridgeClient {
    public static void main(String[] args) {

	Abstraction a = new AbstractionA("A");
	a.setImplementor(new ConcreteImplemtorA());
	a.operation();
	a.setImplementor(new ConcreteImplemtorB());
	a.operation();

	Abstraction b = new AbstractionB("B");
	b.setImplementor(new ConcreteImplemtorA());
	b.operation();
	b.setImplementor(new ConcreteImplemtorB());
	b.operation();

	// 这样通过使用“组合/聚合复用原则”
	// 如果继续有AbstractionC ... 或者ConcreteImplemtorC ...
	// 只需要扩展类即可,不需要修改现有类,符合“开放-封闭”原则
    }

}

案例

/**
 * 桥接模式(Bridge)
 * 手机品牌抽象类
 */
public abstract class PhoneBrand {

    protected PhoneSoft soft;

    // 设置手机软件
    public void setPhoneSoft(PhoneSoft soft) {
        this.soft = soft;
    }

    public abstract void run();

}
/**
 * 桥接模式(Bridge)
 * 手机品牌M
 */
public class PhoneBrandM extends PhoneBrand {

    @Override
    public void run() {
        soft.run();
    }

}
/**
 * 桥接模式(Bridge)
 * 手机品牌N
 */
public class PhoneBrandN extends PhoneBrand {

    @Override
    public void run() {
        soft.run();
    }

}
/**
 * 桥接模式(Bridge)
 * 手机软件抽象类
 */
public abstract class PhoneSoft {
    public abstract void run();
}
/**
 * 桥接模式(Bridge)
 * 手机游戏
 */
public class PhoneGame extends PhoneSoft {

    @Override
    public void run() {
        System.out.println("运行手机游戏");
    }

}
/**
 * 桥接模式(Bridge)
 * 手机通讯录
 */
public class PhoneAddressList extends PhoneSoft {

    @Override
    public void run() {
        System.out.println("运行手机通讯录");
    }

}

/**
 * 桥接模式(Bridge)
 * 客户端main方法
 */
public class Client {

    public static void main(String[] args) {
        PhoneBrand phoneBrand;
        phoneBrand = new PhoneBrandN();
        phoneBrand.setPhoneSoft(new PhoneGame());
        phoneBrand.run();
        phoneBrand.setPhoneSoft(new PhoneAddressList());
        phoneBrand.run();

        phoneBrand = new PhoneBrandM();
        phoneBrand.setPhoneSoft(new PhoneGame());
        phoneBrand.run();
        phoneBrand.setPhoneSoft(new PhoneAddressList());
        phoneBrand.run();
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值