概述
一、示例结构
二、示例代码
cat.h
#pragma once
#include "productBase.h"
class cat : public productBase
{
public:
cat();
~cat();
virtual void printProductInfo();
virtual void printEatFood();
void catVoice();
};
dog.h
#pragma once
#include "productBase.h"
class dog :public productBase
{
public:
dog();
~dog();
virtual void printProductInfo();
virtual void printEatFood();
void dogVoice();
};
animalFactory.h
#pragma once
#include "productBase.h"
class animalFactory
{
public:
animalFactory();
~animalFactory();
productBase* createAnimal(productsType type);
};
productBase.h
#pragma once
#include <iostream>
//抽象类
enum productsType
{
_cat,
_dog,
_tiger,
_monkey,
};
class productBase
{
public:
productBase();
~productBase();
virtual void printProductInfo() = 0;
virtual void printEatFood() = 0;
};
cat.cpp
#include "cat.h"
cat::cat()
{
std::cout << "cat::cat()nn";
}
cat::~cat()
{
std::cout << "cat::~cat()nn";
}
void cat::printProductInfo()
{
std::cout << "This is new cat!nn";
}
void cat::printEatFood()
{
std::cout << "cat eat fish.nn";
}
void cat::catVoice()
{
std::cout << "cat voice: miao~miao~miao~nn";
}
dog.cpp
#include "dog.h"
dog::dog()
{
std::cout << "dog::dog()nn";
}
dog::~dog()
{
std::cout << "dog::~dog()nn";
}
void dog::printProductInfo()
{
std::cout << "This is new dog!nn";
}
void dog::printEatFood()
{
std::cout << "dog eat bone.nn";
}
void dog::dogVoice()
{
std::cout << "dog voice: wang~wang~wang~nn";
}
animalFactory.cpp
#include "animalFactory.h"
#include "cat.h"
#include "dog.h"
animalFactory::animalFactory(){}
animalFactory::~animalFactory(){}
productBase* animalFactory::createAnimal(productsType type)
{
switch (type)
{
case _cat:
return (new cat());
break;
case _dog:
return (new dog());
break;
case _tiger:
break;
case _monkey:
break;
default:
break;
}
}
main.cpp
#include <iostream>
#include "animalFactory.h"
#include "cat.h"
#include "dog.h"
int main()
{
animalFactory factory;
productBase* newCat = factory.createAnimal(_cat); //基类的指针指向子类的对象,先构造基类,再构造子类
productBase* newDog = factory.createAnimal(_dog); //基类的指针指向子类的对象,先构造基类,再构造子类
newCat->printProductInfo(); //指向子类对象的基类指针 调用基类的纯虚方法,实则调用的是子类实现的方法(即多态)
newCat->printEatFood(); //指向子类对象的基类指针 调用基类的纯虚方法,实则调用的是子类实现的方法(即多态
newDog->printProductInfo(); //同上
newDog->printEatFood();
cat* m_cat = new cat(); //正常情况下,new子类的对象实例,构造顺序:先构造基类,再构造子类
m_cat->catVoice(); //正常情况下,调用对象的方法
m_cat->printEatFood(); //正常情况下,调用对象的方法
dog* m_dog = new dog(); //正常情况下,new对象实例,构造顺序:先构造基类,再构造子类
m_dog->dogVoice(); //正常情况下,调用对象的方法
delete newCat; //正常析构基类对象
newCat = NULL;
delete newDog; //正常析构基类对象
newDog = NULL;
delete m_cat; //析构子类时,其顺序与构造时相反:先析构子类,再析构基类
m_cat = NULL;
delete m_dog; //析构子类时,其顺序与构造时相反:先析构子类,再析构基类
m_dog = NULL;
}
productBase.cpp
#include "productBase.h"
productBase::productBase()
{
std::cout << "productBase::productBase()nn";
}
productBase::~productBase()
{
std::cout << "productBase::~productBase()nn";
}
三、更多参考
https://blog.csdn.net/silangquan/article/details/20492293
最后
以上就是受伤蚂蚁为你收集整理的c++ 简单工厂模式 示例的全部内容,希望文章能够帮你解决c++ 简单工厂模式 示例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复