设计模式-工厂模式

1、简单工厂模式

package com.jdwa.factory.simple;

public interface IProduct {
    void use();
}

package com.jdwa.factory.simple;

public class Phone implements IProduct {
    @Override
    public void use() {
        System.out.println("通话");
    }
}

package com.jdwa.factory.simple;

public class Fridge implements IProduct {
    @Override
    public void use() {
        System.out.println("保鲜食物");
    }
}
package com.jdwa.factory.simple;

public class ProductFactory {
    public IProduct create(Class<? extends IProduct> clazz){
        if (clazz != null) {
            try {
                return clazz.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

package com.jdwa.factory.simple;

public class Client {
    public static void main(String[] args) {
        ProductFactory factory = new ProductFactory();
        IProduct fridge = factory.create(Fridge.class);
        fridge.use();
    }
}

2、工厂方法模式

package com.jdwa.factory.method;

public interface IProduct {
    void use();
}

package com.jdwa.factory.method;

public class Fridge implements IProduct {
    @Override
    public void use() {
        System.out.println("工厂方法-保鲜食物");
    }
}

package com.jdwa.factory.method;

public class Phone implements IProduct {
    @Override
    public void use() {
        System.out.println("工厂方法-通话");
    }
}

package com.jdwa.factory.method;

public interface IProductFactory {
    IProduct create();
}

package com.jdwa.factory.method;

public class FridgeFactory implements IProductFactory {
    @Override
    public IProduct create() {
        return new Fridge();
    }
}

package com.jdwa.factory.method;

public class PhoneFactory implements IProductFactory {
    @Override
    public IProduct create() {
        return new Phone();
    }
}

package com.jdwa.factory.method;

public class Client {
    public static void main(String[] args) {
        IProductFactory factory = new PhoneFactory();
        IProduct ph = factory.create();
        ph.use();

        factory = new FridgeFactory();
        IProduct fridge = factory.create();
        fridge.use();

    }
}

3、抽象工厂模式

package com.jdwa.factory.abstractfactory;

public interface IComputer {
    void code();
}

package com.jdwa.factory.abstractfactory;

public interface IPhone {
    void call();
}

package com.jdwa.factory.abstractfactory;

public class AppleComputer implements IComputer{
    @Override
    public void code() {
        System.out.println("use apple computer to code ==");
    }
}

package com.jdwa.factory.abstractfactory;

public class HuaWeiComputer implements IComputer {
    @Override
    public void code() {
        System.out.println("use huawei computer to code===");
    }
}

package com.jdwa.factory.abstractfactory;

public class ApplePhone implements IPhone {
    @Override
    public void call() {
        System.out.println("use apple phone to call someone ===");
    }
}

package com.jdwa.factory.abstractfactory;

public class HuaWeiPhone implements IPhone{
    @Override
    public void call() {
        System.out.println("use huawei  phone to call someone ===");
    }
}

package com.jdwa.factory.abstractfactory;

public abstract class ProductFactory {
    public void init(){
        System.out.println("init============");
    }

    protected abstract IComputer createCom();

    protected abstract IPhone createPh();

}

package com.jdwa.factory.abstractfactory;

public class AppleFactory extends ProductFactory {
    @Override
    protected IComputer createCom() {
        return new AppleComputer();
    }

    @Override
    protected IPhone createPh() {
        return new ApplePhone();
    }
}

package com.jdwa.factory.abstractfactory;

public class HuaWeiFactory extends ProductFactory {
    @Override
    protected IComputer createCom() {
        return new HuaWeiComputer();
    }

    @Override
    protected IPhone createPh() {
        return new HuaWeiPhone();
    }
}

package com.jdwa.factory.abstractfactory;

public class Client {
    public static void main(String[] args) {
        HuaWeiFactory huaWeiFactory = new HuaWeiFactory();
        huaWeiFactory.createCom().code();
        huaWeiFactory.createPh().call();

        AppleFactory appleFactory = new AppleFactory();
        appleFactory.createCom().code();
        appleFactory.createPh().call();
    }
}

欢迎大家留言,以便于后面的人更快解决问题!另外亦欢迎大家可以关注我的微信公众号,方便利用零碎时间互相交流。共勉!

设计模式-工厂模式

上一篇:spring boot导入Swagger


下一篇:eclipse安装go插件