【发布时间】:2018-07-18 05:59:41
【问题描述】:
我正在研究抽象工厂模式与工厂方法模式之间的区别。 我知道工厂方法仅用于创建一种产品,但抽象工厂是关于创建相关或依赖产品的系列。 但是我不清楚工厂方法模式如何使用继承而抽象工厂模式使用组合。
我知道这已经被问过几次了,谁能用下面的代码解释一下继承和组合是如何发生的?
工厂方法代码
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;
}
【问题讨论】:
-
第一个代码在我看来不像典型的工厂方法。带有
Create(string type)的工厂类似乎过于复杂且不必要。你从哪里得到的代码?另外,您从哪里读到“工厂方法模式使用继承而抽象工厂模式使用组合”? -
@guillaume31“工厂方法模式使用继承,而抽象工厂模式使用组合”这个话题已经在stackoverflow本身here中讨论过
-
@guillaume31 我从here 获取的代码作为参考如果您觉得这不是正确的方法,请直接使用正确的方法。
-
我觉得有点做作。如需更清晰的示例,请查看您刚刚链接到的 SO 问题中的 Mark Seemann 示例代码。
-
好的,您从中获取代码的文章有一个很大的缺陷,因为尽管标题声称,它解释了工厂模式 - 其主要目标是将复杂的创建逻辑从构造函数中提取到一个单独的类 - 而不是 Factory Method 模式本身。
标签: c++ design-patterns factory-pattern abstract-factory