创建型
单例
单例模式分为两种,一种为饿汉式,一种为懒汉式,
饿汉式采用直接实例化 Singleton 的方式,但这样如果在不使用的情况下会消耗资源
懒汉式采用的后面实例化,从而达到节约资源,当有多个线程访问进去的时候,为了保证获取到的都是同一个 Singleton.class,通过对其进行上锁,保证只有一个线程可以进去获取实例化,当实例化结束后,其他线程获取到的都是已经被实例化好的singleton
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class Singleton { private static final Singleton instance = new Singleton(); private Singleton(){}; public static Singleton getInstance(){ return instance; } }
class simple{ private static volatile simple instance; private simple(){}; public static simple getInstance(){ if(instance == null){ synchronized (simple.class) { if (instance == null){ instance= new simple(); } } } return instance; } }
|
工厂
简单工厂
1 2 3
| public interface Fruit { Integer cash(); }
|
1 2 3 4 5 6
| public class Apple implements Fruit{ @Override public Integer cash() { return 2; } }
|
1 2 3 4 5 6
| public class Banana implements Fruit{ @Override public Integer cash() { return 3; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public class Factory { public Fruit getFruit(int type) { if (type == 1) { return new Apple(); } else if (type == 2) { return new Banana(); } else { return null; } } public static void main(String[] args) { Factory factory = new Factory(); Fruit fruit = factory.getFruit(1); Fruit fruit2 = factory.getFruit(2); System.out.println(fruit.cash()); System.out.println(fruit2.cash()); } }
|
但这个简单工厂不符合太符合开闭原则的,因为工厂实际上是针对于调用方提供的,所以我们应该尽可能对修改关闭。
工厂方法
在简单工厂中,创建对象的是另一个类,而在工厂方法中,是由子类来创建对象。
定义一个创建对象的接口,但让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。

1 2 3 4
| public abstract class Factory { abstract public Fruit getFruit(); }
|
1 2 3 4 5 6 7
| public class AppleFactory extends Factory{ @Override public Fruit getFruit() { return new Apple(); } }
|
抽象工厂
提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。缺陷在于每添加一个新产品就要在抽象类进行修改,违反了开闭原则
1 2
| public class AbstractProductA { }
|
1 2
| public class AbstractProductB { }
|
1 2
| public class ProductA1 extends AbstractProductA { }
|
1 2
| public class ProductA2 extends AbstractProductA { }
|
1 2
| public class ProductB1 extends AbstractProductB { }
|
1 2
| public class ProductB2 extends AbstractProductB { }
|
1 2 3 4
| public abstract class AbstractFactory { abstract AbstractProductA createProductA(); abstract AbstractProductB createProductB(); }
|
1 2 3 4 5 6 7 8 9
| public class ConcreteFactory1 extends AbstractFactory { AbstractProductA createProductA() { return new ProductA1(); }
AbstractProductB createProductB() { return new ProductB1(); } }
|
1 2 3 4 5 6 7 8 9
| public class ConcreteFactory2 extends AbstractFactory { AbstractProductA createProductA() { return new ProductA2(); }
AbstractProductB createProductB() { return new ProductB2(); } }
|
1 2 3 4 5 6 7 8
| public class Client { public static void main(String[] args) { AbstractFactory abstractFactory = new ConcreteFactory1(); AbstractProductA productA = abstractFactory.createProductA(); AbstractProductB productB = abstractFactory.createProductB(); } }
|
Builder
封装一个对象的构造过程,并允许按步骤构造,单如果这个实体类的属性超过4个或以上的时候就推荐使用建造者模式创建对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| public class Person { String name; int age; String phone; int sex; private Person(String name, int age, String phone, int sex) { this.name = name; this.age = age; this.phone = phone; this.sex = sex; } public static Builder builder(){ return new Builder(); } public static class Builder{ String name; int age; String phone; int sex;
public Builder name(String name) { this.name = name; return this; } public Builder age(int age) { this.age = age; return this; } public Builder phone(String phone) { this.phone = phone; return this; } public Builder sex(int sex) { this.sex = sex; return this; }
public Person build() { return new Person(name, age, phone, sex); } } }
|
原型
原型模式使用原型实例指定待创建对象的类型,并且通过复制这个原型来创建新的对象。也就是说,原型对象作为模板,通过克隆操作,来产生更多的对象,就像细胞的复制一样。