现有一批装备(产品),分为不同的部位(上装、下装)与不同的等级(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. 通过谷歌浏览器,找到页面某个事件属于哪个js文件

    在进行web开发中,有时候需要找到某个事件是属于哪个js文件,以便对文件进行修改,进行代码开发 1.打开谷歌浏览器, 打开事件所在页面, 鼠标右键, 点击"检查"项; 2.选中El ...

  2. vue学前班004(基础指令与使用技巧)

    我学vue 的最终目的是为了 做apicloud 和vue 的开发  作为配合apicloud的前端框架使用 所以项目用不到的会暂时不介绍. (强烈建议  官网案例走一遍) 基础指令的学习(结合aui ...

  3. eclipse代码中使用到Launcher获取类加载器,找不到启动器类。

    解决:移除系统依赖的jar包,重新导入. 只需要在project build path中先移除JRE System Library,再添加库JRE System Library,重新编译后就一切正常了 ...

  4. day 34 js 基础后部分 BOM 和 事件和正则

    前情提要 今天主要学习的是bom 和事件 一:正则表达式 <!DOCTYPE html> <html lang="en"> <head> < ...

  5. 2. Linear Model

    1. 基本形式 给定由$d$个属性描述的示例 $\textbf{x} =(x_1;x_2;...,x_n)$,其中$x_i$是$x$在第$i$个属性上的取值,线性模型(linear model)试图学 ...

  6. UiAutomator -- UiObject2 API

    1.点击与长按 void click() Clicks on this object. void click(long duration) Performs a click on this objec ...

  7. xampp启动失败 Apache shutdown unexpectedly

    我知道是端口被我的虚拟机占用,但是我的虚拟机也要同时打开,所以 我需要修改xampp的端口 1, 2修改二个配置文件 修改httpd.conf 修改httpd-ssl.conf 4,重启xampp

  8. Tomcat中组件的生命周期管理公共接口Lifecycle

    Tomcat的组件都会实现一个Lifecycle接口,以方便组件的生命周期的统一管理 interface Lifecycle 组件生命周期中主要的几个方法 增加监听器,事件委托机制 public vo ...

  9. nlp 总结 分词,词义消歧,词性标注,命名体识别,依存句法分析,语义角色标注

    分词 中文分词 (Word Segmentation, WS) 指的是将汉字序列切分成词序列. 因为在汉语中,词是承载语义的最基本的单元.分词是信息检索.文本分类.情感分析等多项中文自然语言处理任务的 ...

  10. boost bind使用指南

    bind - boost 头文件: boost/bind.hpp bind 是一组重载的函数模板.用来向一个函数(或函数对象)绑定某些参数. bind的返回值是一个函数对象. 它的源文件太长了. 看不 ...