设计模式-AbstractFactory模式戏说
1. 将Factory模式中的抽象基类替换成接口类,不同在于抽象基类不能实例化,而接口类可以。
比如比如将汽车类替换成汽车4S店,
class 汽车4S店
{
public:
汽车4S店();
vitrual ~汽车4S店();
virtual 轮胎* 轮胎()const {return new 轮胎;}
virtual 车轱辘* 车轱辘()const {return new 车轱辘;}
virtual 导弹* 导弹()const {return new 导弹;}
}
class 我的车 : public 汽车4S店
{
public:
我的车();
~我的车();
virtual 轮胎* 轮胎()const {return new 吉利;}
virtual 车轱辘* 车轱辘()const {return new 东风车轱辘;}
virtual 导弹* 导弹()const {return new 地对空导弹;}
}
class 老婆的车 : public 汽车4S店
{
public:
老婆的车();
~老婆的车();
virtual 轮胎* 轮胎()const {return new 米琪琳;}
virtual 车轱辘* 车轱辘()const {return new 韩国车轱辘;}
virtual 导弹* 导弹()const {return new 反导防御;}
}
class 童车 : public 汽车4S店
{
public:
童车();
~童车();
virtual 轮胎* 轮胎()const {return new 吉利;}
virtual 车轱辘* 车轱辘()const {return new 东风车轱辘;}
virtual 导弹* 导弹()const {return new 反导防御;}
}