设计模式(二):工厂模式

本文介绍了工厂模式的三种类型:简单工厂模式、工厂方法模式和抽象工厂模式。简单工厂模式通过一个公共接口创建对象,但增加新产品时需修改工厂类。工厂方法模式为每种产品提供独立工厂,降低了耦合。抽象工厂模式则适用于生产多种类型产品的情况。虽然工厂模式存在扩展性问题,但在实际应用中仍广泛使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

工厂模式提供了一种创建对象的最佳方式,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。工厂模式分为简单工厂模式、工厂方法模式和抽象工厂模式,c++工厂模式主要利用了多态的特性,有关多态的内容可以参考c++多态

简单工厂模式

简单工厂模式包括三个组件:

  • 工厂类:用于创建一个指定具体实例对象。
  • 具体产品类:工厂模式创建的对象。
  • 抽象产品类:具体产品继承的父类或实现的接口。

可以通过一个实例来了解简单工厂模式,假设我们需要通过工厂模式生产衣服,工厂类定义为clothesFactor,具体产品定义为adidasClothes和nikeClothes,抽象产品类定义为clothes。
为了方便展示,在抽象产品类中定义print()接口:

//clothes abstract
class clothes{
public:
	virtual void print() = 0;     //纯虚函数,需要具体产品实现
	virtual ~clothes() {};    //虚析构函数避免内存泄漏
}

具体产品类继承抽象产品类,实现响应接口:

//all kinds of clothes class
class adidasClothes : public clothes{
public:
	void print(){
		std::cout << "it is adidas clothes" << std::endl;
	}
}

class nikeClothes : public clothes{
public:
	void print(){
		std::cout << "it is nike clothes" << std::endl;
	}
}

最后是工厂类的实现,工厂类只需要生产出具体产品即可:

//clothes factory
enum clothes_type{
	adidas,
	nike
};

class clothesFactory{
public:
	clothes* createClothes(clothes_type type){
		switch (type){
        case nike:
            return new nikeClothes();
            break;
        case adidas:
            return new adidasClothes();
            break;
        default:
            return nullptr;
            break;
        }
	}
}

最后贴出完整代码,并通过int main()函数中的代码测试创建简单工厂模式:

#include <iostream>
//衣服类型
enum clothes_type{
	adidas,
	nike
};
//抽象产品
class clothes{
public:
	virtual void print() = 0;     //纯虚函数,需要具体产品实现
	virtual ~clothes() {};    //虚析构函数避免内存泄漏
};
//具体产品adidas
class adidasClothes : public clothes{
public:
	void print(){
		std::cout << "it is adidas clothes" << std::endl;
	}
};
//具体产品nike
class nikeClothes : public clothes{
public:
	void print(){
		std::cout << "it is nike clothes" << std::endl;
	}
};
//工厂
class clothesFactory{
public:
	clothes* createClothes(clothes_type type){
		switch (type){
        case nike:
            return new nikeClothes();
            break;
        case adidas:
            return new adidasClothes();
            break;
        default:
            return nullptr;
            break;
        }
	}
};


int main()
{
    // 构造工厂
    clothesFactory clothes_factory;
    //从工厂构造耐克衣服
    clothes *nike_clothes = clothes_factory.createClothes(nike);
    if (nike_clothes){
        nike_clothes->print();

        delete nike_clothes;
        nike_clothes = nullptr;
    }

    //从工厂构造阿迪达斯衣服
    clothes *adidas_clothes = clothes_factory.createClothes(adidas);
    if (adidas_clothes){
        adidas_clothes->print();

        delete adidas_clothes;
        adidas_clothes = nullptr;
    }
    return 0;
}

执行结果如下:
在这里插入图片描述
简单工厂模式的局限性很大,如果需要增加一种新的具体产品类型,必须修改工厂类,修改之前的代码是需要尽量避免的,可能导致新的bug出现,并且简单工厂模式只有一个工厂,如果每种产品都需要快速生产,这里会成为程序的瓶颈。

工厂方法模式

工厂方法模式为每种产品都配备了一个工厂,使得每种产品都可以快速大量生产,工厂方法模式的组成如下:

  • 抽象工厂类:创建提供具体产品的接口。
  • 具体工厂类:继承抽象工厂类,实现每种具体产品的生产。
  • 具体产品类:工厂模式创建的对象。
  • 抽象产品类:具体产品继承的父类或实现的接口。

工厂方法模式的具体产品和抽象产品与简单工厂方法类似,这里不再解释,而抽象工厂和具体工厂代码可能是下面这样:

// 总工厂
class clothesFactory
{
public:
    virtual clothes *createClothes() = 0;
    virtual ~clothesFactory() {}
};

// 耐克生产工厂
class NiKeProducer : public clothesFactory
{
public:
    clothes *createClothes(){
        return new nikeClothes();
    }
};

// 阿迪达斯生产工厂
class AdidasProducer : public clothesFactory
{
public:
    clothes *createClothes(){
        return new adidasClothes();
    }
};

完整代码如下:

#include <iostream>

class clothes{
public:
	virtual void print() = 0;     //纯虚函数,需要具体产品实现
	virtual ~clothes() {};    //虚析构函数避免内存泄漏
};

class adidasClothes : public clothes{
public:
	void print(){
		std::cout << "it is adidas clothes" << std::endl;
	}
};

class nikeClothes : public clothes{
public:
	void print(){
		std::cout << "it is nike clothes" << std::endl;
	}
};

// 总工厂
class clothesFactory
{
public:
    virtual clothes *createClothes() = 0;
    virtual ~clothesFactory() {}
};

// 耐克生产工厂
class niKeProducer : public clothesFactory
{
public:
    clothes *createClothes(){
        return new nikeClothes();
    }
};

// 阿迪达斯生产工厂
class adidasProducer : public clothesFactory
{
public:
    clothes *createClothes(){
        return new adidasClothes();
    }
};


int main()
{
    // 构造工厂
    clothesFactory *nike_clothes_factory = new niKeProducer();
    clothesFactory *adidas_clothes_factory = new adidasProducer();

    //从工厂构造耐克衣服
    clothes *nike_clothes = nike_clothes_factory->createClothes();
    if (nike_clothes){
        nike_clothes->print();

        delete nike_clothes;
        nike_clothes = nullptr;
    }

    //从工厂构造阿迪达斯衣服
    clothes *adidas_clothes = adidas_clothes_factory->createClothes();
    if (adidas_clothes){
        adidas_clothes->print();

        delete adidas_clothes;
        adidas_clothes = nullptr;
    }

    return 0;
}

运行结果如下:
在这里插入图片描述

抽象工厂模式

抽象工厂模式解决的场景是工厂模式需要生产多种类型的产品,例如不仅生成衣服,还需要生产裤子、鞋子等产品。抽象工厂模式的组成与工厂方法模式一样:

  • 抽象工厂类:创建提供具体产品的接口。
  • 具体工厂类:继承抽象工厂类,实现每种具体产品的生产。
  • 具体产品类:工厂模式创建的对象。
  • 抽象产品类:具体产品继承的父类或实现的接口。

抽象工厂模式由于需要生产多种产品,显然抽象工厂类中需要更多的接口,并在具体工厂类中实现:

// 总工厂
class factory
{
public:
    virtual clothes *createClothes() = 0;
    virtual pants *createPants() = 0;
    virtual ~factory() {}
};

// 耐克生产工厂
class niKeProducer : public factory
{
public:
    clothes *createClothes(){
        return new nikeClothes();
    }
    pants *createPants(){
        return new nikePants();
    }
};

// 阿迪达斯生产工厂
class adidasProducer : public factory
{
public:
    clothes *createClothes(){
        return new adidasClothes();
    }
    pants *createPants(){
        return new adidasPants();
    }
};

相应地需要增加pants的抽象产品和具体产品:

class pants{
public:
	virtual void print() = 0;     //纯虚函数,需要具体产品实现
	virtual ~pants() {};    //虚析构函数避免内存泄漏
};

class adidasPants : public pants{
public:
	void print(){
		std::cout << "it is adidas pants" << std::endl;
	}
};

class nikePants : public pants{
public:
	void print(){
		std::cout << "it is nike pants" << std::endl;
	}
};

完整代码如下:

#include <iostream>

class clothes{
public:
	virtual void print() = 0;     //纯虚函数,需要具体产品实现
	virtual ~clothes() {};    //虚析构函数避免内存泄漏
};

class adidasClothes : public clothes{
public:
	void print(){
		std::cout << "it is adidas clothes" << std::endl;
	}
};

class nikeClothes : public clothes{
public:
	void print(){
		std::cout << "it is nike clothes" << std::endl;
	}
};

class pants{
public:
	virtual void print() = 0;     //纯虚函数,需要具体产品实现
	virtual ~pants() {};    //虚析构函数避免内存泄漏
};

class adidasPants : public pants{
public:
	void print(){
		std::cout << "it is adidas pants" << std::endl;
	}
};

class nikePants : public pants{
public:
	void print(){
		std::cout << "it is nike pants" << std::endl;
	}
};

// 总工厂
class factory
{
public:
    virtual clothes *createClothes() = 0;
    virtual pants *createPants() = 0;
    virtual ~factory() {}
};

// 耐克生产工厂
class niKeProducer : public factory
{
public:
    clothes *createClothes(){
        return new nikeClothes();
    }
    pants *createPants(){
        return new nikePants();
    }
};

// 阿迪达斯生产工厂
class adidasProducer : public factory
{
public:
    clothes *createClothes(){
        return new adidasClothes();
    }
    pants *createPants(){
        return new adidasPants();
    }
};



int main()
{
    // 构造工厂
    factory *nike_producer = new niKeProducer();
    factory *adidas_producer = new adidasProducer();

    //从工厂构造耐克衣服
    clothes *nike_clothes = nike_producer->createClothes();
    if (nike_clothes){
        nike_clothes->print();

        delete nike_clothes;
        nike_clothes = nullptr;
    }

    //从工厂构造耐克裤子
    pants *nike_pants = nike_producer->createPants();
    if (nike_pants){
        nike_pants->print();

        delete nike_pants;
        nike_pants = nullptr;
    }

    //从工厂构造阿迪达斯衣服
    clothes *adidas_clothes = adidas_producer->createClothes();
    if (adidas_clothes){
        adidas_clothes->print();

        delete adidas_clothes;
        adidas_clothes = nullptr;
    }

    //从工厂构造阿迪达斯裤子
    pants *adidas_pants = adidas_producer->createPants();
    if (adidas_pants){
        adidas_pants->print();

        delete adidas_pants;
        adidas_pants = nullptr;
    }

    return 0;
}

总结

工厂模式的优点是封装了生产产品的细节,但也始终没有解决一个问题:每次新增产品,都需要修改之前的代码,但这也不影响工厂模式在c++代码中的广泛应用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值