掌握Java设计模式的23种武器(全):深入解析与实战示例

目录

一、创建型模式

1. 单例模式 (Singleton Pattern)

2. 工厂模式 (Factory Pattern)

3. 抽象工厂模式 (Abstract Factory Pattern)

4. 建造者模式 (Builder Pattern)

5. 原型模式 (Prototype Pattern)

二、结构型模式

6. 适配器模式 (Adapter Pattern)

7. 桥接模式 (Bridge Pattern)

8. 组合模式 (Composite Pattern)

9. 装饰器模式 (Decorator Pattern)

10. 外观模式 (Facade Pattern)

11. 享元模式 (Flyweight Pattern)

12. 代理模式 (Proxy Pattern)

三、行为型模式

13. 责任链模式 (Chain of Responsibility Pattern)

14. 命令模式 (Command Pattern)

15. 解释器模式 (Interpreter Pattern)

16. 迭代器模式 (Iterator Pattern)

17. 中介者模式 (Mediator Pattern)

18. 备忘录模式 (Memento Pattern)

19. 观察者模式 (Observer Pattern)

20. 状态模式 (State Pattern)

21. 空对象模式 (Null Object Pattern)

22. 策略模式 (Strategy Pattern)

23. 模板模式 (Template Pattern)

24. 访问者模式 (Visitor Pattern)


一、创建型模式

1. 单例模式 (Singleton Pattern)

类比:如同系统中的打印机管理器,无论你调用多少次获取打印机管理器实例的方法,都返回同一个实例。

示例代码

public class Singleton {// 创建 SingleObject 的一个对象private static Singleton instance = new Singleton();// 让构造函数为 private,这样该类就不会被实例化private Singleton() {}// 获取唯一可用的对象public static Singleton getInstance() {return instance;}public void showMessage() {System.out.println("Hello World!");}
}public class SingletonPatternDemo {public static void main(String[] args) {// 不合法的构造函数// 编译时错误:构造函数 Singleton() 是不可见的// Singleton object = new Singleton();// 获取唯一可用的对象Singleton object = Singleton.getInstance();// 显示消息object.showMessage();}
}
2. 工厂模式 (Factory Pattern)

类比:如同汽车工厂,根据不同的订单生产不同类型的汽车。

示例代码

interface Shape {void draw();
}class Rectangle implements Shape {public void draw() {System.out.println("Inside Rectangle::draw() method.");}
}class Square implements Shape {public void draw() {System.out.println("Inside Square::draw() method.");}
}class Circle implements Shape {public void draw() {System.out.println("Inside Circle::draw() method.");}
}class ShapeFactory {// 使用 getShape 方法获取形状类型的对象public Shape getShape(String shapeType) {if (shapeType == null) {return null;}if (shapeType.equalsIgnoreCase("CIRCLE")) {return new Circle();} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {return new Rectangle();} else if (shapeType.equalsIgnoreCase("SQUARE")) {return new Square();}return null;}
}public class FactoryPatternDemo {public static void main(String[] args) {ShapeFactory shapeFactory = new ShapeFactory();// 获取 Circle 对象,并调用它的 draw 方法Shape shape1 = shapeFactory.getShape("CIRCLE");shape1.draw();// 获取 Rectangle 对象,并调用它的 draw 方法Shape shape2 = shapeFactory.getShape("RECTANGLE");shape2.draw();// 获取 Square 对象,并调用它的 draw 方法Shape shape3 = shapeFactory.getShape("SQUARE");shape3.draw();}
}
3. 抽象工厂模式 (Abstract Factory Pattern)

类比:如同一个提供家电的工厂,有不同的产品系列,如电视、冰箱、洗衣机,每个系列都有具体的产品。

示例代码

interface Shape {void draw();
}class Rectangle implements Shape {public void draw() {System.out.println("Inside Rectangle::draw() method.");}
}class Square implements Shape {public void draw() {System.out.println("Inside Square::draw() method.");}
}class Circle implements Shape {public void draw() {System.out.println("Inside Circle::draw() method.");}
}abstract class AbstractFactory {abstract Shape getShape(String shapeType);
}class ShapeFactory extends AbstractFactory {public Shape getShape(String shapeType) {if (shapeType == null) {return null;}if (shapeType.equalsIgnoreCase("CIRCLE")) {return new Circle();} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {return new Rectangle();} else if (shapeType.equalsIgnoreCase("SQUARE")) {return new Square();}return null;}
}class FactoryProducer {public static AbstractFactory getFactory(String choice) {if (choice.equalsIgnoreCase("SHAPE")) {return new ShapeFactory();}return null;}
}public class AbstractFactoryPatternDemo {public static void main(String[] args) {// 获取形状工厂AbstractFactory shapeFactory = FactoryProducer.getFactory("SHAPE");// 获取 Circle 对象,并调用它的 draw 方法Shape shape1 = shapeFactory.getShape("CIRCLE");shape1.draw();// 获取 Rectangle 对象,并调用它的 draw 方法Shape shape2 = shapeFactory.getShape("RECTANGLE");shape2.draw();// 获取 Square 对象,并调用它的 draw 方法Shape shape3 = shapeFactory.getShape("SQUARE");shape3.draw();}
}
4. 建造者模式 (Builder Pattern)

类比:如同建造房屋,有不同的步骤如打地基、建墙、装屋顶,每一步都可以有不同的选择。

示例代码

class Meal {private List<Item> items = new ArrayList<Item>();public void addItem(Item item) {items.add(item);}public float getCost() {float cost = 0.0f;for (Item item : items) {cost += item.price();}return cost;}public void showItems() {for (Item item : items) {System.out.print("Item : " + item.name());System.out.print(", Packing : " + item.packing().pack());System.out.println(", Price : " + item.price());}}
}interface Item {String name();Packing packing();float price();
}interface Packing {String pack();
}class Wrapper implements Packing {public String pack() {return "Wrapper";}
}class Bottle implements Packing {public String pack() {return "Bottle";}
}abstract class Burger implements Item {public Packing packing() {return new Wrapper();}public abstract float price();
}abstract class ColdDrink implements Item {public Packing packing() {return new Bottle();}public abstract float price();
}class VegBurger extends Burger {public float price() {return 25.0f;}public String name() {return "Veg Burger";}
}class ChickenBurger extends Burger {public float price() {return 50.5f;}public String name() {return "Chicken Burger";}
}class Coke extends ColdDrink {public float price() {return 30.0f;}public String name() {return "Coke";}
}class Pepsi extends ColdDrink {public float price() {return 35.0f;}public String name() {return "Pepsi";}
}class MealBuilder {public Meal prepareVegMeal() {Meal meal = new Meal();meal.addItem(new VegBurger());meal.addItem(new Coke());return meal;}public Meal prepareNonVegMeal() {Meal meal = new Meal();meal.addItem(new ChickenBurger());meal.addItem(new Pepsi());return meal;}
}public class BuilderPatternDemo {public static void main(String[] args) {MealBuilder mealBuilder = new MealBuilder();Meal vegMeal = mealBuilder.prepareVegMeal();System.out.println("Veg Meal");vegMeal.showItems();System.out.println("Total Cost: " + vegMeal.getCost());Meal nonVegMeal = mealBuilder.prepareNonVegMeal();System.out.println("\nNon-Veg Meal");nonVegMeal.showItems();System.out.println("Total Cost: " + nonVegMeal.getCost());}
}
5. 原型模式 (Prototype Pattern)

类比:如同剪贴板中的复制功能,可以复制现有的对象并创建新的对象。

示例代码

abstract class Shape implements Cloneable {private String id;protected String type;abstract void draw();public String getType() {return type;}public String getId() {return id;}public void setId(String id) {this.id = id;}public Object clone() {Object clone = null;try {clone = super.clone();} catch (CloneNotSupportedException e) {e.printStackTrace();}return clone;}
}class Rectangle extends Shape {public Rectangle() {type = "Rectangle";}public void draw() {System.out.println("Inside Rectangle::draw() method.");}
}class Square extends Shape {public Square() {type = "Square";}public void draw() {System.out.println("Inside Square::draw() method.");}
}class Circle extends Shape {public Circle() {type = "Circle";}public void draw() {System.out.println("Inside Circle::draw() method.");}
}class ShapeCache {private static Hashtable<String, Shape> shapeMap = new Hashtable<String, Shape>();public static Shape getShape(String shapeId) {Shape cachedShape = shapeMap.get(shapeId);return (Shape) cachedShape.clone();}public static void loadCache() {Circle circle = new Circle();circle.setId("1");shapeMap.put(circle.getId(), circle);Square square = new Square();square.setId("2");shapeMap.put(square.getId(), square);Rectangle rectangle = new Rectangle();rectangle.setId("3");shapeMap.put(rectangle.getId(), rectangle);}
}public class PrototypePatternDemo {public static void main(String[] args) {ShapeCache.loadCache();Shape clonedShape = (Shape) ShapeCache.getShape("1");System.out.println("Shape : " + clonedShape.getType());Shape clonedShape2 = (Shape) ShapeCache.getShape("2");System.out.println("Shape : " + clonedShape2.getType());Shape clonedShape3 = (Shape) ShapeCache.getShape("3");System.out.println("Shape : " + clonedShape3.getType());}
}

二、结构型模式

6. 适配器模式 (Adapter Pattern)

类比:如同电源适配器,可以将一种类型的插头转换为另一种类型的插座。

示例代码

interface MediaPlayer {void play(String audioType, String fileName);
}interface AdvancedMediaPlayer {void playVlc(String fileName);void playMp4(String fileName);
}class VlcPlayer implements AdvancedMediaPlayer {public void playVlc(String fileName) {System.out.println("Playing vlc file. Name: " + fileName);}public void playMp4(String fileName) {// 什么也不做}
}class Mp4Player implements AdvancedMediaPlayer {public void playVlc(String fileName) {// 什么也不做}public void playMp4(String fileName) {System.out.println("Playing mp4 file. Name: " + fileName);}
}class MediaAdapter implements MediaPlayer {AdvancedMediaPlayer advancedMusicPlayer;public MediaAdapter(String audioType) {if (audioType.equalsIgnoreCase("vlc")) {advancedMusicPlayer = new VlcPlayer();} else if (audioType.equalsIgnoreCase("mp4")) {advancedMusicPlayer = new Mp4Player();}}public void play(String audioType, String fileName) {if (audioType.equalsIgnoreCase("vlc")) {advancedMusicPlayer.playVlc(fileName);} else if (audioType.equalsIgnoreCase("mp4")) {advancedMusicPlayer.playMp4(fileName);}}
}class AudioPlayer implements MediaPlayer {MediaAdapter mediaAdapter;public void play(String audioType, String fileName) {// 内置支持 mp3 播放if (audioType.equalsIgnoreCase("mp3")) {System.out.println("Playing mp3 file. Name: " + fileName);}// mediaAdapter 提供其他格式支持else if (audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")) {mediaAdapter = new MediaAdapter(audioType);mediaAdapter.play(audioType, fileName);} else {System.out.println("Invalid media. " + audioType + " format not supported");}}
}public class AdapterPatternDemo {public static void main(String[] args) {AudioPlayer audioPlayer = new AudioPlayer();audioPlayer.play("mp3", "beyond the horizon.mp3");audioPlayer.play("mp4", "alone.mp4");audioPlayer.play("vlc", "far far away.vlc");audioPlayer.play("avi", "mind me.avi");}
}
7. 桥接模式 (Bridge Pattern)

类比:如同不同品牌的遥控器控制不同类型的电视,遥控器和电视之间通过桥接模式实现。

示例代码

interface DrawAPI {void drawCircle(int radius, int x, int y);
}class RedCircle implements DrawAPI {public void drawCircle(int radius, int x, int y) {System.out.println("Drawing Circle[ color: red, radius: " + radius + ", x: " + x + ", " + y + "]");}
}class GreenCircle implements DrawAPI {public void drawCircle(int radius, int x, int y) {System.out.println("Drawing Circle[ color: green, radius: " + radius + ", x: " + x + ", " + y + "]");}
}abstract class Shape {protected DrawAPI drawAPI;protected Shape(DrawAPI drawAPI) {this.drawAPI = drawAPI;}public abstract void draw();
}class Circle extends Shape {private int x, y, radius;public Circle(int x, int y, int radius, DrawAPI drawAPI) {super(drawAPI);this.x = x;this.y = y;this.radius = radius;}public void draw() {drawAPI.drawCircle(radius, x, y);}
}public class BridgePatternDemo {public static void main(String[] args) {Shape redCircle = new Circle(100, 100, 10, new RedCircle());Shape greenCircle = new Circle(100, 100, 10, new GreenCircle());redCircle.draw();greenCircle.draw();}
}
8. 组合模式 (Composite Pattern)

类比:如同一个文件系统,文件夹可以包含文件或其他文件夹,文件和文件夹通过组合模式形成树形结构。

示例代码

import java.util.ArrayList;
import java.util.List;class Employee {private String name;private String dept;private int salary;private List<Employee> subordinates;// 构造函数public Employee(String name, String dept, int salary) {this.name = name;this.dept = dept;this.salary = salary;subordinates = new ArrayList<Employee>();}public void add(Employee e) {subordinates.add(e);}public void remove(Employee e) {subordinates.remove(e);}public List<Employee> getSubordinates() {return subordinates;}public String toString() {return ("Employee :[ Name : " + name + ", dept : " + dept + ", salary :" + salary + " ]");}
}public class CompositePatternDemo {public static void main(String[] args) {Employee CEO = new Employee("John", "CEO", 30000);Employee headSales = new Employee("Robert", "Head Sales", 20000);Employee headMarketing = new Employee("Michel", "Head Marketing", 20000);Employee clerk1 = new Employee("Laura", "Marketing", 10000);Employee clerk2 = new Employee("Bob", "Marketing", 10000);Employee salesExecutive1 = new Employee("Richard", "Sales", 10000);Employee salesExecutive2 = new Employee("Rob", "Sales", 10000);CEO.add(headSales);CEO.add(headMarketing);headSales.add(salesExecutive1);headSales.add(salesExecutive2);headMarketing.add(clerk1);headMarketing.add(clerk2);// 打印所有员工System.out.println(CEO);for (Employee headEmployee : CEO.getSubordinates()) {System.out.println(headEmployee);for (Employee employee : headEmployee.getSubordinates()) {System.out.println(employee);}}}
}
9. 装饰器模式 (Decorator Pattern)

类比:如同在一件衣服上添加不同的装饰物,例如纽扣、拉链,可以在不改变衣服结构的前提下添加额外功能。

示例代码

interface Shape {void draw();
}class Rectangle implements Shape {public void draw() {System.out.println("Shape: Rectangle");}
}class Circle implements Shape {public void draw() {System.out.println("Shape: Circle");}
}abstract class ShapeDecorator implements Shape {protected Shape decoratedShape;public ShapeDecorator(Shape decoratedShape) {this.decoratedShape = decoratedShape;}public void draw() {decoratedShape.draw();}
}class RedShapeDecorator extends ShapeDecorator {public RedShapeDecorator(Shape decoratedShape) {super(decoratedShape);}@Overridepublic void draw() {decoratedShape.draw();setRedBorder(decoratedShape);}private void setRedBorder(Shape decoratedShape) {System.out.println("Border Color: Red");}
}public class DecoratorPatternDemo {public static void main(String[] args) {Shape circle = new Circle();Shape redCircle = new RedShapeDecorator(new Circle());Shape redRectangle = new RedShapeDecorator(new Rectangle());System.out.println("Circle with normal border");circle.draw();System.out.println("\nCircle of red border");redCircle.draw();System.out.println("\nRectangle of red border");redRectangle.draw();}
}
10. 外观模式 (Facade Pattern)

类比:如同电影院售票系统,购票过程复杂但对外只提供简单的购票接口。

示例代码

class ShapeMaker {private Shape circle;private Shape rectangle;private Shape square;public ShapeMaker() {circle = new Circle();rectangle = new Rectangle();square = new Square();}public void drawCircle() {circle.draw();}public void drawRectangle() {rectangle.draw();}public void drawSquare() {square.draw();}
}public class FacadePatternDemo {public static void main(String[] args) {ShapeMaker shapeMaker = new ShapeMaker();shapeMaker.drawCircle();shapeMaker.drawRectangle();shapeMaker.drawSquare();}
}
11. 享元模式 (Flyweight Pattern)

类比:如同在文本编辑器中,多个相同的字符只存储一份节省内存。

示例代码

import java.util.HashMap;interface Shape {void draw();
}class Circle implements Shape {private String color;private int x;private int y;private int radius;public Circle(String color) {this.color = color;}public void setX(int x) {this.x = x;}public void setY(int y) {this.y = y;}public void setRadius(int radius) {this.radius = radius;}public void draw() {System.out.println("Circle: Draw() [Color : " + color + ", x : " + x + ", y :" + y + ", radius :" + radius);}
}class ShapeFactory {private static final HashMap<String, Shape> circleMap = new HashMap<>();public static Shape getCircle(String color) {Circle circle = (Circle) circleMap.get(color);if (circle == null) {circle = new Circle(color);circleMap.put(color, circle);System.out.println("Creating circle of color : " + color);}return circle;}
}public class FlyweightPatternDemo {private static final String colors[] = {"Red", "Green", "Blue", "White", "Black"};public static void main(String[] args) {for (int i = 0; i < 20; ++i) {Circle circle = (Circle) ShapeFactory.getCircle(getRandomColor());circle.setX(getRandomX());circle.setY(getRandomY());circle.setRadius(100);circle.draw();}}private static String getRandomColor() {return colors[(int) (Math.random() * colors.length)];}private static int getRandomX() {return (int) (Math.random() * 100);}private static int getRandomY() {return (int) (Math.random() * 100);}
}
12. 代理模式 (Proxy Pattern)

类比:如同明星请经纪人代理处理事务,经纪人是代理模式的实例。

示例代码

interface Image {void display();
}class RealImage implements Image {private String fileName;public RealImage(String fileName) {this.fileName = fileName;loadFromDisk(fileName);}private void loadFromDisk(String fileName) {System.out.println("Loading " + fileName);}public void display() {System.out.println("Displaying " + fileName);}
}class ProxyImage implements Image {private RealImage realImage;private String fileName;public ProxyImage(String fileName) {this.fileName = fileName;}public void display() {if (realImage == null) {realImage = new RealImage(fileName);}realImage.display();}
}public class ProxyPatternDemo {public static void main(String[] args) {Image image = new ProxyImage("test_10mb.jpg");// 图像将从磁盘加载image.display();System.out.println("");// 图像不需要从磁盘加载image.display();}
}

三、行为型模式

13. 责任链模式 (Chain of Responsibility Pattern)

类比:如同企业内部的审批流程,申请需要层层递交直到满足条件。

示例代码

abstract class AbstractLogger {public static int INFO = 1;public static int DEBUG = 2;public static int ERROR = 3;protected int level;// 责任链中的下一个元素protected AbstractLogger nextLogger;public void setNextLogger(AbstractLogger nextLogger) {this.nextLogger = nextLogger;}public void logMessage(int level, String message) {if (this.level <= level) {write(message);}if (nextLogger != null) {nextLogger.logMessage(level, message);}}abstract protected void write(String message);
}class ConsoleLogger extends AbstractLogger {public ConsoleLogger(int level) {this.level = level;}protected void write(String message) {System.out.println("Standard Console::Logger: " + message);}
}class ErrorLogger extends AbstractLogger {public ErrorLogger(int level) {this.level = level;}protected void write(String message) {System.out.println("Error Console::Logger: " + message);}
}class FileLogger extends AbstractLogger {public FileLogger(int level) {this.level = level;}protected void write(String message) {System.out.println("File::Logger: " + message);}
}public class ChainPatternDemo {private static AbstractLogger getChainOfLoggers() {AbstractLogger errorLogger = new ErrorLogger(AbstractLogger.ERROR);AbstractLogger fileLogger = new FileLogger(AbstractLogger.DEBUG);AbstractLogger consoleLogger = new ConsoleLogger(AbstractLogger.INFO);errorLogger.setNextLogger(fileLogger);fileLogger.setNextLogger(consoleLogger);return errorLogger;}public static void main(String[] args) {AbstractLogger loggerChain = getChainOfLoggers();loggerChain.logMessage(AbstractLogger.INFO, "This is an information.");loggerChain.logMessage(AbstractLogger.DEBUG, "This is a debug level information.");loggerChain.logMessage(AbstractLogger.ERROR, "This is an error information.");}
}
14. 命令模式 (Command Pattern)

类比:如同电灯开关,通过开关可以控制电灯的打开和关闭,开关就是命令模式的实例。

示例代码

interface Order {void execute();
}class Stock {private String name = "ABC";private int quantity = 10;public void buy() {System.out.println("Stock [ Name: " + name + ", Quantity: " + quantity + " ] bought");}public void sell() {System.out.println("Stock [ Name: " + name + ", Quantity: " + quantity + " ] sold");}
}class BuyStock implements Order {private Stock abcStock;public BuyStock(Stock abcStock) {this.abcStock = abcStock;}public void execute() {abcStock.buy();}
}class SellStock implements Order {private Stock abcStock;public SellStock(Stock abcStock) {this.abcStock = abcStock;}public void execute() {abcStock.sell();}
}class Broker {private List<Order> orderList = new ArrayList<Order>();public void takeOrder(Order order) {orderList.add(order);}public void placeOrders() {for (Order order : orderList) {order.execute();}orderList.clear();}
}public class CommandPatternDemo {public static void main(String[] args) {Stock abcStock = new Stock();BuyStock buyStockOrder = new BuyStock(abcStock);SellStock sellStockOrder = new SellStock(abcStock);Broker broker = new Broker();broker.takeOrder(buyStockOrder);broker.takeOrder(sellStockOrder);broker.placeOrders();}
}
15. 解释器模式 (Interpreter Pattern)

类比:如同编程语言的解释器,将源代码解释为机器可以理解的指令。

示例代码

interface Expression {boolean interpret(String context);
}class TerminalExpression implements Expression {private String data;public TerminalExpression(String data) {this.data = data;}public boolean interpret(String context) {if (context.contains(data)) {return true;}return false;}
}class OrExpression implements Expression {private Expression expr1;private Expression expr2;public OrExpression(Expression expr1, Expression expr2) {this.expr1 = expr1;this.expr2 = expr2;}public boolean interpret(String context) {return expr1.interpret(context) || expr2.interpret(context);}
}class AndExpression implements Expression {private Expression expr1;private Expression expr2;public AndExpression(Expression expr1, Expression expr2) {this.expr1 = expr1;this.expr2 = expr2;}public boolean interpret(String context) {return expr1.interpret(context) && expr2.interpret(context);}
}public class InterpreterPatternDemo {// 规则:Robert 和 John 是男性public static Expression getMaleExpression() {Expression robert = new TerminalExpression("Robert");Expression john = new TerminalExpression("John");return new OrExpression(robert, john);}// 规则:Julie 是一个已婚女性public static Expression getMarriedWomanExpression() {Expression julie = new TerminalExpression("Julie");Expression married = new TerminalExpression("Married");return new AndExpression(julie, married);}public static void main(String[] args) {Expression isMale = getMaleExpression();Expression isMarriedWoman = getMarriedWomanExpression();System.out.println("John is male? " + isMale.interpret("John"));System.out.println("Julie is a married woman? " + isMarriedWoman.interpret("Married Julie"));}
}
16. 迭代器模式 (Iterator Pattern)

类比:如同书架上的书,通过迭代器可以逐本取出书本。

示例代码

interface Iterator {boolean hasNext();Object next();
}interface Container {Iterator getIterator();
}class NameRepository implements Container {public String names[] = {"Robert", "John", "Julie", "Lora"};public Iterator getIterator() {return new NameIterator();}private class NameIterator implements Iterator {int index;public boolean hasNext() {if (index < names.length) {return true;}return false;}public Object next() {if (this.hasNext()) {return names[index++];}return null;}}
}public class IteratorPatternDemo {public static void main(String[] args) {NameRepository namesRepository = new NameRepository();for (Iterator iter = namesRepository.getIterator(); iter.hasNext();) {String name = (String) iter.next();System.out.println("Name : " + name);}}
}
17. 中介者模式 (Mediator Pattern)

类比:如同机场的塔台,塔台作为中介者协调各飞机的起降。

示例代码

class User {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}public User(String name) {this.name = name;}public void sendMessage(String message) {ChatRoom.showMessage(this, message);}
}class ChatRoom {public static void showMessage(User user, String message) {System.out.println(new Date().toString() + " [" + user.getName() + "] : " + message);}
}public class MediatorPatternDemo {public static void main(String[] args) {User robert = new User("Robert");User john = new User("John");robert.sendMessage("Hi! John!");john.sendMessage("Hello! Robert!");}
}
18. 备忘录模式 (Memento Pattern)

类比:如同游戏存档,可以在某个节点保存游戏状态并在需要时恢复。

示例代码

class Memento {private String state;public Memento(String state) {this.state = state;}public String getState() {return state;}
}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();}
}class CareTaker {private List<Memento> mementoList = new ArrayList<Memento>();public void add(Memento state) {mementoList.add(state);}public Memento get(int index) {return mementoList.get(index);}
}public class MementoPatternDemo {public static void main(String[] args) {Originator originator = new Originator();CareTaker careTaker = new CareTaker();originator.setState("State #1");originator.setState("State #2");careTaker.add(originator.saveStateToMemento());originator.setState("State #3");careTaker.add(originator.saveStateToMemento());originator.setState("State #4");System.out.println("Current State: " + originator.getState());originator.getStateFromMemento(careTaker.get(0));System.out.println("First saved State: " + originator.getState());originator.getStateFromMemento(careTaker.get(1));System.out.println("Second saved State: " + originator.getState());}
}

19. 观察者模式 (Observer Pattern)

类比:如同报纸订阅者,报社有新报纸出版时会通知订阅者。

示例代码

import java.util.ArrayList;
import java.util.List;class Subject {private List<Observer> observers = new ArrayList<Observer>();private int state;public int getState() {return state;}public void setState(int state) {this.state = state;notifyAllObservers();}public void attach(Observer observer) {observers.add(observer);}public void notifyAllObservers() {for (Observer observer : observers) {observer.update();}}
}abstract class Observer {protected Subject subject;public abstract void update();
}class BinaryObserver extends Observer {public BinaryObserver(Subject subject) {this.subject = subject;this.subject.attach(this);}public void update() {System.out.println("Binary String: " + Integer.toBinaryString(subject.getState()));}
}class OctalObserver extends Observer {public OctalObserver(Subject subject) {this.subject = subject;this.subject.attach(this);}public void update() {System.out.println("Octal String: " + Integer.toOctalString(subject.getState()));}
}class HexaObserver extends Observer {public HexaObserver(Subject subject) {this.subject = subject;this.subject.attach(this);}public void update() {System.out.println("Hex String: " + Integer.toHexString(subject.getState()).toUpperCase());}
}public class ObserverPatternDemo {public static void main(String[] args) {Subject subject = new Subject();new HexaObserver(subject);new OctalObserver(subject);new BinaryObserver(subject);System.out.println("First state change: 15");subject.setState(15);System.out.println("Second state change: 10");subject.setState(10);}
}
20. 状态模式 (State Pattern)

类比:如同电梯的状态,可以是上升、下降、静止,通过状态模式来管理状态转换。

示例代码

interface State {void doAction(Context context);
}class StartState implements State {public void doAction(Context context) {System.out.println("Player is in start state");context.setState(this);}public String toString() {return "Start State";}
}class StopState implements State {public void doAction(Context context) {System.out.println("Player is in stop state");context.setState(this);}public String toString() {return "Stop State";}
}class Context {private State state;public Context() {state = null;}public void setState(State state) {this.state = state;}public State getState() {return state;}
}public class StatePatternDemo {public static void main(String[] args) {Context context = new Context();StartState startState = new StartState();startState.doAction(context);System.out.println(context.getState().toString());StopState stopState = new StopState();stopState.doAction(context);System.out.println(context.getState().toString());}
}
21. 策略模式 (Strategy Pattern)

类比:如同商场的促销策略,商场可以根据不同的节日选择不同的促销方案。

示例代码

interface Strategy {int doOperation(int num1, int num2);
}class OperationAdd implements Strategy {public int doOperation(int num1, int num2) {return num1 + num2;}
}class OperationSubtract implements Strategy {public int doOperation(int num1, int num2) {return num1 - num2;}
}class OperationMultiply implements Strategy {public int doOperation(int num1, int num2) {return num1 * num2;}
}class Context {private Strategy strategy;public Context(Strategy strategy) {this.strategy = strategy;}public int executeStrategy(int num1, int num2) {return strategy.doOperation(num1, num2);}
}public class StrategyPatternDemo {public static void main(String[] args) {Context context = new Context(new OperationAdd());System.out.println("10 + 5 = " + context.executeStrategy(10, 5));context = new Context(new OperationSubtract());System.out.println("10 - 5 = " + context.executeStrategy(10, 5));context = new Context(new OperationMultiply());System.out.println("10 * 5 = " + context.executeStrategy(10, 5));}
}
22. 模板模式 (Template Pattern)

类比:如同烹饪菜谱,每道菜都有固定的流程,通过模板模式定义模板方法和步骤。

示例代码

abstract class Game {abstract void initialize();abstract void startPlay();abstract void endPlay();// 模板方法public final void play() {initialize();startPlay();endPlay();}
}class Cricket extends Game {void initialize() {System.out.println("Cricket Game Initialized! Start playing.");}void startPlay() {System.out.println("Cricket Game Started. Enjoy the game!");}void endPlay() {System.out.println("Cricket Game Finished!");}
}class Football extends Game {void initialize() {System.out.println("Football Game Initialized! Start playing.");}void startPlay() {System.out.println("Football Game Started. Enjoy the game!");}void endPlay() {System.out.println("Football Game Finished!");}
}public class TemplatePatternDemo {public static void main(String[] args) {Game game = new Cricket();game.play();System.out.println();game = new Football();game.play();}
}
23. 访问者模式 (Visitor Pattern)

类比:如同博物馆导览员,可以根据游客身份提供不同的讲解服务。

示例代码

interface ComputerPart {void accept(ComputerPartVisitor computerPartVisitor);
}class Keyboard implements ComputerPart {public void accept(ComputerPartVisitor computerPartVisitor) {computerPartVisitor.visit(this);}
}class Monitor implements ComputerPart {public void accept(ComputerPartVisitor computerPartVisitor) {computerPartVisitor.visit(this);}
}class Mouse implements ComputerPart {public void accept(ComputerPartVisitor computerPartVisitor) {computerPartVisitor.visit(this);}
}class Computer implements ComputerPart {ComputerPart[] parts;public Computer() {parts = new ComputerPart[]{new Mouse(), new Keyboard(), new Monitor()};}public void accept(ComputerPartVisitor computerPartVisitor) {for (int i = 0; i < parts.length; i++) {parts[i].accept(computerPartVisitor);}computerPartVisitor.visit(this);}
}interface ComputerPartVisitor {void visit(Computer computer);void visit(Mouse mouse);void visit(Keyboard keyboard);void visit(Monitor monitor);
}class ComputerPartDisplayVisitor implements ComputerPartVisitor {public void visit(Computer computer) {System.out.println("Displaying Computer.");}public void visit(Mouse mouse) {System.out.println("Displaying Mouse.");}public void visit(Keyboard keyboard) {System.out.println("Displaying Keyboard.");}public void visit(Monitor monitor) {System.out.println("Displaying Monitor.");}
}public class VisitorPatternDemo {public static void main(String[] args) {ComputerPart computer = new Computer();computer.accept(new ComputerPartDisplayVisitor());}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/22974.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

通信的本质是什么

通信的本质是信息的传递和交换。在通信过程中&#xff0c;信息从一个主体&#xff08;发送方&#xff09;传递到另一个主体&#xff08;接收方&#xff09;&#xff0c;目的是使接收方理解或使用发送方传递的信息。无论使用什么样的媒介或技术&#xff0c;通信的核心都是在不同…

十三、resultMap解析

分为两部分&#xff1a;解析和使用 解析 1.解析XML的时候单独解析所有的resultMap标签&#xff0c;封装成ResultMap对象存入configuration中 2.解析XML中的SQL语句&#xff0c;封装MappedStatement对象&#xff0c;这里会根据SQL的返回类型是resultMap还是resultType做处理。如…

C语言 | Leetcode C语言题解之第133题克隆图

题目&#xff1a; 题解&#xff1a; struct Node** visited; int* state; //数组存放结点状态 0&#xff1a;结点未创建 1&#xff1a;仅创建结点 2&#xff1a;结点已创建并已填入所有内容void bfs(struct Node* s) {if (visited[s->val] && state[s->val] 2…

【嵌入式系统实践】实验三EXTI按钮外部中断控制LED灯参考代码

此内容不属于实验内容&#xff0c;因自己手头有一STM32F103&#xff0c;故验证性的进行代码实验&#xff0c;按照老师课堂ppt进行了一下复现。 通过按钮控制LED灯的亮灭(状态取反)。 main.c代码&#xff1a; #include "STM32F10X.h" #include "stdio.h"…

Open3D Guided滤波(Python版本)

文章目录 一、简介二、实现代码三、实现效果参考资料一、简介 Guided Filter原本主要用于2D图像的降噪等处理,但经过适当的修改后,它可以有效地应用于3D点云的降噪。这种方法能够保留点云中的细节信息,并且对边缘和曲面进行保护。 其具体计算过程如下所述: 1.局部线性假设:…

Python Lambda函数的应用实例教程

在Python编程中&#xff0c;lambda函数是一种简洁且强大的工具&#xff0c;用于创建小型匿名函数。它们在需要快速定义简单函数时特别有用。本文将详细介绍lambda函数的语法及其多种应用实例&#xff0c;帮助读者更好地理解和使用lambda函数。 一、lambda函数的基本概念 1.1 什…

c++(内存分配,构造,析构)

#include <iostream>using namespace std; class Per { private:string name;int age;double *height;double *weigh; public://无参构造Per(){cout << "Per::无参构造" << endl;}//有参构造Per(string name,int age,double height,double weigh):…

Nginx 的 stream 模块,配置转发redis和mysql

Nginx 的 stream 模块确实可以配置多个 upstream 块&#xff0c;用于定义多个后端服务器组。然而&#xff0c;需要注意的是&#xff0c;每个 upstream 块通常用于一种特定类型的服务&#xff0c;例如定义一组TCP服务器&#xff0c;可以是Redis服务器、MySQL服务器或其他任何TCP…

【TB作品】 51单片机8x8点阵显示滚动汉字仿真

功能 题目5基于51单片机LED8x8点阵显示 流水灯 直接滚动显示HELLO 直接滚动显示老师好 代码 void main( void ) {/** 移位后&#xff0c;右边的是第一个595&#xff0c;接收0X02&#xff0c;显示出0X02* 移位后&#xff0c;左边的是第2个595&#xff0c;接收0Xfe&#xff0c…

创建常规DLL的动态链接库

本文仅供学习交流&#xff0c;严禁用于商业用途&#xff0c;如本文涉及侵权请及时联系本人将于及时删除 【例9.3】创建一个MFC 常规DLL的动态链接库Areadll&#xff0c;在该动态链接库中添加一个导出类CArea&#xff0c;通过该类获取正方形和圆的面积。 (1) 使用“MFC动态链接…

HttpClient Overview(翻译)

HttpClient Overview **原文链接&#xff1a;HttpClient Overview The Hyper-Text Transfer Protocol(HTTP) is perhaps the most significant protocol used on the Internet today.Web services,network-enabled appliances and the growth on of network computing contin…

Allegro器件角度倾斜如何回正?

Allegro器件角度倾斜,坐标含有小数点调整为45度整数倍的方法 Allegro器件角度倾斜回正的方法。 在用Allero进行PCB设计过程中,有时候由于误操作;或者刚开始器件需要非45度整数倍的角度,后又需要调整为整数倍的角度。器件角度倾斜含有小数点调整为45度整数倍的方法。 1、如…

Python Word变量:深入探索与实际应用

Python Word变量&#xff1a;深入探索与实际应用 在Python编程中&#xff0c;处理文本数据是一项至关重要的任务。而Word变量&#xff0c;作为存储和操作文本数据的核心元素&#xff0c;其使用和技巧对于提升编程效率和准确性具有不可忽视的作用。本文将从四个方面、五个方面、…

Arduino网页服务器:如何将Arduino开发板用作Web服务器

大家好&#xff0c;我是咕噜铁蛋&#xff01;今天&#xff0c;我将和大家分享一个有趣且实用的项目——如何使用Arduino开发板搭建一个简易的网页服务器。通过这个项目&#xff0c;你可以将Arduino连接到互联网&#xff0c;并通过网页控制或查询Arduino的状态。 一、项目背景与…

vue实现pdf下载——html2canvas

html2canvas 官方文档https://html2canvas.hertzen.com/getting-started html2canvas 的原理是通过遍历DOM树,将每一个HTML元素转化为Canvas对象,并叠加到一起形成一张完整的图片或者PDF文件。 1. 安装插件 npm install html2canvas jspdf --save 2.使用&#xff08;页面已经…

Stable Diffusion:多领域应用的创新引擎

一、引言 在当今数字化时代&#xff0c;人工智能技术的飞速发展为各个领域带来了前所未有的机遇和挑战。Stable Diffusion 作为一种先进的随机过程模型&#xff0c;以其独特的优势和广泛的应用潜力&#xff0c;成为了人工智能领域的研究热点。本文将深入探讨 Stable Diffusion…

git 的基本操作 Master and branch的版本合并 @ VS 1019

前言&#xff1a; 在VS 2019有git 的可视化管理,但&#xff0c;感觉微软其实就是在git上包了一层。版本冲突后&#xff0c;还是要靠git 的命令行代码搞。本文记录了一次&#xff0c;branch和master的版本合并的过程。作为&#xff0c;后续的参考。 【注意&#xff0c;这个是一…

【二进制部署k8s-1.29.4】十三、metrics-server的安装部署

文章目录 简介 一.metrics-server的安装 简介 本章节主要讲解metrics-server的安装&#xff0c;metrics-server主要是用于采集k8s中节点和pod的内存和cpu指标&#xff0c;在观察几点和pod的实时资源使用情况还是比较有用的&#xff0c;如果需要记录历史信息&#xff0c;建议采用…

运行编译openjdk12-33

编译环境 ubuntu20 Ubuntu里用户可以自行选择安装GCC或CLang来进行编译&#xff0c;但必须确保最低的版本为GCC 4.8或者CLang 3.2以上&#xff0c;官方推荐使用GCC 7.8或者CLang 9.1来完成编译。 源码 https://github.com/openjdk/jdk/tree/jdk-12%2B33 安装gcc sudo apt…

人工智能的未来发展前景:机遇与挑战

人工智能&#xff08;AI&#xff09;的发展在过去的几十年里取得了突飞猛进的成就&#xff0c;已经成为推动全球科技创新的关键动力之一。随着技术的不断进步和应用的日益广泛&#xff0c;AI的未来发展前景显得更加广阔&#xff0c;同时也面临一系列新的机遇和挑战。 技术革新…