//紧耦合
/*public class Main {
public static void main(String args[])
{
HandsetBrand hb;
hb= new HandsetBrandMAddressList();
hb.Ran();
hb=new HandsetBrandMGame();
hb.Ran();
hb=new HandsetBrandNAddressList();
hb.Ran();
hb=new HandsetBrandNGame();
hb.Ran();
}
}
class HandsetBrand{
public void Ran() {}
}
class HandsetBrandM extends HandsetBrand{
}
class HandsetBrandN extends HandsetBrand{
}
class HandsetBrandMGame extends HandsetBrandM{
public void Ran() {
System.out.println("运行M品牌手机游戏");
}
}
class HandsetBrandNGame extends HandsetBrandN{
public void Ran() {
System.out.println("运行N品牌手机游戏");
}
}
class HandsetBrandMAddressList extends HandsetBrandM{
public void Ran() {
System.out.println("运行M品牌手机通讯录");
}
}
class HandsetBrandNAddressList extends HandsetBrandM{
public void Ran() {
System.out.println("运行M品牌手机通讯录");
}
}*/
//桥接模式基本代码
/*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();
}
}
abstract class Implementor{
public abstract void Operation();
}
class ConcreteImplementorA extends Implementor{
public void Operation() {
// TODO Auto-generated method stub
System.out.println("具体实现A的方法执行");
}
}
class ConcreteImplementorB extends Implementor{
public void Operation() {
// 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.Operation();
}
}
class RefinedAbstraction extends Abstraction{
public void Operation() {
implementor.Operation();
}
}*/
//松耦合/桥接模式
public class Main {
public static void main(String args[])
{
HandsetBrand hb;
hb=new HandsetBrandN();
hb.setSoft(new HandsetGame());
hb.Run();
hb.setSoft(new HandsetAddressList());
hb.Run();
hb=new HandsetBrandM();
hb.setSoft(new HandsetGame());
hb.Run();
hb.setSoft(new HandsetAddressList());
hb.Run();
}
}
abstract class HandsetSoft{
public abstract void Run();
}
class HandsetGame extends HandsetSoft{
public void Run() {
// TODO Auto-generated method stub
System.out.println("运行手机游戏");
}
}
class HandsetAddressList extends HandsetSoft{
public void Run() {
// TODO Auto-generated method stub
System.out.println("运行手机通讯录");
}
}
abstract class HandsetBrand{
protected HandsetSoft soft;
public void setSoft(HandsetSoft soft) {
this.soft = soft;
}
public abstract void Run();
}
class HandsetBrandN extends HandsetBrand{
public void Run() {
// TODO Auto-generated method stub
soft.Run();
}
}
class HandsetBrandM extends HandsetBrand{
public void Run() {
// TODO Auto-generated method stub
soft.Run();
}
}
桥接模式 实现
最新推荐文章于 2024-01-16 10:20:11 发布