设计模式学习——抽象工厂模式(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
///
/// @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)的更多相关文章
- 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern)
原文:乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factory Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 抽象工厂模式(Abstract Factor ...
- 【设计模式】抽象工厂模式 Abstract Factory Pattern
简单工厂模式是一个工厂类根据工厂方法的参数创建不出不同的产品, 工厂方法模式是每一个产品都有一个一一对应的工厂负责创建该产品.那么今天要讲的抽象工厂模式是一个工厂能够产生关联的一系列产品.抽象工厂模式 ...
- 二十四种设计模式:抽象工厂模式(Abstract Factory Pattern)
抽象工厂模式(Abstract Factory Pattern) 介绍提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. 示例有Message和MessageModel,Messag ...
- 【UE4 设计模式】抽象工厂模式 Abstract Factory Pattern
概述 描述 提供一个创建一系列相关或相互依赖对象的接口,而无须指定它们具体的类:具体的工厂负责实现具体的产品实例 抽象工厂中每个工厂可以创建多种产品(如苹果公司生产iPhone.iPad): 工厂方法 ...
- 设计模式之抽象工厂模式(Abstract Factory Pattern)
一.抽象工厂模式的由来 抽象工厂模式,最开始是为了解决操作系统按钮和窗体风格,而产生的一种设计模式.例如:在windows系统中,我们要用windows设定的按钮和窗体,当我们切换Linux系统时,要 ...
- 设计模式 - 抽象工厂模式(abstract factory pattern) 具体解释
抽象工厂模式(abstract factory pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/2709 ...
- Net设计模式实例之抽象工厂模式(Abstract Factory Pattern)
一.抽象工厂模式简介(Bref Introduction) 抽象工厂模式(Abstract Factory Pattern),提供一个创建一系列相关或者相互依赖对象的接口,而无需制定他们的具体类.优点 ...
- C#设计模式——抽象工厂模式(Abstract Factory Pattern)
一.概述在软件开发中,常常会需要创建一系列相互依赖的对象,同时,由于需求的变化,往往存在较多系列对象的创建工作.如果采用常规的创建方法(new),会造成客户程序和对象创建工作的紧耦合.对此,抽象工厂模 ...
- 六个创建模式之抽象工厂模式(Abstract Factory Pattern)
问题: 使用工厂方法模式的主要问题是工厂类过多,每个产品对应一个工厂,不利于维护.因此可以考虑使用一个工厂创建一个产品族. 定义: 提供创建一些列相关或相互依赖的对象实例的接口,这些类可以称为一个产品 ...
- 23种设计模式之抽象工厂(Abstract Factory Pattern)
抽象工厂 当想创建一组密不可分的对象时,工厂方法似乎就不够用了 抽象工厂是应对产品族概念的.应对产品族概念而生,增加新的产品线很容易,但是无法增加新的产品.比如,每个汽车公司可能要同时生产轿车.货车. ...
随机推荐
- 文件操作(FILE)与常用文件操作函数
文件 1.文件基本概念 C程序把文件分为ASCII文件和二进制文件,ASCII文件又称文本文件,二进制文件和文本文件(也称ASCII码文件)二进制文件中,数值型数据是以二进制形式存储的, 而在文本文件 ...
- sublime text 另一种对齐
效果如下: http://sublime-text-unofficial-documentation.readthedocs.org/en/latest/extensibility/plugins.h ...
- 2d旋转(css3实现过度效果和动画效果)
效果: 源码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- Java序列化接口Serializable接口的作用总结
一.Java序列化接口Serializable的作用: 一个对象有对应的一些属性,把这个对象保存在硬盘上的过程叫做”持久化”. 对象的默认序列化机制写入的内容是:对象的类,类签名,以及非瞬态和非静态字 ...
- github的本地配置和项目创建
之前完成了github的安装和账号的注册,接下来要进行项目的创建和本地代码仓库的建立 1.创建项目 2.填写项目相关信息 注意:在给项目起名时,尽量起一些有意义的名字,否则会被管理员删除.因为服务器上 ...
- 四、CLR执行程序集中代码和IL代码简介
三.加载公共语言运行时中介绍了在安装了.Net Framework中加载公共语言运行时,公共语言运行时加载程序集的过程.以及通过vs stdio设置源码编译的目标平台的过程. 本问主要介绍公共语言加载 ...
- Class与Style绑定
本文主要介绍如何使用Vue来绑定操作元素的class列表和内联样式(style属性). 因为class和style都是属性,所以通过v-bind命令来处理它们:只需要通过表达式计算出结果即可,不过字符 ...
- 【Java并发编程】:使用wait/notify/notifyAll实现线程间通信
在java中,可以通过配合调用Object对象的wait()方法和notify()方法或notifyAll()方法来实现线程间的通信.在线程中调用wait()方法,将阻塞等待其他线程的通知(其他线程调 ...
- Dubbo-使用Maven构建Dubbo服务的可执行jar包
一.为什么要构建Dubbo服务的可执行jar包? 1.1 Dubbo服务运行方式比较 ✎使用Servlet容器运行(Tomcat.Jetty等) ---不可取 --缺点:增加复杂性(多了容器的端口) ...
- 【数组】3Sum
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...