工厂方法模式

    工厂方法模式 , 一个产品多个工厂 , 每个工厂有一个抽象工厂 , 抽象工厂继承产品工厂 .

    好处在于能够很方便的扩展工厂 


/*************************************************************************
	> File Name: define_1.h
	> Author: Code4
	> Mail: 1614652215@qq.com 
	> Created Time: 2018年04月15日 星期日 09时43分57秒
 ************************************************************************/
//工厂方法模式
//有一个产品 ,被两个工厂制造 ,
// 使用抽象工厂 , 生成两个抽象工厂 , 分别两个工厂进行调用
#pragma once
#include "iostream"

using namespace std;
//产品1
class UTable{
	public :
		virtual int Find() = 0 ;
		virtual int Add() = 0 ;
};
//工厂1
class MUtable : public UTable{
	public :
		int Find () ;
		int Add() ;
};
//工厂2
class SUtable : public UTable{
	public :
		int Find()  ;
		int Add()  ;
};

//抽象工厂
class Abs_Factory {
	public :
		virtual	UTable *Create_Product() = 0;
};

//抽象工厂1
class M_Factory : public Abs_Factory {
	public :
		UTable *Create_Product() ;
};
//抽象工厂2
class S_Factory : public Abs_Factory {
	public :
		UTable *Create_Product() ;
};


/*************************************************************************
	> File Name: index_1.cpp
	> Author: Code4
	> Mail: 1614652215@qq.com 
	> Created Time: 2018年04月15日 星期日 09时44分23秒
 ************************************************************************/
#include "iostream"
#include "define_1.h"

using namespace std ;

int MUtable :: Find (){
	cout<< "MU table find"<<endl ;
	return 0 ;
}

int MUtable :: Add(){
	cout<<"MU table Add"<<endl ;
	return 0 ;
}

int SUtable :: Find (){
	cout<<"SU table Find"<<endl ;
	return 0;
}

int SUtable :: Add(){
	cout<<"SU table Add"<<endl ;
	return 0;
}

UTable * M_Factory :: Create_Product(){
	return new MUtable() ;
}

UTable * S_Factory :: Create_Product(){
	return new SUtable() ;
}


/*************************************************************************
	> File Name: main.cpp
	> Author: Code4
	> Mail: 1614652215@qq.com 
	> Created Time: 2018年04月15日 星期日 09时44分39秒
 ************************************************************************/
#include "iostream"
#include "define_1.h"
#include "index_1.cpp"

using namespace std ;

int main(){
	Abs_Factory *l = new M_Factory() ;
	UTable * n = l->Create_Product() ;
	n->Find() ;
	return 0 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值