简单工厂模式

抽象产品角色 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();
    }
}

标签: 设计模式

添加新评论