/*
抽象工厂针对的是产品族,而不是产品
等级结构
*/
# include <iostream>
using namespace std;
//抽象苹果
class AbstractApple
{
public:
virtual void ShowName() = 0;
};
//中国苹果
class ChinaApple : public AbstractApple
{
public:
virtual void ShowName()
{
cout << "中国苹果" << endl;
}
};
//美帝苹果
class USAApple : public AbstractApple
{
public:
virtual void ShowName()
{
cout << "美帝苹果" << endl;
}
};
//倭国苹果
class JapanApple : public AbstractApple
{
public:
virtual void ShowName()
{
cout << "倭国苹果" << endl;
}
};
//抽象的香蕉
class AbstractBanana
{
public:
virtual void ShowName() = 0;
};
//中国香蕉
class ChinaBanana : public AbstractBanana
{
virtual void ShowName()
{
cout << "中国香蕉" << endl;
}
};
//美帝香蕉
class USABanana : public AbstractBanana
{
virtual void ShowName()
{
cout << "美帝香蕉" << endl;
}
};
//倭国苹果
class JapanBanana : public AbstractBanana
{
public:
virtual void ShowName()
{
cout << "倭国香蕉" << endl;
}
};
//抽象的鸭梨
class AbstractPear
{
public:
virtual void ShowName() = 0;
};
//中国鸭梨
class ChinaPear : public AbstractPear
{
public:
virtual void ShowName()
{
cout << "中国鸭梨" << endl;
}
};
//美帝鸭梨
class USAPear : public AbstractPear
{
public:
virtual void ShowName()
{
cout << "美帝鸭梨" << endl;
}
};
//倭国鸭梨
class JapanPear : public AbstractPear
{
public:
virtual void ShowName()
{
cout << "倭国鸭梨" << endl;
}
};
//抽象工厂 针对产品族
class AbstractFactory
{
public:
virtual AbstractApple* CreatApple() = 0;
virtual AbstractBanana* CreatBanana() = 0;
virtual AbstractPear* CreatPear() = 0;
};
//中国工厂
class ChinaFactory : public AbstractFactory
{
virtual AbstractApple* CreatApple()
{
return new ChinaApple;
}
virtual AbstractBanana* CreatBanana()
{
return new ChinaBanana;
}
virtual AbstractPear* CreatPear()
{
return new ChinaPear;
}
};
//美帝工厂
class USAFactory : public AbstractFactory
{
virtual AbstractApple* CreatApple()
{
return new USAApple;
}
virtual AbstractBanana* CreatBanana()
{
return new USABanana;
}
virtual AbstractPear* CreatPear()
{
return new USAPear;
}
};
//倭国工厂
class JapanFactory : public AbstractFactory
{
virtual AbstractApple* CreatApple()
{
return new JapanApple;
}
virtual AbstractBanana* CreatBanana()
{
return new JapanBanana;
}
virtual AbstractPear* CreatPear()
{
return new JapanPear;
}
};
void test01()
{
AbstractFactory* factory = NULL;
AbstractApple* apple = NULL;
AbstractBanana* banana = NULL;
AbstractPear* pear = NULL;
//构建一个中国的产地
factory = new ChinaFactory;
apple = factory->CreatApple();
banana = factory->CreatBanana();
pear = factory->CreatPear();
apple->ShowName();
banana->ShowName();
pear->ShowName();
delete pear;
delete banana;
delete apple;
delete factory;
}
int main(int argc, char *argv[])
{
test01();
return 0;
}
抽象工厂针对的是产品族,而不是产品
等级结构
*/
# include <iostream>
using namespace std;
//抽象苹果
class AbstractApple
{
public:
virtual void ShowName() = 0;
};
//中国苹果
class ChinaApple : public AbstractApple
{
public:
virtual void ShowName()
{
cout << "中国苹果" << endl;
}
};
//美帝苹果
class USAApple : public AbstractApple
{
public:
virtual void ShowName()
{
cout << "美帝苹果" << endl;
}
};
//倭国苹果
class JapanApple : public AbstractApple
{
public:
virtual void ShowName()
{
cout << "倭国苹果" << endl;
}
};
//抽象的香蕉
class AbstractBanana
{
public:
virtual void ShowName() = 0;
};
//中国香蕉
class ChinaBanana : public AbstractBanana
{
virtual void ShowName()
{
cout << "中国香蕉" << endl;
}
};
//美帝香蕉
class USABanana : public AbstractBanana
{
virtual void ShowName()
{
cout << "美帝香蕉" << endl;
}
};
//倭国苹果
class JapanBanana : public AbstractBanana
{
public:
virtual void ShowName()
{
cout << "倭国香蕉" << endl;
}
};
//抽象的鸭梨
class AbstractPear
{
public:
virtual void ShowName() = 0;
};
//中国鸭梨
class ChinaPear : public AbstractPear
{
public:
virtual void ShowName()
{
cout << "中国鸭梨" << endl;
}
};
//美帝鸭梨
class USAPear : public AbstractPear
{
public:
virtual void ShowName()
{
cout << "美帝鸭梨" << endl;
}
};
//倭国鸭梨
class JapanPear : public AbstractPear
{
public:
virtual void ShowName()
{
cout << "倭国鸭梨" << endl;
}
};
//抽象工厂 针对产品族
class AbstractFactory
{
public:
virtual AbstractApple* CreatApple() = 0;
virtual AbstractBanana* CreatBanana() = 0;
virtual AbstractPear* CreatPear() = 0;
};
//中国工厂
class ChinaFactory : public AbstractFactory
{
virtual AbstractApple* CreatApple()
{
return new ChinaApple;
}
virtual AbstractBanana* CreatBanana()
{
return new ChinaBanana;
}
virtual AbstractPear* CreatPear()
{
return new ChinaPear;
}
};
//美帝工厂
class USAFactory : public AbstractFactory
{
virtual AbstractApple* CreatApple()
{
return new USAApple;
}
virtual AbstractBanana* CreatBanana()
{
return new USABanana;
}
virtual AbstractPear* CreatPear()
{
return new USAPear;
}
};
//倭国工厂
class JapanFactory : public AbstractFactory
{
virtual AbstractApple* CreatApple()
{
return new JapanApple;
}
virtual AbstractBanana* CreatBanana()
{
return new JapanBanana;
}
virtual AbstractPear* CreatPear()
{
return new JapanPear;
}
};
void test01()
{
AbstractFactory* factory = NULL;
AbstractApple* apple = NULL;
AbstractBanana* banana = NULL;
AbstractPear* pear = NULL;
//构建一个中国的产地
factory = new ChinaFactory;
apple = factory->CreatApple();
banana = factory->CreatBanana();
pear = factory->CreatPear();
apple->ShowName();
banana->ShowName();
pear->ShowName();
delete pear;
delete banana;
delete apple;
delete factory;
}
int main(int argc, char *argv[])
{
test01();
return 0;
}