手机品牌游戏通讯录```
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
HandsetBrand ab;
ab = new HandsetBrandMAddressList();
ab.Run();
ab = new HandsetBrandMGame();
ab.Run();
ab = new HandsetBrandNAddressList();
ab.Run();
ab = new HandsetBrandNGame();
ab.Run();
}
}
class HandsetBrand {
public void Run() {
}
}
//手机品牌M,
class HandsetBrandM extends HandsetBrand {
}
//手机品牌N,
class HandsetBrandN extends HandsetBrand {
}
//手机品牌M的游戏,
class HandsetBrandMGame extends HandsetBrandM {
@Override
public void Run() {
System.out.println("运行M品牌手机游戏");
}
}
//手机品牌N的游戏,
class HandsetBrandNGame extends HandsetBrandN {
@Override
public void Run() {
System.out.println("运行N品牌手机游戏");
}
}
//手机品牌M的通讯录,
class HandsetBrandMAddressList extends HandsetBrandM {
@Override
public void Run() {
System.out.println("运行M品牌手机通讯录");
}
}
//手机品牌N的通讯录,
class HandsetBrandNAddressList extends HandsetBrandN {
@Override
public void Run() {
System.out.println("运行N品牌手机通讯录");
}
}
***松耦合程序***
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
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();
}
}
// 抽象手机软件,
abstract class HandsetSoft {
public abstract void Run();
}
// 手机游戏,
class HandsetGame extends HandsetSoft {
@Override
public void Run() {
System.out.println("运行手机游戏");
}
}
// 手机通讯录,
class HandsetAddressList extends HandsetSoft {
@Override
public void Run() {
System.out.println("运行手机通讯录");
}
}
// 抽象手机品牌,
abstract class HandsetBrand {
protected HandsetSoft soft;
// 设置手机软件,
public void SetHandsetSoft(HandsetSoft soft) {
this.soft = soft;
}
// 运行,
public abstract void Run();
}
// 手机品牌N,
class HandsetBrandN extends HandsetBrand {
@Override
public void Run() {
soft.Run();
}
}
// 手机品牌M,
class HandsetBrandM extends HandsetBrand {
@Override
public void Run() {
soft.Run();
}
}
***
## 如果增加MP3功能,只要添加这个类就行,
不会影响其他任何类,类增加的数量也是一个
***
手机MP3播放,
// class HandsetMP3 : HandsetSoft
// {
// public override void Run()
// {
// Console.WriteLine("运行手机MP3播放");
// }
// }
***
## 如果增加手机品牌S,只要添加这个类就行,
不会影响其他任何类,类增加的数量也是一个
***
手机品牌S,
// class HandsetBrandS : HandsetBrand
// {
// public override void Run()
// {
// soft.Run();
// }
// }
***
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200428223154841.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDM5NjkxNg==,size_16,color_FFFFFF,t_70)
## 桥接模式基本代码
***
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
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 {
@Override
public void Operation() {
System.out.println("具体实现A的方法执行");
}
}
class ConcreteImplementorB extends Implementor {
@Override
public void Operation() {
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 {
@Override
public void Operation() {
implementor.Operation();
}
}