设计模式的工厂模式一共有三种:简单工厂模式,工厂模式,抽象工厂模式
简单工厂模式原理:只有一个工厂类,通过传参的形式确定所创建的产品对象种类
代码如下:
#include <stdio.h>
#include <memory>
#include <iostream>
using namespace std;
#define COREA 0
#define COREB 1
class Core
{
public:
virtual void show(){}
};
class CoreA:public Core
{
public:
virtual void show()
{
cout << "This is corea" <<endl;
}
};
class CoreB:public Core
{
public:
virtual void show()
{
cout << "This is coreb" <<endl;
}
}; class Factory
{
public:
Core* create(int producetype); }; Core* Factory::create(int producetype)
{
switch(producetype)
{
case 0:
return new CoreA();
break;
case 1:
return new CoreB();
break;
default:
return NULL;
} }
int main()
{
Factory * fa = new Factory();
Core* pcore = fa->create(0);
pcore->show();
return 0;
}
工厂方法模式原理:通过在客户调用不同的工厂类来实现同一类对象的创建。
produce.h代码
#ifndef PRODUCE_H
#define PRODUCE_H
#include <iostream>
using namespace std;
class Core
{
public:
virtual void show(){}
};
class CoreA:public Core
{
public:
virtual void show()
{
cout << "This is corea" <<endl;
}
};
class CoreB:public Core
{
public:
virtual void show()
{
cout << "This is coreb" <<endl;
}
};
#endif
factory.h代码
#ifndef FACOTRY_H
#define FACOTRY_H
#include <iostream>
#include "produce.h"
using namespace std; class Factory
{
public:
virtual Core* create(); };
class FactoryCoreA:public Factory
{
virtual Core* create();
};
class FactoryCoreB:public Factory
{
virtual Core* create();
}; #endif
factory.cpp代码
#include <iostream>
#include "factory.h"
using namespace std;
Core* Factory::create()
{
return NULL;
}
Core* FactoryCoreA::create()
{
return new CoreA();
}
Core* FactoryCoreB::create()
{
return new CoreB();
}
客户main.cpp代码
#include <stdio.h>
#include <memory>
#include <iostream>
#include "factory.h"
#include "produce.h"
using namespace std;
#define COREA 0
#define COREB 1 int main()
{
Factory * fa = new FactoryCoreA();
Core* pcore = fa->create();
pcore->show();
return 0;
}
抽象工厂原理:抽象工厂可以创建一系列的产品,缺点如果是增加产品种类,需要重新修改抽象工厂接口和实现
部分代码如下
factory.cpp
#include <iostream>
#include "factory.h"
#include "Board.h"
using namespace std;
Core* Factory::createcore()
{
return NULL;
} Core* FactorySeriaA::createcore()
{
return new CoreA();
}
Core* FactorySeriaB::createcore()
{
return new CoreB();
} Board* Factory::createboard()
{
return NULL;
}
Board* FactorySeriaA::createboard()
{
return new BoardA();
}
Board* FactorySeriaB::createboard()
{
return new BoardB();
}
fatory.h
#ifndef FACOTRY_H
#define FACOTRY_H
#include <iostream>
#include "produce.h"
#include "Board.h"
using namespace std; class Factory
{
public:
virtual Core* createcore();
virtual Board* createboard(); };
class FactorySeriaA:public Factory
{
virtual Core* createcore();
virtual Board* createboard();
};
class FactorySeriaB:public Factory
{
virtual Core* createcore();
virtual Board* createboard();
}; #endif
新增的产品种类类Board.h
#ifndef BOARD_H
#define BOARD_H
#include <iostream>
using namespace std;
class Board
{
public:
virtual void show(){}
};
class BoardA:public Board
{
public:
virtual void show()
{
cout << "This is boarda" <<endl;
}
};
class BoardB:public Board
{
public:
virtual void show()
{
cout << "This is boardb" <<endl;
}
};
#endif