2012-11-29 7 views
0

C++ 디자인 패턴에 대해 처음부터 처음부터 배우기 시작했습니다. 드디어 http://sourcemaking.com/design_patterns 웹 사이트를 읽기 시작했습니다. 그러나 http://sourcemaking.com/files/sm/images/patterns/Abstract_Factory.gif 이미지를 만난 후에는 실제로 이미지를 실제 클래스 (및 인터페이스)에 매핑 할 수 없습니다.UML 다이어그램과 C++ 디자인 패턴

사각형, 화살표, 점선 및 실제 코드 구현으로 변환하는 방법은 무엇입니까?

+4

[UML] (http://en.wikipedia.org/wiki/Unified_Modeling_Language)을 검색하고 싶습니다. –

답변

3

다이어그램은 UML - Unified Modeling Language으로 그려져 있습니다. 디자인 패턴을 연구하기 위해서는 실제 코드가 필요하지 않기 때문에 실제로 익숙해 져야합니다. 그렇습니다. 결국 원하는 언어로 디자인 패턴을 구현해야하지만 패턴을 이해하는 것이 코드보다 상위 레벨에 있어야합니다.

0

답변 : "실제 코드 구현으로 변환하는 방법은 무엇입니까?" 보통 포인터 또는 shared_ptr의-의 의미

  • 화살표,
  • 흰색 :

    노트와이 UML과 낙타 표기법 자바처럼 보인다는
    그러나 여기에는 다이어그램에서 일부 C++ 패턴입니다 머리글 화살표
    평균 공개 상속,

  • <<interface>>은 Java의 인터페이스 인 C++의 추상 클래스를 의미하며,
  • 흰 점선 화살표은 메모입니다. 이 경우 구현 세부 정보가 제공되며 문자 그대로 입력 할 수도 있습니다.

코드를 살펴보기 전에 낙타의 경우를 싫어한다고 말하면서 STL과 부스트와 같은 C++ 라이브러리가 언더 코어 _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(); 
} 

은 또한 대신 Abstract_platform명 "나는" "인터페이스"의 약자 IPlatform 헝가리어 표기법을 선호 있습니다.