简单工厂模式

抽象产品角色 Apple
具体产品角色 IPhone、Mac
工厂类 AppleSimpleFactory

public interface Apple {
    void createProduct();
}
public class IPhone implements Apple {
    @Override
    public void createProduct() {
        System.out.println("IPhone mobile");
    }
}
public class Mac implements Apple {
    @Override
    public void createProduct() {
        System.out.println("Mac Computer");
    }
}
public class AppleSimpleFactory {

    Apple createProductFactory(String name){
        if("mac".equals(name)){
            return new Mac();
        }else if("iphone".equals(name)){
            return new IPhone();
        }else
            return null;
    }
}
public class Test {
    public static void main(String[] args) {
        AppleSimpleFactory appleSimpleFactory = new AppleSimpleFactory();
        Apple apple = appleSimpleFactory.createProductFactory("mac");
        System.out.println(apple);
    }
}

工厂方法模式

工厂方法模式也叫工厂模式、虚拟构造器模式、多态工厂模式,工厂方法模式中一个工厂生产一个产品

抽象产品角色 Apple
具体产品角色 Mac 、IPhone
抽象工厂角色 AppleFactory
具体工厂角色 MacFactory 、IPhoneFactory

public interface Apple {
        void createProduct();
}
public class Mac implements Apple {
    @Override
    public void createProduct() {
        System.out.println("Mac Computer");
    }
}
public class IPhone implements Apple {
    @Override
    public void createProduct() {
        System.out.println("IPhone mobile");
    }
}
public interface AppleFactory {
    Apple createProduct();
}
public class IPhoneFactory implements AppleFactory {
    @Override
    public Apple createProduct() {
        return new IPhone();
    }
}
public class MacFactory implements AppleFactory {

    @Override
    public Apple createProduct() {
        return new Mac();
    }
}
public class Test {
    public static void main(String[] args) {
        AppleFactory macFactory = new MacFactory();
        Apple mac = macFactory.createProduct();
        System.out.println(mac);
        AppleFactory iphoneFactory = new IPhoneFactory();
        Apple iphone = iphoneFactory.createProduct();
        System.out.println(iphone);
    }
}

抽象工厂模式

抽象产品角色 Apple
具体产品角色 Mac、IPhone、IPad
抽象工厂角色 AppleAbstractFactory
具体工厂角色 YanTaiFctory、TaiwanFactory

public interface Apple {
        void createProduct();
}
public class Mac implements Apple {
    @Override
    public void createProduct() {
        System.out.println("Mac Computer");
    }
}
public class IPhone implements Apple {
    @Override
    public void createProduct() {
        System.out.println("IPhone mobile");
    }
}
public class IPad implements Apple {
    @Override
    public void createProduct() {
        System.out.println("iPad ");
    }
}
public interface AppleAbstractFactory {

    void createMac();

    void createIPhone();

    void createIPad();
}
public class YanTaiFctory implements AppleAbstractFactory {
    @Override
    public void createMac() {
        System.out.println("MacBook Pro");
    }

    @Override
    public void createIPhone() {
        System.out.println("iPhone X");
    }

    @Override
    public void createIPad() {
        System.out.println("iPad mini4");
    }
}
public class TaiwanFactory implements AppleAbstractFactory {

    @Override
    public void createMac() {
        System.out.println("MacBook Air");
    }

    @Override
    public void createIPhone() {
        System.out.println("iPhone 8s");
    }

    @Override
    public void createIPad() {
        System.out.println("iPad mini2");
    }
}
public class Test {
    public static void main(String[] args) {
        AppleAbstractFactory appleAbstractFactory = new TaiwanFactory();
        appleAbstractFactory.createIPad();
        appleAbstractFactory.createIPhone();
        appleAbstractFactory.createMac();
    }
}

2018-08-17 增加线程安全的懒汉模式

单例模式即只有一个实例,实现分为懒汉模式和饿汉模式。

饿汉模式

/**
 * 单例模式_饿汉模式
 * 类加载的时候完成初始化,因此加载速度相对较慢,但运行是获取对象的速度相对较快
 * 线程安全
 * Created by zhangguoli on 09-18-018.
 * YXV0aG9yOiB6aGFuZ2d1b2xpLmNu
 */
public class HungrySingleton {

    //① 构造方法私有化,外部不可以直接创建对象
    private HungrySingleton () {

    }

    //② 创建类的唯一实例,使用private static修饰
    private static HungrySingleton instance = new HungrySingleton ();

    //③ 提供统一对外获取实例的方法
    public static HungrySingleton getInstance() {
        return instance;
    }
}

懒汉模式

- 阅读剩余部分 -