UML图和C ++设计模式

时间:2012-11-29 15:00:44

标签: c++ design-patterns uml

我从一开始就开始学习更多有关C ++设计模式的知识。我终于开始阅读http://sourcemaking.com/design_patterns网站了。但在遇到http://sourcemaking.com/files/sm/images/patterns/Abstract_Factory.gif图像后,我无法将图像映射到实际的类(及其界面)构造中。

矩形,箭头,虚线以及我们如何将其转换为实际的代码实现是什么?

3 个答案:

答案 0 :(得分:3)

图表以UML绘制 - Unified Modeling Language。你应该真正熟悉它们,因为为了研究设计模式,你并不需要真正的代码。是的,好吧,最终你必须用你想要的语言实现设计模式,但是对模式的理解必须在比代码更高的层次上。

答案 1 :(得分:1)

这是UML http://en.wikipedia.org/wiki/Unified_Modeling_Language一种语言描述的软件设计。这种和设计模式独立于任何编程语言

答案 2 :(得分:0)

回答:“我们如何将其转换为实际的代码实现?”

这个带有注释和CamelCase的UML看起来像Java,
但是这里有一些图表中的C ++模式:

  • 箭头表示通常是指针或shared_ptr-s,
  • 白头箭意味着公共继承,
  • <<interface>> 表示C ++中的抽象类,Java中的接口,
  • 带有虚线箭头的白色东西是笔记。在这种情况下,它们为您提供了实现细节,您也可以按字面输入它们。

在查看代码之前,让我说我讨厌驼峰的情况,我宁愿鼓励你做下划线注释,就像STL和Boost这样的C ++库。因此我已经改变了每个班级以强调表示法。 因此,部分实现可能看起来像这样:

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(); 
}

另请注意,Abstract_platform人不喜欢IPlatform匈牙利语,而“I”代表“接口”。

相关问题