1. 桥接模式
1.1 合成/聚合复用原则
合成/聚合复用原则,尽量使用合成/聚合,尽量不要使用类继承
- 聚合表示一种弱的拥有关系,体现的是A对象可以包含B对象,但B对象不是A对象的一部分
- 合成则是一种强的拥有关系,体现了严格的部分和整体的关系,部分和整体的生命周期一样
好处
1)优先使用对象的合成/聚合将有助于你保持每个类被封装,并被集中在单个任务上。这样类和类继承层次都会保持在一个较小的层次上
1.2 桥接模式
桥接模式,将抽象部分与它的实现部分分离,使它们都可以独立地变换
- 实现指的是抽象类和它的派生类用来实现自己的对象
public abstract class HandsetSoft {
public abstract void run();
}
public class HandsetGame extends HandsetSoft {
@Override
public void run() {
System.out.println("游戏开始了...");
}
}
public class HandsetAlbum extends HandsetSoft {
@Override
public void run() {
System.out.println("这是一个相册...");
}
}
public abstract class HandsetBrand {
protected HandsetSoft soft;
public void setSoft(HandsetSoft soft) {
this.soft = soft;
}
public abstract void run();
}
public class HandsetM extends HandsetBrand {
@Override
public void run() {
this.soft.run();
}
}
public class HandsetClient {
public static void main(String[] args) {
HandsetM handsetM= new HandsetM();
handsetM.setSoft(new HandsetGame());
handsetM.run();
}
}
2. 命令模式
命令模式,将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销的操作
public abstract class Command {
Receiver receiver;
public Command(Receiver receiver) {
this.receiver = receiver;
}
public abstract void executeCommand();
}
public class BakeCommand extends Command {
public BakeCommand(Receiver receiver) {
super(receiver);
}
@Override
public void executeCommand() {
receiver.Action();
}
}
import java.util.ArrayList;
public class Waiter {
ArrayList<Command> commandArrayList = new ArrayList<>();
public void cancelCommand(Command command) {
commandArrayList.remove(command);
}
public void addCommand(Command command) {
commandArrayList.add(command);
}
public void submitCommand() {
for(Command command: commandArrayList) {
command.executeCommand();
}
}
}
public class Receiver {
public void Action() {
System.out.println("蛋糕做好了!");
}
}
public class CommandClient {
public static void main(String[] args) {
//开店前的准备
Waiter girl = new Waiter();
Receiver baker = new Receiver();
BakeCommand bakeCommand = new BakeCommand(baker);
// 开始点餐
girl.addCommand(bakeCommand);
girl.submitCommand();
}
}
- 能比较容易地设计一个命令队列
- 将命令放入日志
- 允许接收方决定是否接受请求
- 容易实现决定的撤销和重做
- 增加新命令很简单
- 只有真正需要如撤销/恢复操作等功能时,把原来的代码重构为命令模式才有意义
3. 职责链模式
职责链模式,使得多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这个对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止
public abstract class Handler {
public Handler successor;
public void setSuccessor (Handler successor) {
this.successor = successor;
}
public abstract void handleRequest(int request);
}
public class ConcreteHandler extends Handler {
@Override
public void handleRequest(int request) {
if( request < 10) {
System.out.println("ConcreteHandler have handled request " + request);
}
else if (successor != null){
successor.handleRequest(request);
}
}
}
public class ConcreteHandler2 extends Handler {
@Override
public void handleRequest(int request) {
if(request > 10 && request < 100) {
System.out.println("ConcreteHandler2 have handled request " + request);
} else {
System.out.println("Nobody can handle");
}
}
}
public class HandlerClient {
public static void main(String[] args) {
ConcreteHandler concreteHandler = new ConcreteHandler();
ConcreteHandler2 concreteHandler2 = new ConcreteHandler2();
concreteHandler.setSuccessor(concreteHandler2);
concreteHandler.handleRequest(5);
concreteHandler.handleRequest(40);
}
}
- 随时增加或修改处理一个请求,增强了对象指派职责的灵活性
- 但可能出现一个请求到末尾都得不到响应的情况