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

时间:2023-03-09 07:31:10
设计模式学习——抽象工厂模式(Abstract Factory Pattern)

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

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

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

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

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

Suit

  ///
/// @file Suit.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-13 15:47:04
/// #ifndef __SUIT_H__
#define __SUIT_H__ #include <iostream> namespace marrs{ using std::cout;
using std::endl;
using std::string; class Suit
{
public:
virtual ~Suit(){} public:
void put_on()
{
cout << "put on" << endl;
show_msg();
}
void put_off()
{
cout << "put off" << endl;
show_msg();
} public:
virtual string get_name() = ;
virtual string get_type() = ; private:
void show_msg()
{
cout << "type : " << get_type() << endl;
cout << "name : " << get_name() << endl;
}
}; } #endif // __SUIT_H__

Trousers

  ///
/// @file Trousers.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-13 15:54:48
/// #ifndef __TROUSERS_H__
#define __TROUSERS_H__ #include "Suit.h" namespace marrs{ class Trousers
: public Suit
{
public:
string get_type()
{
return "Trousers";
}
}; } #endif // __TROUSERS_H__

Clothes

  ///
/// @file Clothes.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-13 16:01:08
/// #ifndef __CLOTHES_H__
#define __CLOTHES_H__ #include "Suit.h" namespace marrs{ class Clothes
: public Suit
{
public:
string get_type()
{
return "Clothes";
}
}; } #endif // __CLOTHES_H__

Trouser_lv1

  ///
/// @file Trouser_lv1.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-13 16:02:40
/// #ifndef __TROUSER_LV1_H__
#define __TROUSER_LV1_H__ #include "Trousers.h" namespace marrs{ class Trouser_lv1
: public Trousers
{
public:
string get_name()
{
return "Trouser_lv1";
}
}; } #endif // __TROUSER_LV1_H__

Trouser_lv2

  ///
/// @file Trouser_lv2.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-13 16:02:40
/// #ifndef __TROUSER_LV2_H__
#define __TROUSER_LV2_H__ #include "Trousers.h" namespace marrs{ class Trouser_lv2
: public Trousers
{
public:
string get_name()
{
return "Trouser_lv2";
}
}; } #endif // __TROUSER_LV2_H__

Clothe_lv1

  ///
/// @file Clothe_lv1.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-13 16:02:40
/// #ifndef __CLOTHE_LV1_H__
#define __CLOTHE_LV1_H__ #include "Clothes.h" namespace marrs{ class Clothe_lv1
: public Clothes
{
public:
string get_name()
{
return "Clothe_lv1";
}
}; } #endif // __CLOTHE_LV1_H__

Clothe_lv2

  ///
/// @file Clothe_lv2.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-13 16:02:40
/// #ifndef __CLOTHE_LV2_H__
#define __CLOTHE_LV2_H__ #include "Clothes.h" namespace marrs{ class Clothe_lv2
: public Clothes
{
public:
string get_name()
{
return "Clothe_lv2";
}
}; } #endif // __CLOTHE_LV2_H__

Factory

  ///
/// @file Factory.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-13 16:07:00
/// #ifndef __FACTORY_H__
#define __FACTORY_H__ #include "Trousers.h"
#include "Clothes.h" namespace marrs{ class Factory
{
public:
virtual ~Factory(){} public:
virtual Trousers * Create_Trousers() = ;
virtual Clothes * Create_Clothes() = ;
}; } #endif // __FACTORY_H__

Factory_lv1

  ///
/// @file Factory_lv1.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-13 16:09:15
/// #ifndef __FACTORY_LV1_H__
#define __FACTORY_LV1_H__ #include "Factory.h"
#include "Trouser_lv1.h"
#include "Clothe_lv1.h" namespace marrs{ class Factory_lv1
: public Factory
{
public:
Trousers * Create_Trousers()
{
return new Trouser_lv1;
} Clothes * Create_Clothes()
{
return new Clothe_lv1;
}
}; } #endif // __FACTORY_LV1_H__

Factory_lv2

  ///
/// @file Factory_lv2.h
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-13 16:09:15
/// #ifndef __FACTORY_LV2_H__
#define __FACTORY_LV2_H__ #include "Factory.h"
#include "Trouser_lv2.h"
#include "Clothe_lv2.h" namespace marrs{ class Factory_lv2
: public Factory
{
public:
Trousers * Create_Trousers()
{
return new Trouser_lv2;
} Clothes * Create_Clothes()
{
return new Clothe_lv2;
}
}; } #endif // __FACTORY_LV2_H__

main

  ///
/// @file main.cc
/// @author marrs(chenchengxi993@gmail.com)
/// @date 2017-08-13 16:11:54
/// #include "Factory_lv1.h"
#include "Factory_lv2.h" using namespace marrs; void Lv1_action()
{
Factory * factory = new Factory_lv1;
Suit * suit_trouse_lv1 = factory->Create_Trousers();
suit_trouse_lv1->put_on();
suit_trouse_lv1->put_off();
Suit * suit_clothe_lv1 = factory->Create_Clothes();
suit_clothe_lv1->put_on();
suit_clothe_lv1->put_off();
delete suit_trouse_lv1;
delete suit_clothe_lv1;
delete factory;
return;
} void Lv2_action()
{
Factory * factory = new Factory_lv2;
Suit * suit_trouse_lv2 = factory->Create_Trousers();
suit_trouse_lv2->put_on();
suit_trouse_lv2->put_off();
Suit * suit_clothe_lv2 = factory->Create_Clothes();
suit_clothe_lv2->put_on();
suit_clothe_lv2->put_off();
delete suit_trouse_lv2;
delete suit_clothe_lv2;
delete factory;
return;
} int main()
{
Lv1_action();
Lv2_action();
return ;
}

编译,运行

[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