我从一开始就开始学习更多关于C++设计模式的知识。我终于开始阅读http://sourcemaking.com/design_patterns网站了。但是在遇到http://sourcemaking.com/files/sm/images/patterns/Abstract_Factory.gif图像之后,我实际上无法将图像映射到实际的类(及其接口)结构。
什么是矩形,箭头,虚线,以及我们如何将其转换为实际的代码实现?
发布于 2012-11-29 23:04:17
使用UML - Unified Modeling Language绘制了这些图表。是的,好吧,最终你将不得不用你想要的语言实现设计模式,但对模式的理解必须比代码更高。
发布于 2012-11-29 23:03:54
这是UML http://en.wikipedia.org/wiki/Unified_Modeling_Language,一种描述软件设计的语言。此模式和设计模式独立于任何编程语言
发布于 2012-11-29 23:25:41
回答:“我们如何将其转换为实际的代码实现?”
这个带有注释和CamelCase的统一建模语言看起来像Java,
但下面是图表中的一些C++模式:
<<interface>>表示公共继承,<<interface>>表示C++中的抽象类,Java中的接口,带有虚线箭头的白色东西是备注。在这种情况下,它们会为您提供实现细节,您也可以逐字键入它们。在看代码之前,我要说我讨厌驼峰式的情况,我鼓励你像STL和C++这样的Boost库那样做underscore_notation。因此,我将每个类都改为下划线表示法。因此,部分实现可能如下所示:
class Abstract_platform {
public:
virtual ~Abstract_platform()=0; // this UML does not specify any functions but we make this class abstract.
};
class Platform_one : public Abstract_platform {
};
class Platform_two : public Abstract_platform {
public:
/// you should implement this make function with shared_ptr, OR NO POINTERS AT ALL but I go according to the notes
Product_one_platform_two* make_product_one();
Product_two_platform_two* make_product_two();
// I provide you with a better_make. It is better than the former make functions
// due to RVO (Return value optimization see wikipedia)
// so this is again a hint that this UML was originally for Java.
Product_one_platform_two better_make_product_one();
};
class Class1 {
private:
Abstract_platform* platform; // OR shared_ptr<Abstract_platform>, OR Abstract_platform&
Abstract_product_two* product_two;
};
/// **Implementation file**
Product_one_platform_two* Platform_two::make_product_one()
{
return new Product_one_platform_two();
}
Product_two_platform_two* Platform_two::make_product_two()
{
return new Product_two_platform_two();
}
Product_one_platform_two Platform_two::better_make_product_one()
{
return Product_one_platform_two();
}还要注意的是,人们更喜欢IPlatform匈牙利符号,其中"I“代表”Abstract_platform“,而不是界面。
https://stackoverflow.com/questions/13628558
复制相似问题