工厂方法模式使用继承,而抽象工厂模式使用组合如何?
Factory method pattern uses inheritance while the abstract factory pattern uses composition how?
我正在研究抽象工厂模式与工厂方法模式之间的区别。
我了解到工厂方法仅用于创建一个产品,而抽象工厂用于创建相关或依赖产品的系列。
但是工厂方法模式如何使用继承而抽象工厂模式如何使用组合我不清楚。
我知道这个问题被问过几次了,有人可以用下面的代码解释一下继承和组合是如何发生的吗?
工厂方法代码
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 IFridge
{
public:
virtual string Run(void) = 0;
};
class FridgeSamsung : public IFridge
{
public:
string Run(void)
{
return "You are now running Samsung Fridge\n";
}
};
class FridgeWhirlpool : public IFridge
{
public:
string Run(void)
{
return "You are now running Whirlpool Fridge\n";
}
};
class IWashingMachine
{
public:
virtual string Run(void) = 0;
};
class WashingMachineSamsung : public IWashingMachine
{
public:
string Run(void)
{
return "You are now running Samsung Washing Machine\n";
}
};
class WashingMachineWhirlpool : public IWashingMachine
{
public:
string Run(void)
{
return "You are now running Whirlpool Washing Machine\n";
}
};
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();
}
};
int main()
{
IFridge* fridge; //Client just knows about fridge and washingMachine.
IWashingMachine* washingMachine; //and factory. He will write operations which
IFactory* factory; //work on fridges and washingMachines.
factory = new FactorySamsung;
//This is the only place where the client
//has to make a choice.
//The rest of the code below will remain same, even
//if the factory is changed. He can change the factory and the same range
//of products but from a different factory will be returned. No need to
//change any code.
fridge = factory->GetFridge();
cout << fridge->Run();
washingMachine = factory->GetWashingMachine();
cout << washingMachine->Run();
cout << "\n";
delete factory;
factory = new FactoryWhirlpool;
//See same client code.
fridge = factory->GetFridge();
cout << fridge->Run();
washingMachine = factory->GetWashingMachine();
cout << washingMachine->Run();
cout << "\n";
delete factory;
return 1;
}
这是经过深思熟虑后修改的回复。
Factory方法: 通常createObject()是Creator对象的一个方法
抽象工厂:通常Factory Object是Creator对象的一个属性。
现在假设 createObject() 和 Factory Object 属于它们各自的 Creator。
工厂方法:createObject() 可能会在 Creator 的子 类 中发生变化。这是通过更改 creatObject() 的 实现 通过继承 .
实现的
Abstract Factory:Factory Object在Creator的子类中也可能发生变化。然而,这种变化是通过用不同的工厂对象替换一个工厂对象而发生的,即Creator对象被组合.
改变了
在您的演示中,creatObject() 和 Factory 对象在 Creator(s) 之外,因此混淆了 composition/inheritance!
之间的区别
Christopher Okhravi 在 YouTube 上有很棒的模式视频。
工厂方法https://www.youtube.com/watch?v=EcFVTgRHJLM&index=4&list=PLrhzvIcii6GNjpARdnO4ueTUAVR9eMBpc
抽象工厂https://www.youtube.com/watch?v=v-GiuMmsXj4&index=5&list=PLrhzvIcii6GNjpARdnO4ueTUAVR9eMBpc
这里要求的是工厂方法的一个版本(这次是在 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;
}
这里还有一个稍微修改过的抽象工厂版本,以更好地展示它是如何使用的。刚刚添加了一个房子对象。请注意,我为我的编译器将所有 "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;
}
我正在研究抽象工厂模式与工厂方法模式之间的区别。 我了解到工厂方法仅用于创建一个产品,而抽象工厂用于创建相关或依赖产品的系列。 但是工厂方法模式如何使用继承而抽象工厂模式如何使用组合我不清楚。
我知道这个问题被问过几次了,有人可以用下面的代码解释一下继承和组合是如何发生的吗?
工厂方法代码
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 IFridge
{
public:
virtual string Run(void) = 0;
};
class FridgeSamsung : public IFridge
{
public:
string Run(void)
{
return "You are now running Samsung Fridge\n";
}
};
class FridgeWhirlpool : public IFridge
{
public:
string Run(void)
{
return "You are now running Whirlpool Fridge\n";
}
};
class IWashingMachine
{
public:
virtual string Run(void) = 0;
};
class WashingMachineSamsung : public IWashingMachine
{
public:
string Run(void)
{
return "You are now running Samsung Washing Machine\n";
}
};
class WashingMachineWhirlpool : public IWashingMachine
{
public:
string Run(void)
{
return "You are now running Whirlpool Washing Machine\n";
}
};
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();
}
};
int main()
{
IFridge* fridge; //Client just knows about fridge and washingMachine.
IWashingMachine* washingMachine; //and factory. He will write operations which
IFactory* factory; //work on fridges and washingMachines.
factory = new FactorySamsung;
//This is the only place where the client
//has to make a choice.
//The rest of the code below will remain same, even
//if the factory is changed. He can change the factory and the same range
//of products but from a different factory will be returned. No need to
//change any code.
fridge = factory->GetFridge();
cout << fridge->Run();
washingMachine = factory->GetWashingMachine();
cout << washingMachine->Run();
cout << "\n";
delete factory;
factory = new FactoryWhirlpool;
//See same client code.
fridge = factory->GetFridge();
cout << fridge->Run();
washingMachine = factory->GetWashingMachine();
cout << washingMachine->Run();
cout << "\n";
delete factory;
return 1;
}
这是经过深思熟虑后修改的回复。
Factory方法: 通常createObject()是Creator对象的一个方法
抽象工厂:通常Factory Object是Creator对象的一个属性。
现在假设 createObject() 和 Factory Object 属于它们各自的 Creator。
工厂方法:createObject() 可能会在 Creator 的子 类 中发生变化。这是通过更改 creatObject() 的 实现 通过继承 .
实现的Abstract Factory:Factory Object在Creator的子类中也可能发生变化。然而,这种变化是通过用不同的工厂对象替换一个工厂对象而发生的,即Creator对象被组合.
改变了在您的演示中,creatObject() 和 Factory 对象在 Creator(s) 之外,因此混淆了 composition/inheritance!
之间的区别Christopher Okhravi 在 YouTube 上有很棒的模式视频。
工厂方法https://www.youtube.com/watch?v=EcFVTgRHJLM&index=4&list=PLrhzvIcii6GNjpARdnO4ueTUAVR9eMBpc
抽象工厂https://www.youtube.com/watch?v=v-GiuMmsXj4&index=5&list=PLrhzvIcii6GNjpARdnO4ueTUAVR9eMBpc
这里要求的是工厂方法的一个版本(这次是在 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;
}
这里还有一个稍微修改过的抽象工厂版本,以更好地展示它是如何使用的。刚刚添加了一个房子对象。请注意,我为我的编译器将所有 "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;
}