【设计模式之禅】工厂方法模式

news/2024/5/19 5:20:34 标签: 抽象类, 设计模式, java, 反射

工厂方法模式

Factory Method Pattern

定义:

Define an interface for creating an object, but let subclass decide which class to instantiate.

Factory Method lets a class defer instantiation to subclass.

定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法让一个类实例化延迟到其子类

就是说,工厂方法模式通过其子类,实现创建对象接口,通过类名来实现实例化类。

通用代码:

java">// Product 抽象类
public abstract class AbstractProduct {
    // 产品类的公共方法
    public void publicMethod() {
        ...
    }
    // 抽象方法
    public abstract void abstractMethod ();
}

// Product 具体类
public class ConcreteProduct1 extends AbstractProduct {
    public void abstractMethod () {
        ...
    }
}
public class ConcreteProduct2 extends AbstractProduct {
    public void abstractMethod () {
        ...
    }
}

//Factory 抽象类
public abstract class AbstractFactory {
    //创建一个产品对象;参数类型通常为String、Enum、Class;限制其下限为Product
    public abstract <T extends Product> T createProduct (Class<T> c);
}

//Factory 具体类
public class ConcreteFactory extends AbstractFactory {
    public <T extends Product> T createProduct (Class<T> c) {
        Product product = null;
        try {
            product = (Product) Class.forName(c.getName()).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (T)product;
    }
}

//使用
AbstractFactory factory = new AbstractFactory();
Product product = factory.createProduct(ConcreteProduct1.class);
product.abstractMethod();

优点:

  • 封装性好,代码结构清晰;
  • 拓展性好,需要新产品时,只要增加多一个Product的具体类就好。
  • 符合迪米特法则(高层模块不但需要关心产品的实现类)、依赖倒置原则、里氏替换原则(产品的的抽象类能代替实现类出现)。

工厂方法模式的变种:

  • 缩小为简单工厂模式:减少掉抽象工厂类,并使用静态方法来实现类的实例化。
  • 升级为多个工厂类:实现多个具体工厂类来创建对应的产品。
  • 代替单例模式:通过反射的方式创建单例对象。
  • 延迟初始化:通过Map缓存创建的产品类,并用synchronize来保证线程安全。

http://www.niftyadmin.cn/n/1358031.html

相关文章

使用postman测试接口需要登录

使用浏览器进行登录操作&#xff0c;将Cookie值复制下来&#xff0c;粘贴在postman的Header中&#xff0c;key为Cookie&#xff0c;value为复制下来的内容 https://blog.csdn.net/pengbin790000/article/details/79386500

【Dubbo】Spring Boot整合Dubbo

Spring Boot整合Dubbo 1.环境依赖 Dubbo是一种嵌入式的去中心化的架构&#xff0c;其引入的依赖&#xff1a; <dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>2.7.8</…

SpringBoot+IDEA热部署

https://blog.csdn.net/My_Chen_Suo_Zhang/article/details/69396808 每次修改都会直接去编译&#xff0c;对电脑损耗挺大的&#xff0c;还是自己改完自己重启比较好

【Dubbo】在开发联调的时候需要做什么?

Dubbo RPC 开发联调方案 一 临时分组联调 在开发provider的服务接口的DubboService添加group属性; 最好这个group是从配置文件中读取的. DubboService(group "${server.member.group}")在需要调用的client的DubboReference同样的添加group属性; DubboReference(i…

IDEA新建Maven模块module显示为灰色

ctrlshiftalta进入Project Structure 选中modules 点击加号 点击 Import Module 选中变灰模块的pom.xml 一路ok下去

CentOS7查看和关闭防火墙

查看防火墙状态 firewall-cmd --state关闭防火墙 systemctl stop firewalld禁止开机启动 systemctl disable firewalld.service

sql为查询为null的值设置默认值

sql对查询为null的值赋默认值 后台开发中经常需要给前端提供接口&#xff0c;返回的字段为null的时候需要设置字段的默认值。针对不同的数据库实现方式有&#xff1a; sqlserver&#xff1a; Sql代码 select isnull(字段,0) from 表名 --这样就是把空值赋值为0 MySQL&#…

【设计模式之禅】模板方法模式

模板方法模式 Template Method Pattern 定义&#xff1a; Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method subclasses redefine certain steps of an algorithm without changing the algorithm’s structure. 定…