读设计模式之禅--工厂模式

  • 简单工厂模式
/**
 * @Author: hyh
 * @Date: 2021/8/6 14:14
 **/
public abstract class Product {
    // 产品公共方法
    public void commonMethod(){
        System.out.println("产品是用来消费的");
    }
    abstract void name();
}
/**
 * @Author: hyh
 * @Date: 2021/8/6 10:34
 **/
public class Computer extends Product {
    @Override
    void name() {
        System.out.println("我是电脑");
    }
}
/**
 * @Author: hyh
 * @Date: 2021/8/6 10:37
 **/
public class Phone extends Product {
    @Override
    void name() {
        System.out.println("我是手机");
    }
}
/**
 * @Author: hyh
 * 工厂模式:定义一个用于创建对象的 接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟
 * 到其子类
 * 简单工厂模式
 * @Date: 2021/8/6 14:09
 **/
public class Factory {

    public <T extends Product> T createrProduct(Class<T> c) {
        Product p = null;
        try {
            p = (Product) Class.forName(c.getName()).newInstance();
        } catch (Exception e) {

        }
        return (T) p;
    }
}
// 我是流水线快乐的打工仔
class People{
    public static void main(String[] args) {
        // 由调用者决定生产那个产品
        Product computer = new Factory().createrProduct(Computer.class);
        computer.commonMethod();
        computer.name();

        Product phone = new Factory().createrProduct(Phone.class);
        phone.commonMethod();
        phone.name();
    }
}
输出:
产品是用来消费的
我是电脑
产品是用来消费的
我是手机
  • 单例工厂模式
/**
 * @Author: hyh
 * 单例产品
 * @Date: 2021/8/6 14:31
 **/
public class SingleProduct {
    private SingleProduct(){}
}
/**
 * @Author: hyh
 * 单例工厂模式:无参构造私有化的,可以通过反射来获取单例对象
 * @Date: 2021/8/6 14:09
 **/
public class SingleFactory {
    private static SingleProduct sp;
    static {
        try {
            // 反射获取class 对象
            Class c = Class.forName(sp.getClass().getName());
            // 获取无参构造
            Constructor constructor = c.getDeclaredConstructor();
            // 设置无参构造为可访问
            constructor.setAccessible(true);
            sp = (SingleProduct) constructor.newInstance();
        } catch (Exception e) {

        }
    }
    public static SingleProduct getInstance(){
        return sp;
    }

}
class SinglePeople {
    public static void main(String[] args) {
        SingleProduct instance = SingleFactory.getInstance();
    }
}
  • 多工厂模式
/**
 * @Author: hyh
 * 多工厂模式 只是将工厂细化了,每种工厂生产一种产品
 * 抽象方法中已经不再需要传递相关参数了,因为每一个具体的工厂都已经非常明 确自己的职责:创建自己
 * 负责的产品类对象
 * @Date: 2021/8/6 14:53
 **/
public abstract class AbstractFactory {
    public abstract Product createProduct();
}
/**
 * @Author: hyh
 * @Date: 2021/8/6 16:20
 **/
public class ComputerFactory extends AbstractFactory {
    @Override
    public Product createProduct() {
        return new Computer();
    }
}
/**
 * @Author: hyh
 * @Date: 2021/8/6 16:23
 **/
public class PhoneFactory extends AbstractFactory{

    @Override
    public Product createProduct() {
        return new Phone();
    }
}
  • 延迟初始化
/**
 * @Author: hyh
 * 延迟初始化: 一个对象被消费完毕后,并不立刻释放,工厂类 保持其初始状态,等待再次被使用
 * @Date: 2021/8/6 16:30
 **/
public class LazyProductFactory {
    // 对象缓存
    private static final Map<String, Product> map = new HashMap<>();
    public synchronized Product createProduct(String type) {
        Product p = null;
        //如果Map中已经有这个对象
        if (map.containsKey(type)) {
            p = map.get(type);
        } else {
            if (type.equals("Computer")) {
                p = new Computer();
            }
            if (type.equals("Phone")) {
                p = new Phone();
            }
            //同时把对象放到缓存容器中
            map.put(type,p);
        }
        return p;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值