您的位置:首页 > 其它

简单工厂模式(Simple Factory Pattern)

2017-03-13 18:40 543 查看
简单工厂模式是编程开发最常用的设计模式之一,它属于创建型设计模式,它提供一种创建对象的一种方法。

专门负责创建类对象的类叫做工厂类,所创建的对象对象的类叫做产品类。

简单工厂模式的本质是由工厂类的对象根据传入的参数决定创建哪一种产品类的对象。

简单的用uml类图表示如下:



Product一般是所有产品的接口或抽象类,作为其他产品的父类。ProductA,ProductB,ProductC是泛化出的具体的Product。这些具体的产品,由ProductFactory负责创建其实例,具体是由方法createProduct方法接收的参数,决定创建哪一种产品。

下面看一个实例

Java版:

People.java(不同国家的people的接口(interface))

public interface People{ //一共五个方法
void speak();
void eat();
void sleep();
void show();
}
Chinese.java

public class Chinese implements People{
public void speak(){
System.out.println("我是中国人,我说汉语");
}
public void eat(){
System.out.println("中国人吃馒头和米饭");
}
public void sleep(){
System.out.println("全世界都睡了,中国都没睡");
}
public void show(){
System.out.println("我是中国人,我爱中国!");
}

}
American.java

public class American implements People{
public void speak(){
System.out.println("I am an American, I speak english");
}
public void eat(){
System.out.println("American eat pizza");
}
public void sleep(){
System.out.println("American asleep");
}
public void show(){
System.out.println("I am an American,I love America!");
}

}
Japanese.java

public class Japanese implements People{
public void speak(){
System.out.println("我日本人,我说日语");
}
public void eat(){
System.out.println("我是日本人,我吃shi");//
}
public void sleep(){
System.out.println("日本人睡了");
}
public void show(){
System.out.println("我是日本人,我爱日本!");
}

}
以上三个类分别继承了People接口,实现了所有的方法。

下面这个类就是工厂类,提供了一个createPeople方法,根据不同的参数创建不同的Peopel

值的一提的是利用Java中反射的机制,可以方便的根据类名获取相应的类对象

PeopleFactory.java

public class PeopleFactory{

public static People createPeople(String pcategory){
People p=null;
try{
p = (People)Class.forName(pcategory).newInstance();
}catch(Exception e){
System.out.println(e);
}finally{
return p;
}
}

}


下面是一个测试类

Test.java

public class Test{

public static void main(String []args){
People chi = PeopleFactory.createPeople("Chinese");
chi.eat();
chi.sleep();
chi.show();
chi.speak();

People american = PeopleFactory.createPeople("American");
american.eat();
american.sleep();
american.show();
american.speak();

People japan = PeopleFactory.createPeople("Japanese");
japan.eat();
japan.sleep();
japan.show();
japan.speak();
}
}
测试结果就不贴了。。。

C++版:

peoplebase.h(所有people的基类,被定义为抽象类)

#ifndef PEOPLE_H_
#define PEOPLE_H_

#include <iostream>
using std::string;
class People
{
private:
string skinColor;
public:
People(){ }
People(string skinColor){ this->skinColor = skinColor; }
virtual void eat() = 0;
virtual void sleep() = 0;
virtual void show() = 0;
virtual void speak() = 0;
string getColor(){ return skinColor; }
virtual ~People(){ }
};

#endif

Chinese.h
#ifndef CHINESE_H_
#define CHINESE_H_

#include "peoplebase.h"
using std::cout;
using std::endl;
class Chinese:public People
{
public:
Chinese():People("黄色"){ }
void eat(){ cout<<"中国人喜欢吃馒头和米饭"<<endl; }
void sleep(){ cout<<"全世界都睡了,中国人还没睡"<<endl; }
void speak(){ cout<<"我是中国人,我说汉语!"<<endl; }
void show(){ cout<<"中国人的肤色是"<<getColor()<<endl; }
~Chinese(){ }
};

#endif

American.h

#ifndef AMERICAN_H_
#define AMERICAN_H_

#include "peoplebase.h"
using std::cout;
using std::endl;
class American:public People
{
public:
American():People("white and black"){		}
void eat(){ cout<<"American like eat pizza"<<endl;	}
void sleep(){	cout<<"American asleep"<<endl;	}
void speak(){	cout<<"I am an American,I speak English"<<endl;	}
void show(){	cout<<"American skin color is "<<this->getColor()<<endl; }
~American(){	}
};

#endif
japanese.h

#ifndef JAPANESE_H_
#define JAPANESE_H_

#include "peoplebase.h"
using std::cout;
using std::endl;
class Japanese:public People
{
public:
Japanese():People("黄色"){		}
void eat(){ cout<<"日本人喜欢吃shi"<<endl;	}
void sleep(){	cout<<"日本人睡了"<<endl;	}
void speak(){	cout<<"我是日本人,我说日语!"<<endl;	}
void show(){	cout<<"日本人的肤色是"<<this->getColor()<<endl; }
~Japanese(){	}
};

#endif


peoplefactory.h

#ifndef PEOPLEFACTORY_H_
#define PEOPLEFACTORY_H_

#include "peoplebase.h"
class PeopleFactory
{
public:
PeopleFactory(){ }
static People* createPeople(std::string pcategory);

};
People* PeopleFactory::createPeople(std::string pcategory)
{
People* p = nullptr;
if(pcategory=="Chinese")
p = new Chinese();
else if(pcategory=="American")
p = new American();
else if(pcategory=="Japanese")
p = new Japanese();
else
p = nullptr;
return p;
//c++ 不支持string switch case....
/*switch(pcategory)
{
case "Chineses":
p = new Chineses();break;
case "American":
p = new American();break;
case "Japanese":
p = new Japanese();break;
default:
p = nullptr;
break;
}*/
}
#endif
需要说明的是C++不支持反射机制,只能使用if ...else ....语句实现类似反射的功能(并且C++不支持string switch case)

main.cpp

#include "chineses.h"
#include "american.h"
#include "japanese.h"
#include "peoplefactory.h"

int main()
{
//People *chi = new Chinese();
People *chi = PeopleFactory::createPeople("Chinese");//通过PeopelFactory创建Chinese
chi->eat();
chi->show();
chi->sleep();
chi->speak();

//People *japan = new Japanese();
People *japan = PeopleFactory::createPeople("Japanese");//通过PeopelFactory创建Japanese
japan->eat();
japan->show();
japan->sleep();
japan->speak();

//People *american = new American();
People *american = PeopleFactory::createPeople("American");//通过PeopleFactory创建American
american->eat();
american->show();
american->sleep();
american->speak();

delete chi;
delete japan;
delete american;
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: