设计模式学习——抽象工厂模式(Abstract Factory Pattern)

现有一批装备(产品),分为不同的部位(上装、下装)与不同的等级(lv1、lv2)。又有不同lv的工厂,只生产对应lv的全套装备。

代码实现上...本次写得比较偷懒,函数实现都写在头文件了....

有些重复的代码,是直接用sed替换一些字符生成的。如:

sed 's/lv1/lv2/g' Factory_lv1.h | sed 's/LV1/LV2/g' > Factory_lv2.h

Suit

 1  ///
 2  /// @file    Suit.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 15:47:04
 5  ///
 6 
 7 #ifndef __SUIT_H__
 8 #define __SUIT_H__
 9 
10 #include <iostream>
11  
12 namespace marrs{
13  
14 using std::cout;
15 using std::endl;
16 using std::string;
17 
18 class Suit
19 {
20     public:
21         virtual ~Suit(){}
22     
23     public:
24         void put_on()
25         {
26             cout << "put on" << endl;
27             show_msg();
28         }
29         void put_off()
30         {
31             cout << "put off" << endl;
32             show_msg();
33         }
34 
35     public:
36         virtual string get_name() = 0;
37         virtual string get_type() = 0;
38 
39     private:
40         void show_msg()
41         {
42             cout << "type : " << get_type() << endl;
43             cout << "name : " << get_name() << endl;
44         }
45 };
46  
47 }
48 
49 #endif // __SUIT_H__

Trousers

 1  ///
 2  /// @file    Trousers.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 15:54:48
 5  ///
 6 
 7 #ifndef __TROUSERS_H__
 8 #define __TROUSERS_H__
 9 
10 #include "Suit.h"
11 
12 namespace marrs{
13 
14 class Trousers
15 : public Suit
16 {
17     public:
18         string get_type()
19         {
20             return "Trousers";
21         }
22 };
23 
24 }
25 
26 #endif // __TROUSERS_H__

Clothes

 1  ///
 2  /// @file    Clothes.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 16:01:08
 5  ///
 6  
 7 #ifndef __CLOTHES_H__
 8 #define __CLOTHES_H__
 9 
10 #include "Suit.h"
11 
12 namespace marrs{
13 
14 class Clothes
15 : public Suit
16 {
17     public:
18         string get_type()
19         {
20             return "Clothes";
21         }
22 };
23  
24 }
25 
26 #endif // __CLOTHES_H__

Trouser_lv1

 1  ///
 2  /// @file    Trouser_lv1.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 16:02:40
 5  ///
 6  
 7 #ifndef __TROUSER_LV1_H__
 8 #define __TROUSER_LV1_H__
 9 
10 #include "Trousers.h"
11 
12 namespace marrs{
13 
14 class Trouser_lv1
15 : public Trousers
16 {
17     public:
18         string get_name()
19         {
20             return "Trouser_lv1";
21         }
22 };
23  
24 }
25 
26 #endif // __TROUSER_LV1_H__

Trouser_lv2

 1  ///
 2  /// @file    Trouser_lv2.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 16:02:40
 5  ///
 6  
 7 #ifndef __TROUSER_LV2_H__
 8 #define __TROUSER_LV2_H__
 9 
10 #include "Trousers.h"
11 
12 namespace marrs{
13 
14 class Trouser_lv2
15 : public Trousers
16 {
17     public:
18         string get_name()
19         {
20             return "Trouser_lv2";
21         }
22 };
23  
24 }
25 
26 #endif // __TROUSER_LV2_H__

Clothe_lv1

 1  ///
 2  /// @file    Clothe_lv1.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 16:02:40
 5  ///
 6  
 7 #ifndef __CLOTHE_LV1_H__
 8 #define __CLOTHE_LV1_H__
 9 
10 #include "Clothes.h"
11 
12 namespace marrs{
13 
14 class Clothe_lv1
15 : public Clothes
16 {
17     public:
18         string get_name()
19         {
20             return "Clothe_lv1";
21         }
22 };
23  
24 }
25 
26 #endif // __CLOTHE_LV1_H__

Clothe_lv2

 1  ///
 2  /// @file    Clothe_lv2.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 16:02:40
 5  ///
 6  
 7 #ifndef __CLOTHE_LV2_H__
 8 #define __CLOTHE_LV2_H__
 9 
10 #include "Clothes.h"
11 
12 namespace marrs{
13 
14 class Clothe_lv2
15 : public Clothes
16 {
17     public:
18         string get_name()
19         {
20             return "Clothe_lv2";
21         }
22 };
23  
24 }
25 
26 #endif // __CLOTHE_LV2_H__

Factory

 1  ///
 2  /// @file    Factory.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 16:07:00
 5  ///
 6 
 7 #ifndef __FACTORY_H__
 8 #define __FACTORY_H__
 9 
10 #include "Trousers.h"
11 #include "Clothes.h"
12 
13 namespace marrs{
14 
15 class Factory
16 {
17     public:
18         virtual ~Factory(){}
19 
20     public:
21         virtual Trousers * Create_Trousers() = 0;
22         virtual Clothes * Create_Clothes() = 0;
23 };
24  
25 }
26 
27 #endif // __FACTORY_H__

Factory_lv1

 1  ///
 2  /// @file    Factory_lv1.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 16:09:15
 5  ///
 6  
 7 #ifndef __FACTORY_LV1_H__
 8 #define __FACTORY_LV1_H__
 9 
10 #include "Factory.h"
11 #include "Trouser_lv1.h"
12 #include "Clothe_lv1.h"
13 
14 namespace marrs{
15  
16 class Factory_lv1
17 : public Factory
18 {
19     public:
20         Trousers * Create_Trousers()
21         {
22             return new Trouser_lv1;
23         }
24 
25         Clothes * Create_Clothes()
26         {
27             return new Clothe_lv1;
28         }
29 };
30  
31 }
32 
33 #endif // __FACTORY_LV1_H__

Factory_lv2

 1  ///
 2  /// @file    Factory_lv2.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 16:09:15
 5  ///
 6  
 7 #ifndef __FACTORY_LV2_H__
 8 #define __FACTORY_LV2_H__
 9 
10 #include "Factory.h"
11 #include "Trouser_lv2.h"
12 #include "Clothe_lv2.h"
13 
14 namespace marrs{
15  
16 class Factory_lv2
17 : public Factory
18 {
19     public:
20         Trousers * Create_Trousers()
21         {
22             return new Trouser_lv2;
23         }
24 
25         Clothes * Create_Clothes()
26         {
27             return new Clothe_lv2;
28         }
29 };
30  
31 }
32 
33 #endif // __FACTORY_LV2_H__

main

 1  ///
 2  /// @file    main.cc
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 16:11:54
 5  ///
 6 
 7 #include "Factory_lv1.h"
 8 #include "Factory_lv2.h"
 9 
10 using namespace marrs;
11 
12 void Lv1_action()
13 {
14     Factory * factory = new Factory_lv1;
15     Suit * suit_trouse_lv1 = factory->Create_Trousers();
16     suit_trouse_lv1->put_on();
17     suit_trouse_lv1->put_off();
18     Suit * suit_clothe_lv1 = factory->Create_Clothes();
19     suit_clothe_lv1->put_on();
20     suit_clothe_lv1->put_off();
21     delete suit_trouse_lv1;
22     delete suit_clothe_lv1;
23     delete factory;
24     return;
25 }
26 
27 void Lv2_action()
28 {
29     Factory * factory = new Factory_lv2;
30     Suit * suit_trouse_lv2 = factory->Create_Trousers();
31     suit_trouse_lv2->put_on();
32     suit_trouse_lv2->put_off();
33     Suit * suit_clothe_lv2 = factory->Create_Clothes();
34     suit_clothe_lv2->put_on();
35     suit_clothe_lv2->put_off();
36     delete suit_trouse_lv2;
37     delete suit_clothe_lv2;
38     delete factory;
39     return;
40 }
41 
42 int main()
43 {
44     Lv1_action();
45     Lv2_action();
46     return 0;
47 }

编译,运行

[ccx@ubuntu ~/object-oriented/Abstract-Factory-Pattern]$>g++ * -o suit_action.exe
[ccx@ubuntu ~/object-oriented/Abstract-Factory-Pattern]$>./suit_action.exe 
put on
type : Trousers
name : Trouser_lv1
put off
type : Trousers
name : Trouser_lv1
put on
type : Clothes
name : Clothe_lv1
put off
type : Clothes
name : Clothe_lv1
put on
type : Trousers
name : Trouser_lv2
put off
type : Trousers
name : Trouser_lv2
put on
type : Clothes
name : Clothe_lv2
put off
type : Clothes
name : Clothe_lv2
原文地址:https://www.cnblogs.com/chinxi/p/7353687.html