C++ 工厂方法模式使用继承而抽象工厂模式使用组合如何?
我正在讨论抽象工厂模式和工厂方法模式之间的区别。 我知道工厂方法只用于创建一个产品,而抽象工厂则用于创建相关或从属产品的系列。 但我不清楚工厂方法模式如何使用继承而抽象工厂模式如何使用组合 我知道这已经被问了好几次了,有人能用下面的代码解释一下继承和组合是如何发生的吗 工厂方法代码C++ 工厂方法模式使用继承而抽象工厂模式使用组合如何?,c++,design-patterns,factory-pattern,abstract-factory,C++,Design Patterns,Factory Pattern,Abstract Factory,我正在讨论抽象工厂模式和工厂方法模式之间的区别。 我知道工厂方法只用于创建一个产品,而抽象工厂则用于创建相关或从属产品的系列。 但我不清楚工厂方法模式如何使用继承而抽象工厂模式如何使用组合 我知道这已经被问了好几次了,有人能用下面的代码解释一下继承和组合是如何发生的吗 工厂方法代码 class IRose { public: virtual string Color(void)=0; }; class RedRose: public IRose { public: string Color(v
class IRose
{
public:
virtual string Color(void)=0;
};
class RedRose: public IRose
{
public:
string Color(void)
{
return "Red";
}
};
class YellowRose: public IRose
{
public:
string Color(void)
{
return "Yellow";
}
};
class IFactory
{
public:
virtual IRose* Create(string type)=0;
//The factory create method in 90% of cases will take a parameter which
//determines what kind of the object the factory will return.
};
class Factory: public IFactory
{
public:
IRose* Create(string type)
{
if ("Red" == type)
return new RedRose();
if ("Yellow" == type)
return new YellowRose();
return NULL;
}
};
int main()
{
IRose* p = NULL;
IFactory* f = NULL;
f = new Factory(); //You have to create an INSTANCE of the factory
p = f->Create("Red");
cout<<"\nColor is: "<<p->Color()<<"\n";
delete p;
p = f->Create("Yellow");
cout<<"\nColor is: "<<p->Color()<<"\n";
delete p;
return 1;
}
class-IRose
{
公众:
虚拟字符串颜色(void)=0;
};
红玫瑰类:公共IRose
{
公众:
字符串颜色(空)
{
返回“红色”;
}
};
黄玫瑰类:公共IRose
{
公众:
字符串颜色(空)
{
返回“黄色”;
}
};
类工厂
{
公众:
虚拟IRose*创建(字符串类型)=0;
//在90%的情况下,factory create方法将采用以下参数:
//确定工厂将返回的对象类型。
};
类别工厂:公共工厂
{
公众:
IRose*创建(字符串类型)
{
如果(“红色”==类型)
返回新的红玫瑰();
如果(“黄色”==类型)
返回新的黄玫瑰();
返回NULL;
}
};
int main()
{
IRose*p=NULL;
IFactory*f=NULL;
f=new Factory();//必须创建工厂的实例
p=f->创建(“红色”);
这是经过深思熟虑后修改的答复
工厂方法:通常,createObject()是Creator对象的方法
抽象工厂:工厂对象通常是创建者对象的属性
现在假设createObject()和Factory对象属于各自的创建者
工厂方法:createObject()可能在创建者的子类中发生更改。这是通过通过继承更改createObject()的实现来实现的
抽象工厂:在创建者的子类中,工厂对象也可能发生更改。但是,这种更改是通过将一个工厂对象替换为另一个工厂对象来实现的,即创建者对象通过合成来更改
在您的演示中,creatObject()和Factory对象是创建者的外部对象,因此混淆了合成/继承之间的区别
克里斯托弗·奥克拉维在youTube上有很多关于模式的视频
工厂法
抽象工厂
这里要求的是一个工厂方法的版本(在C++中,这次!)
如果你需要帮助,请告诉我
class IRose
{
public:
virtual const char * Color(void) = 0;
};
class RedRose : public IRose
{
public:
const char * Color(void)
{
return "I am a Red rose";
}
};
class YellowRose : public IRose
{
public:
const char * Color(void)
{
return "I am a Yellow rose";
}
};
class RoseGarden
{
protected: class IRose* rose; // a pointer to the garden's rose
public:
virtual void createRose() { } // abstract Factory Method
public: void sayColor() {
cout << rose->Color() << '\n';
}
};
class RedRoseGarden : public RoseGarden
{
public:
void createRose()
{
this->rose = new RedRose(); // concrete factory method
}
};
class YellowRoseGarden : public RoseGarden
{
public:
void createRose()
{
this->rose = new YellowRose(); // concrete factory method
}
};
int main()
{
RoseGarden * garden = NULL;
garden = new YellowRoseGarden;
garden->createRose(); // correct factory method is chosen via inheritance
garden->sayColor();
delete garden;
garden = new RedRoseGarden;
garden->createRose(); // correct factory method is chosen via inheritance
garden->sayColor();
delete garden;
return 1;
}
class-IRose
{
公众:
虚拟常量字符*颜色(void)=0;
};
红玫瑰类:公共IRose
{
公众:
常量字符*颜色(空)
{
返回“我是一朵红玫瑰”;
}
};
黄玫瑰类:公共IRose
{
公众:
常量字符*颜色(空)
{
返回“我是一朵黄玫瑰”;
}
};
玫瑰园
{
protected:class IRose*rose;//指向花园玫瑰的指针
公众:
虚拟void createRose(){}//抽象工厂方法
公众:void sayColor(){
cout Color()rose=new RedRose();//具体工厂方法
}
};
黄色玫瑰园类:公共玫瑰园
{
公众:
void createRose()
{
this->rose=newyellowRose();//具体工厂方法
}
};
int main()
{
玫瑰园*garden=NULL;
花园=新的黄玫瑰园;
garden->createRose();//通过继承选择正确的工厂方法
花园->sayColor();
删除花园;
花园=新的红玫瑰园;
garden->createRose();//通过继承选择正确的工厂方法
花园->sayColor();
删除花园;
返回1;
}
这里还有一个对抽象工厂稍加修改的版本,以更好地展示它的使用方式。只添加了一个house对象。注意,我为我的编译器将所有“string”对象更改为const char*
// make different types of fridges
class IFridge
{
public:
virtual const char* Run(void) = 0;
};
class FridgeSamsung : public IFridge
{
public:
const char* Run(void)
{
return "This house has a Samsung Fridge\n";
}
};
class FridgeWhirlpool : public IFridge
{
public:
const char* Run(void)
{
return "This house has a Whirlpool Fridge\n";
}
};
// make different types of washing machine
class IWashingMachine
{
public:
virtual const char* Run(void) = 0;
};
class WashingMachineSamsung : public IWashingMachine
{
public:
const char* Run(void)
{
return "This house has a Samsung Washing Machine\n";
}
};
class WashingMachineWhirlpool : public IWashingMachine
{
public:
const char* Run(void)
{
return "This house has a Whirlpool Washing Machine\n";
}
};
// make different type of factory
class IFactory
{
public:
virtual IFridge* GetFridge(void) = 0;
virtual IWashingMachine* GetWashingMachine(void) = 0;
};
class FactorySamsung : public IFactory
{
IFridge* GetFridge(void)
{
return new FridgeSamsung();
}
IWashingMachine* GetWashingMachine(void)
{
return new WashingMachineSamsung();
}
};
class FactoryWhirlpool : public IFactory
{
IFridge* GetFridge(void)
{
return new FridgeWhirlpool();
}
IWashingMachine* GetWashingMachine(void)
{
return new WashingMachineWhirlpool();
}
};
// Make a house object that has a fridge and a washing machine
class House
{
private:
class IWashingMachine * washingMachine;
class IFridge * fridge;
public:
House(IFactory * houseFactory) {
washingMachine = houseFactory->GetWashingMachine();
fridge = houseFactory->GetFridge();
}
void showAppliances() {
cout << washingMachine->Run();
cout << fridge->Run();
}
};
int main()
{
class IFactory * factory;
class House * house;
// make a samsung house
factory = new FactorySamsung;
house = new House(factory); // passing the factory by injection
house->showAppliances(); // now we have a Samsung house
cout << '\n';
// clean up
delete house;
delete factory;
// make a whirlpool house
factory = new FactoryWhirlpool;
house = new House(factory); // passing the factory by injection
house->showAppliances(); // now we have a WHilepool house
cout << '\n';
// clean up
delete house;
delete factory;
return 1;
}
//制作不同类型的冰箱
IFridge类
{
公众:
虚拟常量字符*Run(void)=0;
};
班级冰箱三星:公共IFridge
{
公众:
常量字符*运行(无效)
{
return“此房屋有三星冰箱\n”;
}
};
类别冰箱惠而浦:公共IFridge
{
公众:
常量字符*运行(无效)
{
return“此房子有一台漩涡冰箱\n”;
}
};
//制造不同类型的洗衣机
I类洗衣机
{
公众:
虚拟常量字符*Run(void)=0;
};
洗衣机类别:公用洗衣机
{
公众:
常量字符*运行(无效)
{
return“此房子有一台三星洗衣机\n”;
}
};
类别洗衣机惠而浦:公用洗衣机
{
公众:
常量字符*运行(无效)
{
return“此房子有一台漩涡洗衣机\n”;
}
};
//制造不同类型的工厂
类工厂
{
公众:
虚拟IFridge*GetFridge(void)=0;
虚拟IWashingMachine*GetWashingMachine(void)=0;
};
类别工厂三星:公共IFactory
{
IFridge*GetFridge(无效)
{
归还新冰箱三星();
}
IWashingMachine*GetWashingMachine(无效)
{
退回新洗衣机Samsung();
}
};
类别工厂惠而浦:公共IFactory
{
IFridge*GetFridge(无效)
{
归还新冰箱惠而浦();
}
IWashingMachine*GetWashingMachine(无效)
{
退回新洗衣机惠而浦();
}
};
//制作一个有冰箱和洗衣机的家居用品
班房
{
私人:
I类洗衣机*洗衣机;
IFridge*类冰箱;
公众:
房屋(IFactory*房屋工厂){
洗衣机=家庭工厂->获取洗衣机();
冰箱=家庭工厂->获取冰箱();
}
void showAppliances(){
无法运行();
无法运行();
}
};
int main()
{
I类工厂*工厂;
班级宿舍*宿舍;
//打造三星之家
工厂=新工厂三星;
房子=新房子(工厂);
// make different types of fridges
class IFridge
{
public:
virtual const char* Run(void) = 0;
};
class FridgeSamsung : public IFridge
{
public:
const char* Run(void)
{
return "This house has a Samsung Fridge\n";
}
};
class FridgeWhirlpool : public IFridge
{
public:
const char* Run(void)
{
return "This house has a Whirlpool Fridge\n";
}
};
// make different types of washing machine
class IWashingMachine
{
public:
virtual const char* Run(void) = 0;
};
class WashingMachineSamsung : public IWashingMachine
{
public:
const char* Run(void)
{
return "This house has a Samsung Washing Machine\n";
}
};
class WashingMachineWhirlpool : public IWashingMachine
{
public:
const char* Run(void)
{
return "This house has a Whirlpool Washing Machine\n";
}
};
// make different type of factory
class IFactory
{
public:
virtual IFridge* GetFridge(void) = 0;
virtual IWashingMachine* GetWashingMachine(void) = 0;
};
class FactorySamsung : public IFactory
{
IFridge* GetFridge(void)
{
return new FridgeSamsung();
}
IWashingMachine* GetWashingMachine(void)
{
return new WashingMachineSamsung();
}
};
class FactoryWhirlpool : public IFactory
{
IFridge* GetFridge(void)
{
return new FridgeWhirlpool();
}
IWashingMachine* GetWashingMachine(void)
{
return new WashingMachineWhirlpool();
}
};
// Make a house object that has a fridge and a washing machine
class House
{
private:
class IWashingMachine * washingMachine;
class IFridge * fridge;
public:
House(IFactory * houseFactory) {
washingMachine = houseFactory->GetWashingMachine();
fridge = houseFactory->GetFridge();
}
void showAppliances() {
cout << washingMachine->Run();
cout << fridge->Run();
}
};
int main()
{
class IFactory * factory;
class House * house;
// make a samsung house
factory = new FactorySamsung;
house = new House(factory); // passing the factory by injection
house->showAppliances(); // now we have a Samsung house
cout << '\n';
// clean up
delete house;
delete factory;
// make a whirlpool house
factory = new FactoryWhirlpool;
house = new House(factory); // passing the factory by injection
house->showAppliances(); // now we have a WHilepool house
cout << '\n';
// clean up
delete house;
delete factory;
return 1;
}