设计模式(十五)——桥接模式

一、紧耦合的程序演化
例:有一个N品牌手机,调用游戏

class HandsetNGame{  //N品牌手机中的游戏
	public void Run() {
		System.out.println("运行N品牌手机游戏");
	}
}
public class Main {
	public static void main(String[] args) {
		HandsetNGame game=new HandsetNGame();
		game.Run();
	}
}

改进:加入M品牌手机和手机通讯录

class HandsetGame { // 手机游戏
	public void Run() {

	}
}

class HandsetMGame extends HandsetGame {
	public void Run() {
		System.out.println("运行M品牌手机游戏");
	}
}

class HandsetNGame extends HandsetGame {
	public void Run() {
		System.out.println("运行N品牌手机游戏");
	}
}

class HandsetBrand { // 手机通讯录
	public void Run() {

	}
}

class HandsetBrandM extends HandsetBrand {

}

class HandsetBrandN extends HandsetBrand {

}

class HandsetBrandMGame extends HandsetBrandM {
	public void Run() {
		System.out.println("运行M品牌手机游戏");
	}
}

class HandsetBrandNGame extends HandsetBrandN {
	public void Run() {
		System.out.println("运行N品牌手机游戏");
	}
}

class HandsetBrandMAddressList extends HandsetBrandM {
	public void Run() {
		System.out.println("运行M品牌手机通讯录");
	}
}

class HandsetBrandNAddressList extends HandsetBrandN {
	public void Run() {
		System.out.println("运行N品牌手机通讯录");
	}
}

public class Main {
	public static void main(String[] args) {
		HandsetBrand ab;
		ab = new HandsetBrandMAddressList();
		ab.Run();
		ab = new HandsetBrandMGame();
		ab.Run();
		ab = new HandsetBrandNAddressList();
		ab.Run();
		ab = new HandsetBrandNGame();
		ab.Run();
	}
}

二、松耦合的程序

abstract class HandsetSoft { // 手机软件
	public abstract void Run();
}

//游戏、通讯录等具体类
class HandsetGame extends HandsetSoft {

	@Override
	public void Run() {
		// TODO Auto-generated method stub
		System.out.println("运行手机游戏");
	}

}

class HandsetAddressList extends HandsetSoft {

	@Override
	public void Run() {
		// TODO Auto-generated method stub
		System.out.println("运行手机通讯录");
	}

}

//手机品牌类
abstract class HandsetBrand {
	protected HandsetSoft soft;

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

	public abstract void Run(); // 运行
}

//品牌N品牌M具体类
class HandsetBrandN extends HandsetBrand {

	@Override
	public void Run() {
		// TODO Auto-generated method stub
		soft.Run();
	}

}

class HandsetBrandM extends HandsetBrand {

	@Override
	public void Run() {
		// TODO Auto-generated method stub
		soft.Run();
	}

}

public class Main {
	public static void main(String[] args) {
		HandsetBrand ab;
		ab = new HandsetBrandN();
		ab.SetHandsetSoft(new HandsetGame());
		ab.Run();
		ab.SetHandsetSoft(new HandsetAddressList());
		ab.Run();
		ab = new HandsetBrandM();
		ab.SetHandsetSoft(new HandsetGame());
		ab.Run();
		ab.SetHandsetSoft(new HandsetAddressList());
		ab.Run();
	}
}

如果要增加一个功能,只要增加一个品牌子类就好了。
例如:

class HandsetMP3 extends HandsetSoft{

	@Override
	public void Run() {
		// TODO Auto-generated method stub
		System.out.println("运行手机MP3播放");
	}
	
}

增加一个品牌,例如:

class HandsetVrandS extends HandsetBrand{

	@Override
	public void Run() {
		// TODO Auto-generated method stub
		soft.Run();
	}
	
}

三、桥接模式
桥接模式,将抽象部分与它的实现部分分离,使它们都可以独立地变化。

abstract class Implementor {
	public abstract void Opreation();
}

class ConcreteImplementorA extends Implementor {

	@Override
	public void Opreation() {
		// TODO Auto-generated method stub
		System.out.println("具体实现A的方法执行");
	}

}

class ConcreteImplementorB extends Implementor {

	@Override
	public void Opreation() {
		// TODO Auto-generated method stub
		System.out.println("具体实现B的方法执行");
	}

}

class Abstraction {
	protected Implementor implementor;

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

	public void Operation() {
		implementor.Opreation();
	}
}

class RefinedAbstraction extends Abstraction {
	public void Opreation() {
		implementor.Opreation();
	}
}

public class Main {
	public static void main(String[] args) {
		Abstraction ab = new RefinedAbstraction();
		ab.SetImplementor(new ConcreteImplementorA());
		ab.Operation();
		ab.SetImplementor(new ConcreteImplementorB());
		ab.Operation();
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值