Java中的设计模式有哪些类别?请举例说明。

Java 中的设计模式是指在软件开发中,为解决常见设计问题而形成的一系列被广泛应用的最佳实践。这些设计模式按照其目的和应用场景通常分为三大类:创建型模式结构型模式行为型模式。每一类模式都解决了特定类型的设计问题,以下是对这些类别及其常见模式的概述和示例说明。

1. 创建型模式(Creational Patterns)

创建型模式主要关注对象的创建过程,目的是将对象的创建与使用分离,以实现对象创建的灵活性和复用性。

  • 单例模式(Singleton Pattern): 确保一个类只有一个实例,并提供全局访问点。

    示例:

    public class Singleton {
        private static Singleton instance;
    
        private Singleton() {}
    
        public static Singleton getInstance() {
            if (instance == null) {
                instance = new Singleton();
            }
            return instance;
        }
    }
    
  • 工厂方法模式(Factory Method Pattern): 定义一个创建对象的接口,但由子类决定实例化哪个类。工厂方法将对象的创建延迟到子类。

    示例:

    interface Product {
        void use();
    }
    
    class ConcreteProductA implements Product {
        public void use() {
            System.out.println("Using Product A");
        }
    }
    
    class ConcreteProductB implements Product {
        public void use() {
            System.out.println("Using Product B");
        }
    }
    
    abstract class Creator {
        public abstract Product createProduct();
    }
    
    class ConcreteCreatorA extends Creator {
        public Product createProduct() {
            return new ConcreteProductA();
        }
    }
    
    class ConcreteCreatorB extends Creator {
        public Product createProduct() {
            return new ConcreteProductB();
        }
    }
    
  • 抽象工厂模式(Abstract Factory Pattern): 提供一个创建一系列相关或互相依赖对象的接口,而无需指定它们具体的类。

    示例:

    interface AbstractFactory {
        ProductA createProductA();
        ProductB createProductB();
    }
    
    class ConcreteFactory1 implements AbstractFactory {
        public ProductA createProductA() {
            return new ProductA1();
        }
        public ProductB createProductB() {
            return new ProductB1();
        }
    }
    
    class ConcreteFactory2 implements AbstractFactory {
        public ProductA createProductA() {
            return new ProductA2();
        }
        public ProductB createProductB() {
            return new ProductB2();
        }
    }
    
  • 原型模式(Prototype Pattern): 通过复制已有的实例来创建新对象,而不是通过实例化新的对象。适用于创建代价较高的对象。

    示例:

    public class Prototype implements Cloneable {
        public Prototype clone() throws CloneNotSupportedException {
            return (Prototype) super.clone();
        }
    }
    
  • 建造者模式(Builder Pattern): 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。

    示例:

    public class Product {
        private String partA;
        private String partB;
    
        public void setPartA(String partA) {
            this.partA = partA;
        }
    
        public void setPartB(String partB) {
            this.partB = partB;
        }
    }
    
    public class Builder {
        private Product product = new Product();
    
        public Builder buildPartA(String partA) {
            product.setPartA(partA);
            return this;
        }
    
        public Builder buildPartB(String partB) {
            product.setPartB(partB);
            return this;
        }
    
        public Product build() {
            return product;
        }
    }
    

2. 结构型模式(Structural Patterns)

结构型模式主要关注类和对象的组合,目的是让这些组合在一起能够更好地协同工作,通常通过继承和接口实现。

  • 适配器模式(Adapter Pattern): 将一个类的接口转换为客户端所期望的另一个接口,从而使原本接口不兼容的类能够一起工作。

    示例:

    interface Target {
        void request();
    }
    
    class Adaptee {
        public void specificRequest() {
            System.out.println("Specific request");
        }
    }
    
    class Adapter implements Target {
        private Adaptee adaptee;
    
        public Adapter(Adaptee adaptee) {
            this.adaptee = adaptee;
        }
    
        public void request() {
            adaptee.specificRequest();
        }
    }
    
  • 桥接模式(Bridge Pattern): 将抽象部分与它的实现部分分离,使它们都可以独立变化。

    示例:

    interface Implementor {
        void operationImpl();
    }
    
    class ConcreteImplementorA implements Implementor {
        public void operationImpl() {
            System.out.println("ConcreteImplementorA operation");
        }
    }
    
    class ConcreteImplementorB implements Implementor {
        public void operationImpl() {
            System.out.println("ConcreteImplementorB operation");
        }
    }
    
    abstract class Abstraction {
        protected Implementor implementor;
    
        protected Abstraction(Implementor implementor) {
            this.implementor = implementor;
        }
    
        public abstract void operation();
    }
    
    class RefinedAbstraction extends Abstraction {
        public RefinedAbstraction(Implementor implementor) {
            super(implementor);
        }
    
        public void operation() {
            implementor.operationImpl();
        }
    }
    
  • 组合模式(Composite Pattern): 使得用户对单个对象和组合对象的使用具有一致性,常用于树形结构的数据处理中。

    示例:

    interface Component {
        void operation();
    }
    
    class Leaf implements Component {
        public void operation() {
            System.out.println("Leaf operation");
        }
    }
    
    class Composite implements Component {
        private List<Component> children = new ArrayList<>();
    
        public void add(Component component) {
            children.add(component);
        }
    
        public void operation() {
            for (Component child : children) {
                child.operation();
            }
        }
    }
    
  • 装饰者模式(Decorator Pattern): 动态地为对象添加新的功能,而不影响其他同类对象的功能。

    示例:

    interface Component {
        void operation();
    }
    
    class ConcreteComponent implements Component {
        public void operation() {
            System.out.println("ConcreteComponent operation");
        }
    }
    
    class Decorator implements Component {
        protected Component component;
    
        public Decorator(Component component) {
            this.component = component;
        }
    
        public void operation() {
            component.operation();
        }
    }
    
    class ConcreteDecorator extends Decorator {
        public ConcreteDecorator(Component component) {
            super(component);
        }
    
        public void operation() {
            super.operation();
            addedBehavior();
        }
    
        private void addedBehavior() {
            System.out.println("ConcreteDecorator added behavior");
        }
    }
    
  • 外观模式(Facade Pattern): 为子系统中的一组接口提供一个一致的接口,使得子系统更容易使用。

    示例:

    class SubsystemA {
        public void operationA() {
            System.out.println("SubsystemA operation");
        }
    }
    
    class SubsystemB {
        public void operationB() {
            System.out.println("SubsystemB operation");
        }
    }
    
    class Facade {
        private SubsystemA subsystemA = new SubsystemA();
        private SubsystemB subsystemB = new SubsystemB();
    
        public void operation() {
            subsystemA.operationA();
            subsystemB.operationB();
        }
    }
    
  • 享元模式(Flyweight Pattern): 使用共享对象来最小化内存使用量。常用于创建大量相似对象的场景。

    示例:

    interface Flyweight {
        void operation(String extrinsicState);
    }
    
    class ConcreteFlyweight implements Flyweight {
        private String intrinsicState;
    
        public ConcreteFlyweight(String intrinsicState) {
            this.intrinsicState = intrinsicState;
        }
    
        public void operation(String extrinsicState) {
            System.out.println("Intrinsic: " + intrinsicState + ", Extrinsic: " + extrinsicState);
        }
    }
    
    class FlyweightFactory {
        private Map<String, Flyweight> flyweights = new HashMap<>();
    
        public Flyweight getFlyweight(String key) {
            if (!flyweights.containsKey(key)) {
                flyweights.put(key, new ConcreteFlyweight(key));
            }
            return flyweights.get(key);
        }
    }
    
  • 代理模式(Proxy Pattern): 为其他对象提供一种代理以控制对这个对象的访问。

    示例:

    interface Subject {
        void request();
    }
    
    class RealSubject implements Subject {
        public void request() {
            System.out.println("RealSubject request");
        }
    }
    
    class Proxy implements Subject {
        private RealSubject realSubject;
    
        public void request() {
            if (realSubject == null) {
                realSubject = new Real
    
  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值