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

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

有些重复的代码,是直接用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

设计模式学习——抽象工厂模式(Abstract Factory Pattern)的更多相关文章

  1. 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)

    原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factor ...

  2. 【设计模式】抽象工厂模式 Abstract Factory Pattern

    简单工厂模式是一个工厂类根据工厂方法的参数创建不出不同的产品, 工厂方法模式是每一个产品都有一个一一对应的工厂负责创建该产品.那么今天要讲的抽象工厂模式是一个工厂能够产生关联的一系列产品.抽象工厂模式 ...

  3. 二十四种设计模式:抽象工厂模式(Abstract Factory Pattern)

    抽象工厂模式(Abstract Factory Pattern) 介绍提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 示例有Message和MessageModel,Messag ...

  4. 【UE4 设计模式】抽象工厂模式 Abstract Factory Pattern

    概述 描述 提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类:具体的工厂负责实现具体的产品实例 抽象工厂中每个工厂可以创建多种产品(如苹果公司生产iPhone.iPad): 工厂方法 ...

  5. 设计模式之抽象工厂模式(Abstract Factory Pattern)

    一.抽象工厂模式的由来 抽象工厂模式,最开始是为了解决操作系统按钮和窗体风格,而产生的一种设计模式.例如:在windows系统中,我们要用windows设定的按钮和窗体,当我们切换Linux系统时,要 ...

  6. 设计模式 - 抽象工厂模式(abstract factory pattern) 具体解释

    抽象工厂模式(abstract factory pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/2709 ...

  7. Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)

    一.抽象工厂模式简介(Bref Introduction) 抽象工厂模式(Abstract Factory Pattern),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类.优点 ...

  8. C#设计模式——抽象工厂模式(Abstract Factory Pattern)

    一.概述在软件开发中,常常会需要创建一系列相互依赖的对象,同时,由于需求的变化,往往存在较多系列对象的创建工作.如果采用常规的创建方法(new),会造成客户程序和对象创建工作的紧耦合.对此,抽象工厂模 ...

  9. 六个创建模式之抽象工厂模式(Abstract Factory Pattern)

    问题: 使用工厂方法模式的主要问题是工厂类过多,每个产品对应一个工厂,不利于维护.因此可以考虑使用一个工厂创建一个产品族. 定义: 提供创建一些列相关或相互依赖的对象实例的接口,这些类可以称为一个产品 ...

  10. 23种设计模式之抽象工厂(Abstract Factory Pattern)

    抽象工厂 当想创建一组密不可分的对象时,工厂方法似乎就不够用了 抽象工厂是应对产品族概念的.应对产品族概念而生,增加新的产品线很容易,但是无法增加新的产品.比如,每个汽车公司可能要同时生产轿车.货车. ...

随机推荐

  1. webpack快速入门——如何安装webpack及注意事项

    1.window+R键,输入cmd打开命令行工具,输入 mkdir XXXX(XX:文件夹名): 2.cd XXX 进入刚刚创建好的文件夹里,输入cnpm install -g webpack (安装 ...

  2. 再看javascript执行上下文、变量对象

    突然看到一篇远在2010年的老文,作者以章节的形式向我们介绍了ECMA-262-3的部分内容,主要涉及到执行上下文.变量对象.作用域.this等语言细节.内容短小而精悍,文风直白而严谨,读完有酣畅淋漓 ...

  3. 自己实现简单的RSA秘钥生成与加解密(Java )

    最近在学习PKI,顺便接触了一些加密算法.对RSA着重研究了一下,自己也写了一个简单的实现RSA算法的Demo,包括公.私钥生成,加解密的实现.虽然比较简单,但是也大概囊括了RSA加解密的核心思想与流 ...

  4. Java基础梳理(一)

    List和Set比较,各自的子类比较 对比一:Arraylist与LinkedList的比较 1.ArrayList是实现了基于动态数组的数据结构,因为地址连续,一旦数据存储好了,查询操作效率会比较高 ...

  5. POJ 1126

    #include <stdio.h> #include <string> #include <iostream> using namespace std; int ...

  6. .NET 如何隐藏Console Application的窗口

    1)创建Console Application工程 2)修改Output type 为Windows Application即可

  7. 剑指offer五十六之删除链表中重复的结点

    一.题目 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3->3->4->4->5 处理后 ...

  8. Kafka消息队列

    转自:http://blog.csdn.net/yfkiss/article/details/17348693 代码案例 http://blog.csdn.net/ganglia/article/de ...

  9. python网络爬虫技术图谱

  10. Java之集合(二)ArrayDeque

    转载请注明源出处:http://www.cnblogs.com/lighten/p/7283928.html 1.前言 上章讲解了Java中的集合接口和相关实现抽象类,本章开始介绍一些具体的实现类,第 ...