#ifndef Animal_hpp #define Animal_hpp #define CC_SYNTHESIZE(varType, varName, funName)\ protected: varType varName;\ public: virtual varType get##funName(void) const { return varName; }\ public: virtual void set##funName(varType var){ varName = var; } #define CC_CONSTRUCTED(classType)\ public: classType() {} \ public: ~classType() {} #define CC_CREATECLASS(classType)\ new classType(); #include <stdio.h> #include <iostream> using namespace std; class Animal { public: CC_CONSTRUCTED(Animal); virtual void say(); CC_SYNTHESIZE(string, type, Type); }; #endif /* Animal_hpp */
#include "Animal.hpp" void Animal::say() { printf("%sAnimal,*****say\n" , this->getType().c_str()); }
#ifndef Cat_hpp #define Cat_hpp #include <stdio.h> #include "Animal.hpp" class Cat: public Animal { public: CC_CONSTRUCTED(Cat); virtual void say(); }; #endif /* Cat_hpp */
#include "Cat.hpp" void Cat::say() { printf("%scat,*****say" , this->getType().c_str()); }
#ifndef Dog_hpp #define Dog_hpp #include <stdio.h> #include "Animal.hpp" class Dog: public Animal { public: CC_CONSTRUCTED(Dog); virtual void say(); }; #endif /* Dog_hpp */
#include "Dog.hpp" void Dog::say() { printf("%sDog,*****say" , this->getType().c_str()); }
#ifndef CatFemale_hpp #define CatFemale_hpp #include <stdio.h> #include "Animal.hpp" class CatFemale: public Animal { public: CC_CONSTRUCTED(CatFemale); }; #endif /* CatFemale_hpp */
#ifndef CatMale_hpp #define CatMale_hpp #include <stdio.h> #include "Animal.hpp" class CatMale: public Animal { public: CC_CONSTRUCTED(CatMale); }; #endif /* CatMale_hpp */
#ifndef DogFemale_hpp #define DogFemale_hpp #include <stdio.h> #include "Animal.hpp" class DogFemale: public Animal { public: CC_CONSTRUCTED(DogFemale); }; #endif /* DogFemale_hpp */
#ifndef DogMale_hpp #define DogMale_hpp #include <stdio.h> #include "Animal.hpp" class DogMale: public Animal { public: CC_CONSTRUCTED(DogMale); }; #endif /* DogMale_hpp */
#ifndef Creater_hpp #define Creater_hpp #include <stdio.h> #include "Animal.hpp" enum Type{ MType , FType }; enum ALL{ MDOG, FDOG, MCAT, FCAT }; class Creater { public: Creater() {dog = NULL;cat = NULL;}; ~Creater() {}; virtual Animal* createCat() {return nullptr;}; virtual Animal* createDog() {return nullptr;}; CC_SYNTHESIZE(Animal*, dog, Dog); CC_SYNTHESIZE(Animal*, cat, Cat); CC_SYNTHESIZE(Animal*, animal, Animal); virtual Animal* createDogAnything(Type type); virtual Animal* createCatAnything(Type type); virtual Animal* createAny(ALL type); }; #endif /* Creater_hpp */
#include "Creater.hpp" #include "DogFemale.hpp" #include "CatFemale.hpp" #include "CatMale.hpp" #include "DogMale.hpp" Animal* Creater::createDogAnything(Type type) { dog = nullptr; switch (type) { case MType: dog = CC_CREATECLASS(DogMale); dog->setType("母狗"); break; case FType: dog = CC_CREATECLASS(DogFemale); dog->setType("公狗"); break; default: dog = CC_CREATECLASS(DogFemale); dog->setType("公共狗"); break; } return dog; } Animal* Creater::createCatAnything(Type type) { cat = nullptr; switch (type) { case MType: cat = CC_CREATECLASS(CatMale); cat->setType("母猫"); break; case FType: cat = CC_CREATECLASS(CatFemale); cat->setType("公猫"); break; default: cat = CC_CREATECLASS(CatFemale); cat->setType("公共猫"); break; } return cat; } Animal* Creater::createAny(ALL type) { switch (type) { case MCAT: animal = CC_CREATECLASS(CatMale); break; case MDOG: animal = CC_CREATECLASS(DogMale); break; case FCAT: animal = CC_CREATECLASS(CatFemale); break; case FDOG: animal = CC_CREATECLASS(DogFemale); break; default: animal = CC_CREATECLASS(DogFemale); break; } animal->setType("全部"); return animal; }
#ifndef FCreater_hpp #define FCreater_hpp #include <stdio.h> #include "Creater.hpp" class FCreater: public Creater { public: CC_CONSTRUCTED(FCreater); virtual Animal* createCat(); virtual Animal* createDog(); static Creater* getInsatane(); }; #endif /* FCreater_hpp */
#include "FCreater.hpp" #include "CatFemale.hpp" #include "DogFemale.hpp" Animal* FCreater::createCat() { if (!this->getCat()) { this->setCat(new CatFemale()); this->getCat()->setType("公猫"); } return this->getCat(); } Animal* FCreater::createDog() { if (!this->getDog()) { this->setDog(new DogFemale()); this->getDog()->setType("公狗"); } return this->getDog(); }
#ifndef MCreater_hpp #define MCreater_hpp #include <stdio.h> #include "Creater.hpp" class MCreater: public Creater { public: CC_CONSTRUCTED(MCreater); virtual Animal* createCat(); virtual Animal* createDog(); }; #endif /* MCreater_hpp */
#include "MCreater.hpp" #include "DogMale.hpp" #include "CatMale.hpp" Animal* MCreater::createCat() { if (!this->getCat()) { this->setCat(new CatMale()); this->getCat()->setType("母猫"); } return this->getCat(); } Animal* MCreater::createDog() { if (!this->getDog()) { this->setDog(new DogMale()); this->getDog()->setType("母狗"); } return this->getDog(); }
/* 抽象工厂 */ #include <iostream> #include "Creater.hpp" #include "MCreater.hpp" #include "FCreater.hpp" #include "Animal.hpp" int main(int argc, const char * argv[]) { // insert code here... std::cout << "Hello, 抽象工厂!\n"; std::cout<<"母的\n"; MCreater* mcreater = new MCreater(); mcreater->createCat()->say(); mcreater->createDog()->say(); std::cout<<"公的\n"; FCreater* fcreater = new FCreater(); fcreater->createCat()->say(); fcreater->createDog()->say(); std::cout<<"createCatAnything\n"; fcreater->createCatAnything(MType)->say(); fcreater->createDogAnything(FType)->say(); std::cout<<"createAny\n"; Creater* create = CC_CREATECLASS(Creater); create->createAny(MDOG)->say(); create->createAny(MCAT)->say(); create->createAny(FCAT)->say(); create->createAny(FDOG)->say(); return 0; }
//个人认为业务不够庞大的情况下不需要用抽象工厂模式,在使用抽象工厂时评估一下业务大小,否则则想我代码这样 其实用工厂就已经足够了的,以上代码使用工厂模式较为简洁,当然也同时列举出抽象工厂的写法啦 大家一起学习喔