网上有很多设计模式方面的内容,这篇写给像我一样的菜鸟 供大家体会 注该代码为转载代码 写的很好 拿来用一下 本着开源的精神 望原作者见谅

//	Bridge模式是构造型的设计模式之一。Bridge模式基于类的最小设计原则,通过使用封装,聚合以及继承等行为来让不同的类承担不同的责任。它的主要特点是把抽象(abstraction)与行为实现(implementation)分离开来,从而可以保持各部分的独立性以及应对它们的功能扩展。

package com.test.pattern.bridge;


public abstract class AbstractComputer {


CpuAbility cpuAbility;


     public abstract void checkAbility();

public CpuAbility getCpuAbility() {

return cpuAbility;

}


public void setCpuAbility(CpuAbility cpuAbility) {

this.cpuAbility = cpuAbility;

}

     public AbstractComputer(CpuAbility cpuAbility) {

this.cpuAbility = cpuAbility;

}



}

/**

 * 

 */

package com.test.pattern.bridge;


/**

 * @author acer

 *

 */

public class AmdCpu implements CpuAbility {


/* (non-Javadoc)

* @see com.test.pattern.bridge.CpuAbility#abilityCpu()

*/

@Override

public String abilityCpu() {

// TODO Auto-generated method stub

return "amdcup 一般";

}


}



public interface CpuAbility {

  String abilityCpu();

}




public class IntelCpu implements CpuAbility {


@Override

public String abilityCpu() {

// TODO Auto-generated method stub

return "intel cpu 性能牛逼";

}


}


public class IswComputer extends AbstractComputer {


public IswComputer(CpuAbility cpuAbility) {

super(cpuAbility);

// TODO Auto-generated constructor stub

}


@Override

public void checkAbility() {

// TODO Auto-generated method stub

            System.out.println("华硕笔记本 性能牛逼"+cpuAbility.abilityCpu());

}


}

package com.test.pattern.bridge;


public class LenevoComputer extends AbstractComputer{


public LenevoComputer(CpuAbility cpuAbility) {

super(cpuAbility);

// TODO Auto-generated constructor stub

}


@Override

public void checkAbility() {

// TODO Auto-generated method stub

System.out.println(cpuAbility.abilityCpu()+"lenovo");

}


 


}

public class TestBridge {

public static void main(String[] args){


CpuAbility ability =new IntelCpu();

AbstractComputer computer =new LenevoComputer(ability);

computer.checkAbility();



}

// 可以看到,通过对象组合的方式,Bridge 模式把两个角色之间的继承关系改为了耦合的关系,从而使这两者可以从容自若的各自独立的变化,这也是Bridge模式的本意。





}