设计模式(C++) 练习1 简单工厂模式

设计模式(C++) 练习1 简单工厂模式

Design Patterns(C++) Exercise 1:Easy Factory Model

Exercise 1.1

一个农场专门负责种植各种水果,比如苹果、草莓、葡萄等,任何水果都可以种植、生长和采摘.此外,每一种水果还有自己的特性,如:苹果是多年生具有树龄的属性,葡萄则分为有籽或无籽等。农场生产的水果都送入超市进行销售。请采用简单工厂模式对上述问题进行建模要求画出类图并使用C++语言编写程序。
设计模式(C++) 练习1 简单工厂模式

/** 
 *	@author 墨晓柯
 */
 
#include<iostream>

#define interface class

using namespace std;

interface IFruit {
public: 
	virtual void plant() = 0; 
	virtual void grow() = 0; 
	virtual void harvest() = 0;
}; 

class Apple : public IFruit{
private:
	int treeAge;
public:	
	void setTreeAge(int aTreeAge) {
		treeAge = aTreeAge;
	} 
	int getTreeAge() {
		return treeAge;
	}
	void plant() { 
		cout << "Apple planted!" << endl;
	}
	void grow() { 
		cout << "Apple growing!" << endl;
	}
	void harvest() {
		cout << "Apple harvested!" << endl;
	}	
};

class Strawberry : public IFruit {
public:
	void plant() {
		cout << "Strawberry planted!" << endl;
	}
	void grow() {
		cout << "Strawberry growing!" << endl;
	}
	void harvest() {
		cout << "Strawberry harvested!" << endl;
	}
};

class Grape : public IFruit {	
private:
	bool seedful;
public:	
	void setSeedful(bool hasSeed) { 
		seedful = hasSeed;
	} 
	bool getSeedful() { 
		return seedful;
	}
	void plant() { 
		cout << "Grape planted!" << endl;
	}
	void grow() { 
		cout << "Grape growing!" << endl;
	}
	void harvest() { 
		cout << "Grape harvested!" << endl;
	}
};

class Gardener {
public:
	static IFruit* fruitFactory(string fruitName) {
		if("apple" == fruitName) 
			return new Apple();
		else if("strawberry" == fruitName) 
			return new Strawberry();
		else if("grape" == fruitName) 
		 	return new Grape();
		else
			return NULL;
	}
};

int main(void) {	
	IFruit *fruit1 = Gardener::fruitFactory("apple");
	fruit1 -> plant();
	fruit1 -> grow();
	fruit1 -> harvest();
	IFruit *fruit2 = Gardener::fruitFactory("strawberry");
	fruit2 -> plant();
	fruit2 -> grow();
	fruit2 -> harvest();	
	IFruit *fruit3 = Gardener::fruitFactory("grape");
	fruit3 -> plant();
	fruit3 -> grow();
	fruit3 -> harvest();
	
	return 0;
}

Exercise 1.2

“俗说天地开辟,未有人民,女娲抟黄土为人”,女娲需要用黄土造出一个个人,如张三、李四王五等。采用简单工厂模式对上述问题进行建模要求画出类图并使用C++语言编写程序。
设计模式(C++) 练习1 简单工厂模式

/** 
 *	@author 墨晓柯
 */
  
#include<iostream>

#define interface class

using namespace std;

interface IHuman {
public: 
	virtual void eat() = 0; 
	virtual void sleep() = 0; 
	virtual void speak() = 0;
}; 

class ZhangSan : public IHuman{
public:	
	void eat() {
		cout << "ZhangSan eat." << endl;
	}
	void sleep() {
		cout << "ZhangSan sleep." << endl;
	}
	void speak() {
		cout << "ZhangSan speak." << endl;
	}
};

class LiSi: public IHuman {
public:
	void eat() {
		cout << "LiSi eat." << endl;
	}
	void sleep() {
		cout << "LiSi sleep." << endl;
	}
	void speak() {
		cout << "LiSi speak." << endl;
	}
};

class WangWu : public IHuman {	
public:	
	void eat() {
		cout << "WangWu eat." << endl;
	}
	void sleep() {
		cout << "WangWu sleep." << endl;
	}
	void speak() {
		cout << "WangWu speak." << endl;
	}
};

class Nuwa {
public:
	static IHuman* humanFactory(string name) {
		if("zhangSan" == name) 
			return new ZhangSan();	
		else if("liSi" == name) 
			return new LiSi();	
		else if("wangWu" == name) 
			return new WangWu();
		else 
			return NULL;
	}
};

int main(void) {	
	IHuman *zhangSan = Nuwa::humanFactory("zhangSan");
	zhangSan -> eat();
	zhangSan -> sleep();
	zhangSan -> speak();
	IHuman *liSi = Nuwa::humanFactory("liSi");
	liSi -> eat();
	liSi -> sleep();
	liSi -> speak();	
	IHuman *wangWu = Nuwa::humanFactory("wangWu");
	wangWu -> eat();
	wangWu -> sleep();
	wangWu -> speak();
 
	return 0;
}