简单工厂模式
一个工厂,按照菜单,创建多个实例
class PayMent
{
public:
virtual void Pay() = 0;
virtual ~PayMent() {}
};
class AliPay :public PayMent
{
public:
void Pay()
{
std::cout << "AliPay" << std::endl;
}
~AliPay() {}
};
class WeChatPay :public PayMent
{
public:
void Pay()
{
std::cout << "WeChatPay" << std::endl;
}
~WeChatPay() {}
};
class Factory
{
public:
static std::unique_ptr<PayMent> CreatPayMent(const std::string name)
{
if ("AliPay" == name)
{
return std::unique_ptr<PayMent>(new AliPay());
}
else if ("WeChatPay" == name)
{
return std::unique_ptr<PayMent>(new WeChatPay());
}
}
};
int main()
{
std::unique_ptr<PayMent> pm = Factory::CreatPayMent("AliPay");
pm->Pay();
std::unique_ptr<PayMent> pm2 = Factory::CreatPayMent("WeChatPay");
pm2->Pay();
return 0;
}
优点:扩充的时候很简单,只需要添加class 和if就可以了
缺点:
1.包含了很多if else 代码块,整个代码相当冗长,代码越长,阅读维护,测试的难度越大,而且大量的if else还会影响系统的性能。
的条件还影响系统的性能。
2.CreatePayMent方法负责构建所有的PayMent,违反了单一职责原则,
对上面的稍做改进
class PayMent
{
public:
virtual void Pay() = 0;
virtual ~PayMent() {}
};
class AliPay :public PayMent
{
public:
void Pay()
{
std::cout << "AliPay" << std::endl;
}
~AliPay() {}
};
class WeChatPay :public PayMent
{
public:
void Pay()
{
std::cout << "WeChatPay" << std::endl;
}
~WeChatPay() {}
};
class Factory
{
public:
Factory()
{
std::string arr[] = { "AliPay","WeChatPay" };
for (auto str : arr)
{
payMent_.emplace(str, CreatePayMent(str));
}
}
static std::shared_ptr<PayMent> CreatePayMent(const std::string name)
{
if ("AliPay" == name)
{
return std::shared_ptr<PayMent>(new AliPay());
}
else if("WeChatPay" == name)
{
return std::shared_ptr<PayMent>(new WeChatPay());
}
}
static std::shared_ptr<PayMent> GetPayMent(const std::string& name)
{
return payMent_[name];
}
private:
static std::map<std::string, std::shared_ptr<PayMent>> payMent_;
};
std::map<std::string, std::shared_ptr<PayMent>> Factory:: payMent_;
int main()
{
Factory factory;
std::shared_ptr<PayMent> pm = factory.GetPayMent("AliPay");
pm->Pay();
std::shared_ptr<PayMent> pm2 = factory.GetPayMent("WeChatPay");
pm2->Pay();
return 0;
}
把实例存放在map中,使用的使用一获取,可以复用这些对象,从而避免了使用的时候再创建,使用完之后又销毁,减少了对象的创建。
工厂方法模式
多个工厂,按照菜单,创建多个实例。
class PayMent
{
public:
virtual void Pay() = 0;
virtual ~PayMent(){}
};
class AliPay :public PayMent
{
public:
void Pay()
{
std::cout << "AliPay" << std::endl;
}
~AliPay() {}
};
class WeChatPay :public PayMent
{
public:
void Pay()
{
std::cout << "WeChatPay" << std::endl;
}
~WeChatPay() {}
};
class Factory
{
public:
virtual std::unique_ptr<PayMent> GetPayMent() = 0;
};
class AliPayFactory : public Factory
{
public:
std::unique_ptr<PayMent> GetPayMent()
{
return std::unique_ptr<PayMent>(new AliPay());
}
};
class WeChatFactory : public Factory
{
public:
std::unique_ptr<PayMent> GetPayMent()
{
return std::unique_ptr<PayMent>(new WeChatPay());
}
};
int main()
{
AliPayFactory aliPayFactory;
WeChatFactory weChartFactory;
std::unique_ptr<PayMent> pm = aliPayFactory.GetPayMent();
std::unique_ptr<PayMent> pm2 = weChartFactory.GetPayMent();
pm->Pay();
pm2->Pay();
return 0;
}
优点:相对于简单工厂,扩展性好,只需要添加实例类和对应的工厂。
抽象工厂模式
按照工厂菜单,创建专用工厂。
再用专用工厂,按照产品菜单,创建实例
class PayMent
{
public:
virtual void Pay() = 0;
virtual ~PayMent() {}
};
class AliPay :public PayMent
{
public:
void Pay()
{
std::cout << "AliPay" << std::endl;
}
~AliPay() {}
};
class WeChatPay :public PayMent
{
public:
void Pay()
{
std::cout << "WeChatPay" << std::endl;
}
~WeChatPay() {}
};
class AbstractFactory
{
public:
virtual std::unique_ptr<PayMent> GetPayMent() = 0;
};
class AliPayMentFactory : public AbstractFactory
{
public:
std::unique_ptr<PayMent> GetPayMent()
{
return std::unique_ptr<PayMent>(new AliPay());
}
};
class WeChatMentFactory : public AbstractFactory
{
public:
std::unique_ptr<PayMent> GetPayMent()
{
return std::unique_ptr<PayMent>(new WeChatPay());
}
};
class FactoryProducer
{
public:
std::unique_ptr<AbstractFactory> GetFactory(const std::string & name)
{
if ("AliPayMentFactory" == name)
{
return std::unique_ptr<AbstractFactory>(new AliPayMentFactory());
}
else if ("WeChatMentFactory" == name)
{
return std::unique_ptr<AbstractFactory>(new WeChatMentFactory());
}
}
};
int main()
{
FactoryProducer factoryProducer;
std::unique_ptr<AbstractFactory> af = factoryProducer.GetFactory("AliPayMentFactory");
std::unique_ptr<AbstractFactory> af2 = factoryProducer.GetFactory("WeChatMentFactory");
std::unique_ptr<PayMent> pm = af->GetPayMent();
std::unique_ptr<PayMent> pm2 = af2->GetPayMent();
pm->Pay();
pm2->Pay();
return 0;
}
优点:面向一类产品创建工厂,这一类产品具有统一的属性。