一、紧耦合的程序演化
例:有一个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();
}
}