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

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: 掌握Java设计模式的23种武器(全):深入解析与实战示例

一、创建型模式

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);
    }
 
    @Override
    public 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());
    }
}


相关文章
|
15天前
|
监控 Java 应用服务中间件
高级java面试---spring.factories文件的解析源码API机制
【11月更文挑战第20天】Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它通过自动配置、起步依赖和内嵌服务器等特性,极大地简化了Spring应用的开发和部署过程。本文将深入探讨Spring Boot的背景历史、业务场景、功能点以及底层原理,并通过Java代码手写模拟Spring Boot的启动过程,特别是spring.factories文件的解析源码API机制。
45 2
|
19天前
|
Java
轻松上手Java字节码编辑:IDEA插件VisualClassBytes全方位解析
本插件VisualClassBytes可修改class字节码,包括class信息、字段信息、内部类,常量池和方法等。
70 6
|
6天前
|
数据采集 存储 Web App开发
Java爬虫:深入解析商品详情的利器
在数字化时代,信息处理能力成为企业竞争的关键。本文探讨如何利用Java编写高效、准确的商品详情爬虫,涵盖爬虫技术概述、Java爬虫优势、开发步骤、法律法规遵守及数据处理分析等内容,助力电商领域市场趋势把握与决策支持。
|
10天前
|
存储 安全 Java
Java多线程编程中的并发容器:深入解析与实战应用####
在本文中,我们将探讨Java多线程编程中的一个核心话题——并发容器。不同于传统单一线程环境下的数据结构,并发容器专为多线程场景设计,确保数据访问的线程安全性和高效性。我们将从基础概念出发,逐步深入到`java.util.concurrent`包下的核心并发容器实现,如`ConcurrentHashMap`、`CopyOnWriteArrayList`以及`BlockingQueue`等,通过实例代码演示其使用方法,并分析它们背后的设计原理与适用场景。无论你是Java并发编程的初学者还是希望深化理解的开发者,本文都将为你提供有价值的见解与实践指导。 --- ####
|
16天前
|
存储 算法 Java
Java Set深度解析:为何它能成为“无重复”的代名词?
Java的集合框架中,Set接口以其“无重复”特性著称。本文解析了Set的实现原理,包括HashSet和TreeSet的不同数据结构和算法,以及如何通过示例代码实现最佳实践。选择合适的Set实现类和正确实现自定义对象的hashCode()和equals()方法是关键。
25 4
|
18天前
|
设计模式 JavaScript Java
Java设计模式:建造者模式详解
建造者模式是一种创建型设计模式,通过将复杂对象的构建过程与表示分离,使得相同的构建过程可以创建不同的表示。本文详细介绍了建造者模式的原理、背景、应用场景及实际Demo,帮助读者更好地理解和应用这一模式。
|
12天前
|
Java 开发者
Java多线程编程中的常见误区与最佳实践####
本文深入剖析了Java多线程编程中开发者常遇到的几个典型误区,如对`start()`与`run()`方法的混淆使用、忽视线程安全问题、错误处理未同步的共享变量等,并针对这些问题提出了具体的解决方案和最佳实践。通过实例代码对比,直观展示了正确与错误的实现方式,旨在帮助读者构建更加健壮、高效的多线程应用程序。 ####
|
3天前
|
缓存 Java 开发者
Java多线程编程的陷阱与最佳实践####
本文深入探讨了Java多线程编程中常见的陷阱,如竞态条件、死锁和内存一致性错误,并提供了实用的避免策略。通过分析典型错误案例,本文旨在帮助开发者更好地理解和掌握多线程环境下的编程技巧,从而提升并发程序的稳定性和性能。 ####
|
3天前
|
安全 Java 开发者
Java中的多线程编程:从基础到实践
本文深入探讨了Java多线程编程的核心概念和实践技巧,旨在帮助读者理解多线程的工作原理,掌握线程的创建、管理和同步机制。通过具体示例和最佳实践,本文展示了如何在Java应用中有效地利用多线程技术,提高程序性能和响应速度。
24 1
|
11天前
|
安全 Java 开发者
Java 多线程并发控制:深入理解与实战应用
《Java多线程并发控制:深入理解与实战应用》一书详细解析了Java多线程编程的核心概念、并发控制技术及其实战技巧,适合Java开发者深入学习和实践参考。