抽象工厂模式
扫描二维码
随时随地手机看文章
自己画的图,有不准的多多指教
————–JAVA——————-
public interface Xiezi {
void Run();
}
public class Caoxie implements Xiezi {
@Override
public void Run() {
// TODO Auto-generated method stub
System.out.println("我是草鞋.....");
}
}
package com.shiji.chouxianggongchang;
public class tuoxie implements Xiezi {
@Override
public void Run() {
// TODO Auto-generated method stub
System.out.println("我是拖鞋.......");
}
}
package com.shiji.chouxianggongchang;
public interface Yifu {
void Run();
}
package com.shiji.chouxianggongchang;
public class Duanxiu implements Yifu {
@Override
public void Run() {
// TODO Auto-generated method stub
System.out.println("短袖");
}
}
package com.shiji.chouxianggongchang;
public class Chenshan implements Yifu{
@Override
public void Run() {
// TODO Auto-generated method stub
System.out.println("衬衫");
}
}
package com.shiji.chouxianggongchang;
public abstract class Factory {
abstract Xiezi BuyXiezi(String type);
abstract Yifu BuyYifu(String type);
}
package com.shiji.chouxianggongchang;
public class XieziFactory extends Factory {
@Override
Xiezi BuyXiezi(String type) {
// TODO Auto-generated method stub
if (type =="草鞋") {
return new Caoxie();
}else if (type=="拖鞋") {
return new tuoxie();
}
return null;
}
@Override
Yifu BuyYifu(String type) {
// TODO Auto-generated method stub
return null;
}
}
package com.shiji.chouxianggongchang;
public class YifuFactory extends Factory{
@Override
Xiezi BuyXiezi(String type) {
// TODO Auto-generated method stub
return null;
}
@Override
Yifu BuyYifu(String type) {
// TODO Auto-generated method stub
if (type=="衬衫") {
return new Chenshan();
}else if (type=="短袖") {
return new Duanxiu();
}
return null;
}
}
package com.shiji.chouxianggongchang;
public class FactoryDelegate {
Factory CreateFactory(String type)
{
if(type=="鞋子")
{
return new XieziFactory();
}else if (type=="衣服") {
return new YifuFactory();
};
return null;
}
}
public class test {
public static void main(String[] args) {
FactoryDelegate factoryDelegate = new FactoryDelegate();
factoryDelegate.CreateFactory("鞋子").BuyXiezi("草鞋").Run();
factoryDelegate.CreateFactory("鞋子").BuyXiezi("拖鞋").Run();
factoryDelegate.CreateFactory("衣服").BuyYifu("衬衫").Run();
factoryDelegate.CreateFactory("衣服").BuyYifu("短袖").Run();
}
}
———————C++————————————
class Xiezi
{
public:
Xiezi();
virtual void Run() = 0;
~Xiezi();
};
#pragma once
#include "Xiezi.h"
class Tuoxie :
public Xiezi
{
public:
Tuoxie();
virtual void Run()
{
printf("拖鞋");
}
~Tuoxie();
};
class CaoXie :
public Xiezi
{
public:
CaoXie();
virtual void Run()
{
printf("草鞋");
}
~CaoXie();
};
#pragma once
class Yifu
{
public:
Yifu();
virtual void Run()=0;
~Yifu();
};
#pragma once
#include "Yifu.h"
class Chenshan :
public Yifu
{
public:
Chenshan();
virtual void Run() {
printf("衬衫");
}
~Chenshan();
};
#pragma once
#include "Yifu.h"
class DuanXiu :
public Yifu
{
public:
DuanXiu();
virtual void Run()
{
printf("短袖");
}
~DuanXiu();
};
#pragma once
#include "Xiezi.h"
#include "Yifu.h"
enum _Xiezi
{
_CAOXIE=0,
_TUOXIE
};
enum _Yifu
{
_DUANXIU = 0,
_CHENSHAN
};
class Factory
{
public:
Factory();
virtual Xiezi* BuyXiezi(_Xiezi t) = 0;
virtual Yifu* BuyYifu(_Yifu t) = 0;
~Factory();
};
#pragma once
#include "Factory.h"
#include "CaoXie.h"
#include "Tuoxie.h"
class XieziFactory :
public Factory
{
public:
XieziFactory();
virtual Xiezi* BuyXiezi(_Xiezi t) {
if (t== _Xiezi::_CAOXIE)
{
CaoXie* p = new CaoXie();
return p;
}
else if(t == _Xiezi::_TUOXIE)
{
Tuoxie* p = new Tuoxie();
return p;
}
return NULL;
}
virtual Yifu* BuyYifu(_Yifu t)
{
return NULL;
}
~XieziFactory();
};
#pragma once
#include "Factory.h"
#include "DuanXiu.h"
#include "Chenshan.h"
class YifuFactory :
public Factory
{
public:
YifuFactory();
virtual Yifu* BuyYifu(_Yifu t)
{
if (t == _Yifu::_DUANXIU)
{
return new DuanXiu();
}else if (t==_Yifu::_CHENSHAN)
{
return new Chenshan();
}
return NULL;
}
virtual Xiezi* BuyXiezi(_Xiezi t)
{
return NULL;
}
~YifuFactory();
};
#pragma once
#include "Factory.h"
#include "XieziFactory.h"
#include "YifuFactory.h"
enum _Factory{
__XIEZI,
__YIFU
};
class FactoryDelegate
{
public:
FactoryDelegate();
Factory* CreateFactory(_Factory t)
{
if (t == _Factory::__XIEZI)
{
return new XieziFactory();
}else if (t ==_Factory::__YIFU)
{
return new YifuFactory();
}
return NULL;
}
~FactoryDelegate();
};
#include "FactoryDelegate.h"
int main()
{
FactoryDelegate f;
Factory* fac = f.CreateFactory(_Factory::__XIEZI);
Xiezi* x = fac->BuyXiezi(_Xiezi::_CAOXIE);
x->Run();
delete fac;
delete x;
fac = f.CreateFactory(_Factory::__YIFU);
Yifu* y = fac->BuyYifu(_Yifu::_DUANXIU);
y->Run();
delete fac;
delete y;
return 0;
}