⼯⼚模式(C++实现)
  没事捣⿎了下相对⽽⾔⽐较简单的⼯⼚模式,然后写了个⼩Demo,记录下,等⼗年后看到也是种回忆哈.......
  ⼯⼚模式⼀般分为三种:简单⼯⼚模式、⼯⼚⽅法模式、抽象⼯⼚模式。
⼀、简单⼯⼚模式:
  简单⼯⼚模式,⼯⼚类是创建产品的,它决定创建哪⼀种产品,就像领导决定采⽤那种技术⽅案样。举个例⼦,现在有宝马车和奔驰车两种车需要⽣产,但是只有⼀个⼯⼚,且只能在同⼀时间⽣产⼀种车,这时就有⼯⼚决定⽣产那种车了。例⼦虽然不是⼗分恰当,但是会其意即可。我们直接看UML类图和代码吧。
看代码就⼀⽬了然了:
#include <iostream>
using namespace std;
enum CarType{BENZ, BMW};
class Car//车类
{
public:
virtual void createdCar(void) = 0;
};
class BenzCar : public Car //奔驰车
{
public:
BenzCar()
{
cout<<"Benz::Benz()"<<endl;
}
virtual void createdCar(void)
{
cout<<"BenzCar::createdCar()"<<endl;
}
~BenzCar()
{
}
};
class BmwCar : public Car //宝马车
{
public:
BmwCar()
{
cout<<"Bmw::Bmw()"<<endl;
}
virtual void createdCar(void)
{
cout<<"BmwCar::createdCar()"<<endl;
}
};
class CarFactory //车⼚
{
public:
Car* createSpecificCar(CarType type)
{
switch(type)
{
case BENZ://⽣产奔驰车
return (new BenzCar());
break;
case BMW://⽣⾠宝马车
return (new BmwCar());
break;
default:
return NULL;
break;
}
}
};
int main(int argc, char** argv)
{
CarFactory carfac;
Car* specificCarA = ateSpecificCar(BENZ);//看到⽹上众多⽰例在new后没有delete,感觉不是特别严谨
Car* specificCarB = ateSpecificCar(BMW);
delete specificCarA; delete specificCarB;
return0;
}
输出结果如下:
  简单⼯⼚模式在每次增加新的车型时,需要修改⼯⼚类,这就违反了开放封闭原则:软件实体(类、模块、函数)可以扩展,但是不可修改。于是,⼯⼚⽅法模式出现了。
⼆、⼯⼚⽅法模式:
  ⼯⼚⽅法模式:不再只由⼀个⼯⼚类决定那⼀个产品类应当被实例化,这个决定权被交给⼦类去做。当有新的产品(新型汽车)产⽣时,只要按照抽象产品⾓⾊、抽象⼯⼚⾓⾊提供的⽅法来⽣成即可(新车型可以⽤⼀个新类继承创建产品即可),那么就可以被客户使⽤,⽽不必去修改任何已有的代码。可以看出⼯⼚⾓⾊的结构也是符合开闭原则。如下⾯UML类图:
代码如下:
#include <iostream>
using namespace std;
class Car//车类
{
public:
virtual void createdCar(void) = 0;
奔驰与宝马};
class BenzCar : public Car //奔驰车
{
public:
BenzCar()
{
cout<<"Benz::Benz()"<<endl;
}
virtual void createdCar(void)
{
cout<<"BenzCar::createdCar()"<<endl;
}
~BenzCar()
{
}
};
class BmwCar : public Car //宝马车
{
public:
BmwCar()
{
cout<<"Bmw::Bmw()"<<endl;
}
virtual void createdCar(void)
{
cout<<"BmwCar::createdCar()"<<endl;
}
};
class Factory//车⼚
{
public:
virtual Car* createSpecificCar(void) = 0;
};
class BenzFactory : public Factory//奔驰车⼚
{
public:
virtual Car* createSpecificCar(void)
{
return (new BenzCar());
}
};
class BmwFactory : public Factory//宝马车⼚
{
public:
virtual Car* createSpecificCar(void)
{
return (new BmwCar());
}
};
int main(int argc, char** argv)
{
Factory* factory = new BenzFactory();
Car* specificCarA = factory->createSpecificCar();
factory = new BmwFactory();
Car* specificCarB = factory->createSpecificCar();
delete factory; delete specificCarA; delete specificCarB;
return0;
}
三、抽象⼯⼚:
  在上⾯的⼯⼚⽅法模式基础上,有需要⽣产⾼配版的奔驰和宝马,那⼯⼚⽅法模式就有点鞭长莫及了,这就⼜有抽象⼯⼚模式,UML类图如下:
代码如下:
#include <iostream>
using namespace std;
class Car//车类
{
public:
virtual void createdCar(void) = 0;
};
class BenzCar : public Car //奔驰车
{
public:
BenzCar()
{
cout<<"Benz::Benz()"<<endl;
}
virtual void createdCar(void)
{
cout<<"BenzCar::createdCar()"<<endl;
}
~BenzCar()
{
}
};
class BmwCar : public Car //宝马车
{
public:
BmwCar()
{
cout<<"Bmw::Bmw()"<<endl;
}
virtual void createdCar(void)
{
cout<<"BmwCar::createdCar()"<<endl;
}
};
class HighCar //⾼配版车型
{
public:
virtual void createdCar(void) = 0;
};
class HighBenzCar : public HighCar //⾼配奔驰车
{
public:
HighBenzCar()
{
cout<<"HighBenzCarBenz::Benz()"<<endl;
}
virtual void createdCar(void)
{
cout<<"HighBenzCar::createdCar()"<<endl;
}
};
class HighBmwCar : public HighCar //⾼配宝马车
{
public:
HighBmwCar()
{
cout<<"HighBmwCar::Bmw()"<<endl;
}
virtual void createdCar(void)
{
cout<<"HighBmwCar::createdCar()"<<endl;
}
};
class Factory//车⼚
{
public:
virtual Car* createSpecificCar(void) = 0;
virtual HighCar* createdSpecificHighCar(void) = 0;
};
class BenzFactory : public Factory//奔驰车⼚
{
public:
virtual Car* createSpecificCar(void)
{
return (new BenzCar());
}
virtual HighCar* createdSpecificHighCar(void)
{
return (new HighBenzCar());
}
};
class BmwFactory : public Factory//宝马车⼚
{
public:
virtual Car* createSpecificCar(void)
{
return (new BmwCar());
}
virtual HighCar* createdSpecificHighCar(void)
{
return (new HighBmwCar());
}
};
int main(int argc, char** argv)
{
Factory* factory = new BenzFactory();
Car* specificCar = factory->createSpecificCar();
HighCar* spcificHighCar = factory->createdSpecificHighCar(); delete factory; delete specificCar; delete spcificHighCar; return0;
}
输出结果如下:
  以上代码在VC6.0上运⾏OK。