0
点赞
收藏
分享

微信扫一扫

【设计模式】常见设计模式

陬者 03-20 08:00 阅读 1

原型模式

// 原型接口
interface Shape extends Cloneable {
    void draw();
    Shape clone();
}

// 具体原型类 - 圆形
class Circle implements Shape {
    private String color;

    public Circle(String color) {
        this.color = color;
    }

    @Override
    public void draw() {
        System.out.println("Drawing a circle with color: " + color);
    }

    @Override
    public Shape clone() {
        return new Circle(color);
    }
}

// 具体原型类 - 正方形
class Square implements Shape {
    private String color;

    public Square(String color) {
        this.color = color;
    }

    @Override
    public void draw() {
        System.out.println("Drawing a square with color: " + color);
    }

    @Override
    public Shape clone() {
        return new Square(color);
    }
}

// 原型管理器
class ShapeCache {
    private static Map<String, Shape> shapeMap = new HashMap<>();

    public static Shape getShape(String type) {
        Shape cachedShape = shapeMap.get(type);
        return (Shape) cachedShape.clone();
    }

    public static void loadCache() {
        Circle circle = new Circle("red");
        shapeMap.put("circle", circle);

        Square square = new Square("blue");
        shapeMap.put("square", square);
    }
}

// 示例使用
public class PrototypePatternExample {
    public static void main(String[] args) {
        ShapeCache.loadCache();

        Shape clonedShape1 = ShapeCache.getShape("circle");
        clonedShape1.draw();  // 输出: Drawing a circle with color: red

        Shape clonedShape2 = ShapeCache.getShape("square");
        clonedShape2.draw();  // 输出: Drawing a square with color: blue
    }
}

模板模式

代码举例

// 抽象模板类
abstract class Game {
    abstract void initialize();
    abstract void startPlay();
    abstract void endPlay();

    // 模板方法,定义算法框架
    public final void play() {
        initialize();
        startPlay();
        endPlay();
    }
}

// 具体模板类 - 足球游戏
class FootballGame extends Game {
    @Override
    void initialize() {
        System.out.println("Football Game Initialized! Start playing.");
    }

    @Override
    void startPlay() {
        System.out.println("Playing football...");
    }

    @Override
    void endPlay() {
        System.out.println("Football Game Finished!");
    }
}

// 具体模板类 - 篮球游戏
class BasketballGame extends Game {
    @Override
    void initialize() {
        System.out.println("Basketball Game Initialized! Start playing.");
    }

    @Override
    void startPlay() {
        System.out.println("Playing basketball...");
    }

    @Override
    void endPlay() {
        System.out.println("Basketball Game Finished!");
    }
}

// 示例使用
public class TemplatePatternExample {
    public static void main(String[] args) {
        Game footballGame = new FootballGame();
        footballGame.play();
        // 输出:
        // Football Game Initialized! Start playing.
        // Playing football...
        // Football Game Finished!

        System.out.println();

        Game basketballGame = new BasketballGame();
        basketballGame.play();
        // 输出:
        // Basketball Game Initialized! Start playing.
        // Playing basketball...
        // Basketball Game Finished!
    }
}

策略模式

代码举例

// 策略接口
interface Strategy {
    int doOperation(int num1, int num2);
}

// 具体策略类 - 加法
class AddStrategy implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        return num1 + num2;
    }
}

// 具体策略类 - 减法
class SubtractStrategy implements Strategy {
    @Override
    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 StrategyPatternExample {
    public static void main(String[] args) {
        Context context = new Context(new AddStrategy());
        int result1 = context.executeStrategy(10, 5);
        System.out.println("10 + 5 = " + result1);  // 输出: 10 + 5 = 15

        context = new Context(new SubtractStrategy());
        int result2 = context.executeStrategy(10, 5);
        System.out.println("10 - 5 = " + result2);  // 输出: 10 - 5 = 5
    }
}

委派模式

代码举例

// 委派接口
interface Printer {
    void print();
}

// 具体委派类 - 打印机A
class PrinterA implements Printer {
    @Override
    public void print() {
        System.out.println("Printer A is printing.");
    }
}

// 具体委派类 - 打印机B
class PrinterB implements Printer {
    @Override
    public void print() {
        System.out.println("Printer B is printing.");
    }
}

// 委派类
class PrinterManager {
    private Printer printer;

    public void setPrinter(Printer printer) {
        this.printer = printer;
    }

    public void print() {
        printer.print();
    }
}

// 示例使用
public class DelegatePatternExample {
    public static void main(String[] args) {
        PrinterManager printerManager = new PrinterManager();

        // 使用打印机A
        PrinterA printerA = new PrinterA();
        printerManager.setPrinter(printerA);
        printerManager.print();  // 输出: Printer A is printing.

        System.out.println();

        // 使用打印机B
        PrinterB printerB = new PrinterB();
        printerManager.setPrinter(printerB);
        printerManager.print();  // 输出: Printer B is printing.
    }
}

适配器模式

代码举例

// 目标接口
interface Target {
    void request();
}

// 适配者类
class Adaptee {
    public void specificRequest() {
        System.out.println("Specific request from Adaptee.");
    }
}

// 适配器类
class Adapter implements Target {
    private Adaptee adaptee;

    public Adapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public void request() {
        adaptee.specificRequest();
    }
}

// 示例使用
public class AdapterPatternExample {
    public static void main(String[] args) {
        Adaptee adaptee = new Adaptee();
        Target adapter = new Adapter(adaptee);

        adapter.request();  // 输出: Specific request from Adaptee.
    }
}

举报

相关推荐

0 条评论