设计模式-工厂模式
作者:
也许
,
2022-05-22 21:07:40
,
所有人可见
,
阅读 237
简单工厂模式
class Fruit
{
public:
virtual void getFruit() = 0;
protected:
private:
};
class Banana : public Fruit
{
public:
virtual void getFruit()
{
cout<<"香蕉"<<endl;
}
protected:
private:
};
class Pear : public Fruit
{
public:
virtual void getFruit()
{
cout<<"梨子"<<endl;
}
protected:
private:
};
class Factory
{
public:
static Fruit* Create(char *name)
{
Fruit *tmp = NULL;
if (strcmp(name, "pear") == 0)
{
tmp = new Pear();
}
else if (strcmp(name, "banana") == 0)
{
tmp = new Banana();
}
else
{
return NULL;
}
return tmp;
}
protected:
private:
};
void main()
{
Fruit *pear = Factory::Create("pear");
if (pear == NULL)
{
cout<<"创建pear失败\n";
}
pear->getFruit();
Fruit *banana = Factory::Create("banana");
banana->getFruit();
system("pause");
}
工厂模式
简单工厂模式,当有新的对象需要创建时,需要修改factory类的方法,不符合开闭原则
工厂模式:当系统扩展需要添加新的产品对象时,仅仅需要添加一个具体对象以及一个具体工厂对象,原有工厂对象不需要
进行任何修改
#include "iostream"
using namespace std;
class Fruit
{
public:
virtual void sayname()
{
cout<<"fruit\n";
}
};
class FruitFactory
{
public:
virtual Fruit* getFruit()
{
return new Fruit();
}
};
class Banana : public Fruit
{
public:
virtual void sayname()
{
cout<<"Banana \n"<<endl;
}
};
class BananaFactory : public FruitFactory
{
public:
virtual Fruit* getFruit()
{
return new Banana;
}
};
class Apple : public Fruit
{
public:
virtual void sayname()
{
cout<<"Apple \n"<<endl;
}
};
class AppleFactory : public FruitFactory
{
public:
virtual Fruit* getFruit()
{
return new Apple;
}
};
void main()
{
FruitFactory * ff = NULL;
Fruit *fruit = NULL;
ff = new BananaFactory();
fruit = ff->getFruit();
fruit->sayname();
delete fruit;
delete ff;
ff = new AppleFactory();
fruit = ff->getFruit();
fruit->sayname();
delete fruit;
delete ff;
cout<<"hello....\n";
system("pause");
}
抽象工厂模式
工厂模式只能生产一个产品。(要么香蕉、要么苹果)
抽象工厂可以一下生产一个产品族(里面有很多产品组成)
class Fruit
{
public:
virtual void sayname()
{
cout<<"fruit\n";
}
};
class FruitFactory
{
public:
virtual Fruit* getApple()
{
return new Fruit();
}
virtual Fruit* getBanana()
{
return new Fruit();
}
};
class SouthBanana : public Fruit
{
public:
virtual void sayname()
{
cout<<"South Banana \n"<<endl;
}
};
class SouthApple : public Fruit
{
public:
virtual void sayname()
{
cout<<"South Apple \n"<<endl;
}
};
class NorthBanana : public Fruit
{
public:
virtual void sayname()
{
cout<<"North Banana \n"<<endl;
}
};
class NorthApple : public Fruit
{
public:
virtual void sayname()
{
cout<<"North Apple \n"<<endl;
}
};
class SourthFruitFactory : public FruitFactory
{
public:
virtual Fruit* getApple()
{
return new SouthApple();
}
virtual Fruit* getBanana()
{
return new SouthBanana();
}
};
class NorthFruitFactory : public FruitFactory
{
public:
virtual Fruit* getApple()
{
return new NorthApple();
}
virtual Fruit* getBanana()
{
return new NorthBanana();
}
};
void main()
{
FruitFactory * ff = NULL;
Fruit *fruit = NULL;
ff = new SourthFruitFactory();
fruit = ff->getApple();
fruit->sayname();
fruit = ff->getBanana();
fruit->sayname();
delete fruit;
delete ff;
ff = new NorthFruitFactory();
fruit = ff->getApple();
fruit->sayname();
fruit = ff->getBanana();
fruit->sayname();
delete fruit;
delete ff;
cout<<"hello....\n";
system("pause");
}