示例
抽象工厂模式是对工厂进一步抽象,定义PCFactory抽象类,DELLFactory和HPFactory作为其子类,子类由具体的厂商实现,每个厂商不仅可以生产鼠标,还可以生产键盘,相比于工厂方法模式无需增加过多的factory
实现
/// <summary>
/// 对工厂进一步抽象,成为PCFactory,子类由具体的厂商实现,每个厂商不仅可以生产鼠标,还可以生产键盘,相比于工厂方法模式无需增加过多的factory
/// </summary>
abstract class PCFactory
{
internal abstract Mouse CreateMouse();
internal abstract Mouse CreateKeyBoard();
}
class HPFactory : PCFactory
{
internal override Mouse CreateKeyBoard()
{
throw new NotImplementedException();
}
internal override Mouse CreateMouse()
{
return new HPMouse();
}
}
class DELLFactory : PCFactory
{
internal override Mouse CreateKeyBoard()
{
throw new NotImplementedException();
}
internal override Mouse CreateMouse()
{
return new DellMouse();
}
}
//抽象工厂模式
new HPFactory().CreateMouse().DoubleClick();
new DELLFactory().CreateMouse().DoubleClick();
输出