返回

以超广角镜头剖析设计模式中的创建型模式

后端

设计模式是程序员必备的技能,其中创建型设计模式尤为重要。创建型设计模式提供一种创建对象的方式方法,可以帮助我们更方便的创建对象,构建复杂对象,以及复制对象。本文将为您详细介绍这些模式,帮助您成为一名更好的程序员。

一、创建型设计模式解读

创建型设计模式是一种提供创建对象的方式方法的设计模式。创建型设计模式包括单例模式、工厂模式、建造者模式和原型模式。

  • 单例模式:单例模式是一种确保某个类只有一个实例的方法。
  • 工厂模式:工厂模式可以使我们更方便的创建对象。
  • 建造者模式:建造者模式可以帮助我们构建复杂对象。
  • 原型模式:原型模式可以方便地复制对象。

二、单例模式,保证类的唯一实例

单例模式是一种确保某个类只有一个实例的方法。单例模式通常用于创建全局对象,例如数据库连接池、日志记录器等。

单例模式的实现方法有很多种,最常见的方法是使用静态变量。静态变量在类的整个生命周期内都存在,因此我们可以使用静态变量来存储类的实例。

public class Singleton {
    private static Singleton instance = null;

    private Singleton() {
        // 私有构造函数,防止外界直接创建实例
    }

    public static Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

三、工厂模式,创造优雅对象

工厂模式是一种可以使我们更方便的创建对象的设计模式。工厂模式将创建对象的代码封装在工厂类中,这样我们就可以通过调用工厂类的工厂方法来创建对象,而无需关心对象的具体创建过程。

工厂模式有两种常见类型:简单工厂模式和抽象工厂模式。

  • 简单工厂模式:简单工厂模式只提供一种工厂类,该工厂类可以创建多种类型的对象。
  • 抽象工厂模式:抽象工厂模式提供多种工厂类,每种工厂类可以创建一种类型的对象。
// 简单工厂模式
public class SimpleFactory {
    public static Product createProduct(String type) {
        if ("A".equals(type)) {
            return new ProductA();
        } else if ("B".equals(type)) {
            return new ProductB();
        } else {
            return null;
        }
    }
}

// 抽象工厂模式
public interface Factory {
    public Product createProduct();
}

public class ConcreteFactoryA implements Factory {
    @Override
    public Product createProduct() {
        return new ProductA();
    }
}

public class ConcreteFactoryB implements Factory {
    @Override
    public Product createProduct() {
        return new ProductB();
    }
}

四、建造者模式,畅享复杂构建

建造者模式是一种可以帮助我们构建复杂对象的设计模式。建造者模式将对象的构建过程封装在一个独立的类中,这样我们就可以通过调用建造者类的建造方法来构建对象,而无需关心对象的具体构建过程。

建造者模式通常用于构建复杂的对象,例如汽车、房屋等。

public class Builder {
    private Product product;

    public Builder() {
        product = new Product();
    }

    public Builder buildPartA() {
        product.addPart("A");
        return this;
    }

    public Builder buildPartB() {
        product.addPart("B");
        return this;
    }

    public Builder buildPartC() {
        product.addPart("C");
        return this;
    }

    public Product build() {
        return product;
    }
}

public class Director {
    public Product construct(Builder builder) {
        builder.buildPartA();
        builder.buildPartB();
        builder.buildPartC();
        return builder.build();
    }
}

public class Client {
    public static void main(String[] args) {
        Director director = new Director();
        Product product = director.construct(new Builder());
        System.out.println(product);
    }
}

五、原型模式,巧妙地克隆对象

原型模式是一种可以方便地复制对象的设计模式。原型模式将对象的状态存储在一个原型对象中,然后通过克隆原型对象来创建新的对象。

原型模式通常用于创建相同类型的多个对象,例如学生对象、商品对象等。

public class Prototype implements Cloneable {
    private String name;
    private int age;

    public Prototype(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public Prototype clone() {
        try {
            return (Prototype) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

public class Client {
    public static void main(String[] args) {
        Prototype prototype = new Prototype("张三", 20);
        Prototype clone = prototype.clone();
        System.out.println(prototype);
        System.out.println(clone);
    }
}