//工厂方法模式
/*
简单工厂模式 + 开闭原则 = 工厂方法模式
工厂方法模式的优缺点
优点:
1)不需要记住具体类名,甚至连具体参数都不用记忆。
2)实现了对象创建和使用的分离。
3)系统的可扩展性也就变得非常好,无需修改接口和原类。
缺点:
1)增加系统中类的个数,复杂度和理解度增加。
2)增加了系统的抽象性和理解难度
*/
# include <iostream>
# include <string>
using namespace std;
//抽象水果
class AbstractFruit
{
public:
virtual void ShowName() = 0;
};
//苹果
class Apple : public AbstractFruit
{
public:
virtual void ShowName()
{
cout << "我是苹果!" << endl;
}
};
//香蕉
class Banana : public AbstractFruit
{
public:
virtual void ShowName()
{
cout << "我是香蕉!" << endl;
}
};
//鸭梨
class Pear : public AbstractFruit
{
public:
virtual void ShowName()
{
cout << "我是鸭梨!" << endl;
}
};
//抽象水果工厂
class AbstractFruitFactory
{
public:
virtual AbstractFruit* CreatFruit() = 0;
};
//苹果工厂
class AppleFactory : public AbstractFruitFactory
{
virtual AbstractFruit* CreatFruit()
{
return new Apple;
}
};
//香蕉工厂
class BananaFactory: public AbstractFruitFactory
{
virtual AbstractFruit* CreatFruit()
{
return new Banana;
}
};
//鸭梨工厂
class PearFactory: public AbstractFruitFactory
{
virtual AbstractFruit* CreatFruit()
{
return new Pear;
}
};
void test01()
{
AbstractFruitFactory* factory = NULL;
AbstractFruit *fruit = NULL;
//创建一个苹果工厂
factory = new AppleFactory;
fruit = factory->CreatFruit();
fruit->ShowName();
delete fruit;
delete factory;
//创建一个香蕉工厂
factory = new BananaFactory;
fruit = factory->CreatFruit();
fruit->ShowName();
delete fruit;
delete factory;
//创建一个鸭梨工厂
factory = new PearFactory;
fruit = factory->CreatFruit();
fruit->ShowName();
delete fruit;
delete factory;
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
/*
简单工厂模式 + 开闭原则 = 工厂方法模式
工厂方法模式的优缺点
优点:
1)不需要记住具体类名,甚至连具体参数都不用记忆。
2)实现了对象创建和使用的分离。
3)系统的可扩展性也就变得非常好,无需修改接口和原类。
缺点:
1)增加系统中类的个数,复杂度和理解度增加。
2)增加了系统的抽象性和理解难度
*/
# include <iostream>
# include <string>
using namespace std;
//抽象水果
class AbstractFruit
{
public:
virtual void ShowName() = 0;
};
//苹果
class Apple : public AbstractFruit
{
public:
virtual void ShowName()
{
cout << "我是苹果!" << endl;
}
};
//香蕉
class Banana : public AbstractFruit
{
public:
virtual void ShowName()
{
cout << "我是香蕉!" << endl;
}
};
//鸭梨
class Pear : public AbstractFruit
{
public:
virtual void ShowName()
{
cout << "我是鸭梨!" << endl;
}
};
//抽象水果工厂
class AbstractFruitFactory
{
public:
virtual AbstractFruit* CreatFruit() = 0;
};
//苹果工厂
class AppleFactory : public AbstractFruitFactory
{
virtual AbstractFruit* CreatFruit()
{
return new Apple;
}
};
//香蕉工厂
class BananaFactory: public AbstractFruitFactory
{
virtual AbstractFruit* CreatFruit()
{
return new Banana;
}
};
//鸭梨工厂
class PearFactory: public AbstractFruitFactory
{
virtual AbstractFruit* CreatFruit()
{
return new Pear;
}
};
void test01()
{
AbstractFruitFactory* factory = NULL;
AbstractFruit *fruit = NULL;
//创建一个苹果工厂
factory = new AppleFactory;
fruit = factory->CreatFruit();
fruit->ShowName();
delete fruit;
delete factory;
//创建一个香蕉工厂
factory = new BananaFactory;
fruit = factory->CreatFruit();
fruit->ShowName();
delete fruit;
delete factory;
//创建一个鸭梨工厂
factory = new PearFactory;
fruit = factory->CreatFruit();
fruit->ShowName();
delete fruit;
delete factory;
}
int main(int argc, char *argv[])
{
test01();
return 0;
}