设计模式之工厂模式

  1. 工厂模式的概念
    工厂模式的作用是实例化对象,用工厂的方式来代替new操作创建对象的方式,工厂模式用来产生相似的对象
  2. 应用
    定义一个接口来创建对象,但是让子类来决定哪些类需要被实例化。工厂方法把实例化的工作推迟到子类中去实现
  3. 适用场景(以下会有例子进行说明)
    ①一组类似的对象需要创建
    ②在编码时不能预见需要创建那种类的实例
    ③系统需要考虑扩展性,不应该依赖产品实例产生的一些细节
  4. 工厂模式应用举例
    创建一个生产衣服的接口
package com.fgy.factory;

public interface ClothesInterface {

    //生产衣服,衣服会有很多类型,男装,女装,运动装,休闲装等等
    public void productClothes();
}

实现类

package com.fgy.factory;
public class ManClothes implements ClothesInterface {
    @Override
    public void productClothes() {
        System.out.println("***************生产男装**************");
    }

}
package com.fgy.factory;
public class WomanClothes implements ClothesInterface {

    @Override
    public void productClothes() {
        System.out.println("***************生产女装**************");
    }

}

测试类:

package com.fgy.factory;
public class FactoryTest {
    public static void main(String[] args) {
        ClothesInterface manClothes = new ManClothes();
        ClothesInterface womanClothes = new WomanClothes();
        manClothes.productClothes();
        womanClothes.productClothes();
    }

}

结果
这里写图片描述
但是这样写代码,不利于扩展和维护,所以写成如下的,先建立一个ClothesFactory类,来对衣服的种类进行统一管理,这样就不需要客户端来显示的创建衣服了,直接交给它就可以了

package com.fgy.factory;

public class ClothesFactory {

    public static ClothesInterface getClothes(String key) {
        if ("man".equals(key)) {
            return new ManClothes();
        }else if ("woman".equals(key)) {
            return new WomanClothes();
        }
        return null;
    }
}

这种方式有一个缺点,每多一种类型的衣服,就要加一个else if,很麻烦,不能智能的根据衣服的种类来自动的创建,用类的反射原理进行改进代码

package com.fgy.factory;

public class ClothesFactory {

    public static ClothesInterface getClothesByClassName(String className) {
        try {
            ClothesInterface clothes = (ClothesInterface) Class.forName(className).newInstance();
            return clothes;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

工厂方法模式完事
抽象工厂模式应用举例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
工厂模式是一种常见的创建型设计模式,用于创建对象,而不是通过直接调用构造函数来创建它们。工厂模式定义了一个接口,用于创建相关对象,但是让子类决定要实例化的类。在C++中,工厂模式可以通过以下步骤实现: 1. 创建一个抽象基类,该类定义了一个纯虚拟函数,该函数将返回一个指向基类的指针。这个基类就是我们的工厂接口。 ```c++ class Product { public: virtual ~Product() {} virtual void operation() = 0; }; ``` 2. 创建具体的产品类,它们继承自抽象基类,并实现了其纯虚拟函数。这些类就是我们的具体产品。 ```c++ class ConcreteProductA : public Product { public: void operation() override { /* 具体产品 A 的操作 */ } }; class ConcreteProductB : public Product { public: void operation() override { /* 具体产品 B 的操作 */ } }; ``` 3. 创建一个工厂类,该类实现了工厂接口,并根据需要创建具体的产品。这个工厂类就是我们的具体工厂。 ```c++ class Factory { public: virtual ~Factory() {} virtual std::unique_ptr<Product> createProduct() = 0; }; class ConcreteFactoryA : public Factory { public: std::unique_ptr<Product> createProduct() override { return std::make_unique<ConcreteProductA>(); } }; class ConcreteFactoryB : public Factory { public: std::unique_ptr<Product> createProduct() override { return std::make_unique<ConcreteProductB>(); } }; ``` 4. 在客户端代码中使用具体工厂创建具体产品。 ```c++ int main() { std::unique_ptr<Factory> factory = std::make_unique<ConcreteFactoryA>(); std::unique_ptr<Product> product = factory->createProduct(); product->operation(); return 0; } ``` 这就是工厂模式的基本实现方式。通过这种方式,我们可以将对象的创建过程与客户端代码分离,从而更好地实现模块化和可扩展性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值