工厂方法模式 , 一个产品多个工厂 , 每个工厂有一个抽象工厂 , 抽象工厂继承产品工厂 .
好处在于能够很方便的扩展工厂
/*************************************************************************
> 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 ;
}