// 具体建造者类 public class ConcreteBuilder implements Builder { private String partA; private String partB;
@Override public void buildPartA() { partA = "Part A"; }
@Override public void buildPartB() { partB = "Part B"; }
@Override public Product getResult() { return new Product(partA, partB); } }
// 指导者类 public class Director { private Builder builder;
public Director(Builder builder) { this.builder = builder; }
public void construct() { builder.buildPartA(); builder.buildPartB(); } }
// 客户端代码 public class Client { public static void main(String[] args) { Builder builder = new ConcreteBuilder(); Director director = new Director(builder); director.construct(); Product product = builder.getResult(); System.out.println(product); } }
// 实现接口 public interface Implementor { void operation(); }
// 具体实现A public class ConcreteImplementorA implements Implementor { @Override public void operation() { System.out.println("ConcreteImplementorA operation"); } }
// 具体实现B public class ConcreteImplementorB implements Implementor { @Override public void operation() { System.out.println("ConcreteImplementorB operation"); } }
// 抽象类 public abstract class Abstraction { protected Implementor implementor;
public Abstraction(Implementor implementor) { this.implementor = implementor; }
public abstract void operation(); }
// 扩展抽象类 public class RefinedAbstraction extends Abstraction { public RefinedAbstraction(Implementor implementor) { super(implementor); }
@Override public void operation() { implementor.operation(); // 委托给实现类 } }
// 客户端代码 public class Client { public static void main(String[] args) { Implementor implementorA = new ConcreteImplementorA(); Abstraction abstraction = new RefinedAbstraction(implementorA); abstraction.operation();
Implementor implementorB = new ConcreteImplementorB(); abstraction = new RefinedAbstraction(implementorB); abstraction.operation(); } }
// 组件接口 public interface Component { void operation(); }
// 叶子节点 public class Leaf implements Component { @Override public void operation() { System.out.println("Leaf operation"); } }
// 容器节点 public class Composite implements Component { private List<Component> children = new ArrayList<>();
public void add(Component component) { children.add(component); }
public void remove(Component component) { children.remove(component); }
@Override public void operation() { for (Component child : children) { child.operation(); } } }
// 客户端代码 public class Client { public static void main(String[] args) { Composite root = new Composite(); Component leaf1 = new Leaf(); Component leaf2 = new Leaf();
// 处理者接口 public abstract class Handler { protected Handler nextHandler;
public void setNextHandler(Handler nextHandler) { this.nextHandler = nextHandler; }
public abstract void handleRequest(String request); }
// 具体处理者A public class ConcreteHandlerA extends Handler { @Override public void handleRequest(String request) { if (request.equals("A")) { System.out.println("Handler A handling request A"); } else if (nextHandler != null) { nextHandler.handleRequest(request); } } }
// 具体处理者B public class ConcreteHandlerB extends Handler { @Override public void handleRequest(String request) { if (request.equals("B")) { System.out.println("Handler B handling request B"); } else if (nextHandler != null) { nextHandler.handleRequest(request); } } }
// 客户端代码 public class Client { public static void main(String[] args) { Handler handlerA = new ConcreteHandlerA(); Handler handlerB = new ConcreteHandlerB(); handlerA.setNextHandler(handlerB);
// 客户端代码 public class Client { public static void main(String[] args) { ConcreteMediator mediator = new ConcreteMediator(); ComponentA componentA = new ComponentA(mediator); ComponentB componentB = new ComponentB(mediator); mediator.setComponentA(componentA); mediator.setComponentB(componentB);
// 备忘录类 public class Memento { private String state;
public Memento(String state) { this.state = state; }
public String getState() { return state; } }
// 发起人类 public class Originator { private String state;
public void setState(String state) { this.state = state; }
public String getState() { return state; }
public Memento saveStateToMemento() { return new Memento(state); }
public void getStateFromMemento(Memento memento) { state = memento.getState(); } }
// 管理者类 public class Caretaker { private Memento memento;
public void saveMemento(Memento memento) { this.memento = memento; }
public Memento retrieveMemento() { return memento; } }
// 客户端代码 public class Client { public static void main(String[] args) { Originator originator = new Originator(); Caretaker caretaker = new Caretaker();
@Override public int interpret(Map<String, Integer> context) { return left.interpret(context) + right.interpret(context); } }
// 客户端代码 public class Client { public static void main(String[] args) { Expression expression = new PlusExpression(new NumberExpression(5), new NumberExpression(3)); Map<String, Integer> context = new HashMap<>(); int result = expression.interpret(context); System.out.println("Result: " + result); // 输出结果 8 } }
// 状态接口 public interface State { void handle(Context context); }
// 具体状态A public class ConcreteStateA implements State { @Override public void handle(Context context) { System.out.println("Handling state A"); context.setState(new ConcreteStateB()); // 切换到状态B } }
// 具体状态B public class ConcreteStateB implements State { @Override public void handle(Context context) { System.out.println("Handling state B"); context.setState(new ConcreteStateA()); // 切换到状态A } }
// 上下文类 public class Context { private State state;
public Context(State state) { this.state = state; }
public void setState(State state) { this.state = state; }
public void request() { state.handle(this); } }
// 客户端代码 public class Client { public static void main(String[] args) { Context context = new Context(new ConcreteStateA()); context.request(); // 处理状态A context.request(); // 处理状态B } }
// 具体访问者A public class ConcreteVisitorA implements Visitor { @Override public void visit(ConcreteElementA elementA) { System.out.println("Visitor A visiting Element A"); }
@Override public void visit(ConcreteElementB elementB) { System.out.println("Visitor A visiting Element B"); } }
// 具体访问者B public class ConcreteVisitorB implements Visitor { @Override public void visit(ConcreteElementA elementA) { System.out.println("Visitor B visiting Element A"); }
@Override public void visit(ConcreteElementB elementB) { System.out.println("Visitor B visiting Element B"); } }
// 元素接口 public interface Element { void accept(Visitor visitor); }
// 具体元素A public class ConcreteElementA implements Element { @Override public void accept(Visitor visitor) { visitor.visit(this); } }
// 具体元素B public class ConcreteElementB implements Element { @Override public void accept(Visitor visitor) { visitor.visit(this); } }
// 对象结构 public class ObjectStructure { private List<Element> elements = new ArrayList<>();
public void addElement(Element element) { elements.add(element); }
public void accept(Visitor visitor) { for (Element element : elements) { element.accept(visitor); } } }
// 客户端代码 public class Client { public static void main(String[] args) { ObjectStructure structure = new ObjectStructure(); structure.addElement(new ConcreteElementA()); structure.addElement(new ConcreteElementB());
Visitor visitorA = new ConcreteVisitorA(); structure.accept(visitorA); // Visitor A visiting elements
Visitor visitorB = new ConcreteVisitorB(); structure.accept(visitorB); // Visitor B visiting elements } }
// 客户端代码 public class Client { public static void main(String[] args) { ConcreteSubject subject = new ConcreteSubject(); Observer observer1 = new ConcreteObserver("Observer1"); Observer observer2 = new ConcreteObserver("Observer2"); subject.addObserver(observer1); subject.addObserver(observer2);