侧边栏壁纸
  • 累计撰写 29 篇文章
  • 累计创建 38 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Java 23种设计模式超全详解(二)

16uni
2025-06-11 / 0 评论 / 0 点赞 / 23 阅读 / 0 字 / 正在检测是否收录...
温馨提示:
部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

结构型模式(管"搭积木"的)

6. 适配器模式(Adapter)

问题:接口不兼容怎么办?
生活案例:Type-C转3.5mm耳机转换器
核心:包装旧对象,实现新接口

// 旧接口:圆孔耳机
class CircleJack {
    void playSound() { System.out.println("播放声音"); }
}

// 新接口:Type-C
interface USBC {
    void transmitData();
}

// 适配器
class JackAdapter implements USBC {
    private CircleJack oldJack;
    
    public JackAdapter(CircleJack jack) {
        this.oldJack = jack;
    }
    
    public void transmitData() {
        oldJack.playSound(); // 调用旧方法
    }
}

7. 桥接模式(Bridge)

问题:如何避免类爆炸?
生活案例:不同颜色+不同形状的组合(红圆/蓝方)
核心:分离抽象和实现

interface Color {
    void applyColor();
}

abstract class Shape {
    protected Color color; // 桥接点
    public Shape(Color color) { this.color = color; }
    abstract void draw();
}

class Circle extends Shape {
    public Circle(Color color) { super(color); }
    void draw() {
        System.out.print("圆形:");
        color.applyColor(); // 调用颜色实现
    }
}

8. 组合模式(Composite)

问题:如何统一处理整体和部分?
生活案例:文件夹包含文件/子文件夹
核心:树形结构 + 统一接口

interface FileSystem {
    void display();
}

class File implements FileSystem {
    public void display() { System.out.println("显示文件"); }
}

class Folder implements FileSystem {
    private List<FileSystem> items = new ArrayList<>();
    
    public void add(FileSystem item) { items.add(item); }
    
    public void display() {
        for (FileSystem item : items) {
            item.display(); // 递归调用
        }
    }
}

9. 装饰器模式(Decorator)

问题:如何动态添加功能?
生活案例:给咖啡加糖/加奶
核心:包装对象 + 实现相同接口

interface Coffee {
    double getCost();
}

class BasicCoffee implements Coffee {
    public double getCost() { return 10; }
}

class SugarDecorator implements Coffee {
    private Coffee coffee;
    public SugarDecorator(Coffee coffee) { this.coffee = coffee; }
    public double getCost() { return coffee.getCost() + 2; }
}
// 使用:
Coffee coffee = new SugarDecorator(new BasicCoffee());
System.out.println(coffee.getCost()); // 12

10. 外观模式(Facade)

问题:如何简化复杂系统?
生活案例:一键观影模式(关灯/开投影/降幕布)
核心:统一入口封装子系统

class Projector { void on() { /*...*/ } }
class Lights { void dim() { /*...*/ } }

class HomeTheaterFacade {
    private Projector projector;
    private Lights lights;
    
    public void watchMovie() {
        lights.dim();
        projector.on();
    }
}
// 使用:
new HomeTheaterFacade().watchMovie();

11. 享元模式(Flyweight)

问题:如何节省内存?
生活案例:棋盘重复使用棋子对象
核心:共享细粒度对象

class Tree {
    // 内部状态(可共享)
    private String type; 
    
    // 外部状态(不可共享)
    void display(int x, int y) {
        System.out.println(type + "树在("+x+","+y+")");
    }
}

class Forest {
    private static Map<String, Tree> trees = new HashMap<>();
    
    public Tree getTree(String type) {
        Tree tree = trees.get(type);
        if (tree == null) {
            tree = new Tree(type);
            trees.put(type, tree);
        }
        return tree;
    }
}

12. 代理模式(Proxy)

问题:如何控制对象访问?
生活案例:房产中介代理房东
核心:代替真实对象执行操作

interface House {
    void sell();
}

class Owner implements House {
    public void sell() { System.out.println("房东卖房"); }
}

class Agent implements House {
    private Owner owner;
    public void sell() {
        if (owner == null) owner = new Owner();
        preProcess();
        owner.sell();
        postProcess();
    }
    private void preProcess() { /* 筛选客户 */ }
}

0

评论区