目录
简单的工厂模式
工厂模式的意义是为了简化用户创建某个对象的过程,假设一个对象创建需要经过读文件,解析文件,设置参数,创建对象等复杂的步骤。而利用工厂模式,就可以实现把复杂对象的创建交给类完成而不是用户自己完成,做到与具体的类解耦:
cpp
class singer
{
public:
virtual void showname() = 0;
};
class Jay :public singer
{
public:
virtual void showname()
{
cout << "周杰伦" << endl;
}
};
class Wayne :public singer
{
public:
virtual void showname()
{
cout << "林俊杰" << endl;
}
};
class Taylor_Swift:public singer
{
public:
virtual void showname()
{
cout << "泰勒 斯威夫特" << endl;
}
};
class factory
{
public:
singer* createSinger(string name)
{
if (name == "Jay")
return new Jay;
else if (name == "Wayne")
return new Wayne;
else return new Taylor_Swift;
}
};
void test4()
{
factory* fay = new factory;
singer* jay = fay->createSinger("Jay");
jay->showname();
}
int main()
{
test4();
return 0;
}
但是以上模式不符合开闭原则, 当新增singer派生类的时候,就需要修改工厂内的源码。
工厂方法模式
cpp
class singer
{
public:
virtual void showname() = 0;
};
class Jay :public singer
{
public:
virtual void showname()
{
cout << "周杰伦" << endl;
}
};
class Wayne :public singer
{
public:
virtual void showname()
{
cout << "林俊杰" << endl;
}
};
class Taylor_Swift:public singer
{
public:
virtual void showname()
{
cout << "泰勒 斯威夫特" << endl;
}
};
//class factory
//{
//public:
// singer* createSinger(string name)
// {
// if (name == "Jay")
// return new Jay;
// else if (name == "Wayne")
// return new Wayne;
// else return new Taylor_Swift;
// }
//};
//void test4()
//{
// factory* fay = new factory;
// singer* jay = fay->createSinger("Jay");
// jay->showname();
//}
// 工厂方法模式
class factory
{
public:
virtual singer* createSinger() = 0;
};
class JayFactory:public factory
{
public:
virtual singer* createSinger()
{
return new Jay;
}
};
class WayneFactory :public factory
{
public:
virtual singer* createSinger()
{
return new Wayne;
}
};
class TayFactory : public factory
{
public:
virtual singer* createSinger()
{
return new Taylor_Swift;
}
};
void test5()
{
factory* fy = new JayFactory;
singer* jay = fy->createSinger();
jay->showname();
}
int main()
{
test5();
return 0;
}
该模式符合开闭原则。一个工厂类对应一个singer派生类,分别创建对应的对象。但是这样会使工厂类数量增大。
抽象工厂模式
参考上面的模型,设计出了以下的模式:
cpp
class Everywhere_is_you
{
public:
virtual void showname() = 0;
};
class Everywhere_is_you_Jay :public Everywhere_is_you
{
public:
virtual void showname()
{
cout << "哪里都是你(周董)" << endl;
}
};
class Everywhere_is_you_Other :public Everywhere_is_you
{
public:
virtual void showname()
{
cout << "哪里都是你(其他)" << endl;
}
};
class mermaid
{
public:
virtual void showname() = 0;
};
class mermaid_Jay :public mermaid
{
public:
virtual void showname()
{
cout << "美人鱼(周董)" << endl;
}
};
class mermaid_Other :public mermaid
{
public:
virtual void showname()
{
cout << "美人鱼(其他)" << endl;
}
};
class factory
{
public:
virtual Everywhere_is_you* createE() = 0;
virtual mermaid* createM() = 0;
};
class Jay_factory :public factory
{
virtual Everywhere_is_you* createE()
{
return new Everywhere_is_you_Jay;
}
virtual mermaid* createM()
{
return new mermaid_Jay;
}
};
class Other_factory :public factory
{
virtual Everywhere_is_you* createE()
{
return new Everywhere_is_you_Other;
}
virtual mermaid* createM()
{
return new mermaid_Other;
}
};
void test5()
{
factory* faJ = new Jay_factory;
mermaid* meJ = faJ->createM();
meJ->showname();
}
int main()
{
test5();
return 0;
}
适用场景:
- 一系列相关产品对象(属于同一产品族)一起创建时需要大量的重复代码;
- 提供一个产品类的库,所有的产品以同样的接口出现,从而使得客户端不依赖于具体的实现。
注意:如果增加了一个产品族,那么会影响到整个代码的编写,如果只是增加一个产品等级,那么只需要再添加代码即可。