设计模式学习——抽象工厂模式(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)
抽象工厂 当想创建一组密不可分的对象时,工厂方法似乎就不够用了 抽象工厂是应对产品族概念的.应对产品族概念而生,增加新的产品线很容易,但是无法增加新的产品.比如,每个汽车公司可能要同时生产轿车.货车. ...
随机推荐
- MySQL(慢日志记录)
day63 参考:https://www.cnblogs.com/wupeiqi/articles/5716963.html
- Java 日期加减
import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date; public class Test ...
- cassandra用户名和密码的设置
设置Cassandra使用用户名和密码验证的步骤如下: 1.修改${CASSANDRA_HOME}/conf/cassandra.yaml,把authenticator: AllowAllAuthen ...
- springmvc执行流程详细介绍
1.什么是MVC MVC是Model View Controller的缩写,它是一个设计模式 2.springmvc执行流程详细介绍 第一步:发起请求到前端控制器(DispatcherServlet) ...
- OSS和CDN配置使用
Oss和cdn目的就是: 1 把资源文件和程序分开存储 2 可以通过cdn缓存加速 下面介绍阿里云OSS和CDN如何配制 一 阿里云开通子RAM账户 1 不使用主账号访问OSS,需要创建子RA ...
- 再学Java 之 解决No enclosing instance of type * is accessible
深夜,临睡前写了个小程序,出了点小问题 public class Test_drive { public static void main(String[] args){ A a = new A(); ...
- 详解XMLHttpRequest的跨域资源共享
0x00 背景 在Browser Security-同源策略.伪URL的域这篇文章中提到了浏览器的同源策略,其中提到了XMLHttpRequest严格遵守同源策略,非同源不可请求.但是,在实践当中,经 ...
- 【数组】Sort Colors
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- C/C++ -- Gui编程 -- Qt库的使用 -- Qt5试用
1.头文件<QtGui>变成了<QtWidgets> 相应<QtGui/***>变成了<QtWidgets/***> 2.QTextCodec::set ...
- Android硬件抽象层(HAL)深入剖析(二)【转】
上一篇我们分析了android HAL层的主要的两个结构体hw_module_t(硬件模块)和hw_device_t(硬件设备)的成员,下面我们来具体看看上层app到底是怎么实现操作硬件的? 我们知道 ...