现在我需要开发一个相机操作模块,它可能在Windows
下运行,也可能在Linux
下运行。由于在厂家提供的SDK中,Windows
下的SDK和Linux
下的SDK是有区别的,因此对于一个品牌的相机,我们要创建两个类去封装这两个不同平台下的API。
我们先使用工厂方法模式去设计(以Basler
相机为例),类图如下:
对应的代码(就不用智能指针了,要不然类图不好画):
class BaslerCamera
{
public:
virtual ~BaslerCamera() = default;
virtual bool OpenCamera() = 0;
};
class LinuxBaslerCamera : public BaslerCamera
{
public:
~LinuxBaslerCamera() override = default;
bool OpenCamera() override
{
return true;
}
};
class WindowsBaslerCamera : public BaslerCamera
{
public:
~WindowsBaslerCamera() override = default;
bool OpenCamera() override
{
return true;
}
};
class CameraFactory
{
public:
virtual ~CameraFactory() = default;
virtual BaslerCamera* CreateBaslerCamera() = 0;
};
class LinuxCameraFactory : public CameraFactory
{
public:
BaslerCamera* CreateBaslerCamera() override
{
return new LinuxBaslerCamera();
}
};
class WindowsCameraFactory : public CameraFactory
{
public:
BaslerCamera* CreateBaslerCamera() override
{
return new WindowsBaslerCamera();
}
};
//客户端
int main()
{
//如果更换平台,客户端代码只需要修改这一处
CameraFactory* cameraFactory = new LinuxCameraFactory();
BaslerCamera* camera = cameraFactory->CreateBaslerCamera();
camera->OpenCamera();
return 0;
}
现在若新增了一个品牌的相机:Sick
,那么按照工厂方法模式的设计思路,就会为其创建出对应的抽象工厂类和具体工厂类(具体代码略)。
但是进一步分析可以发现,对于这个模块,它要么在Windows
下运行,要么在Linux
下运行。即对于抽象产品BaslerCamera
和SickCamera
,要么实例化LinuxBaslerCamera
和LinuxSickCamera
,要么实例化WindowsBaslerCamera
和WindowsSickCamera
。
可以说不同的相机被划分在Linux相机和Window相机这两个产品族下,因此我们不需要为每一个品牌的相机都去实现一组对应的工厂类,而是只使用两个工厂WindowsCameraFactory
和LinuxCameraFactory
去管理各自对应平台下的相机的创建过程。
那么工厂类的代码就会变成这样:
class CameraFactory
{
public:
virtual ~CameraFactory() = default;
virtual BaslerCamera* CreateBaslerCamera() = 0;
virtual SickCamera* CreateSickCamera() = 0;
};
class LinuxCameraFactory : public CameraFactory
{
public:
BaslerCamera* CreateBaslerCamera() override
{
return new LinuxBaslerCamera();
}
SickCamera* CreateSickCamera() override
{
return new LinuxSickCamera();
}
};
class WindowsCameraFactory : public CameraFactory
{
public:
BaslerCamera* CreateBaslerCamera() override
{
return new WindowsBaslerCamera();
}
SickCamera* CreateSickCamera() override
{
return new WindowsSickCamera();
}
};
这就引出了抽象工厂模式
抽象工厂模式,提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们具体的类
AbstractProductA
和AbstractProductB
是两个抽象产品,之所以为抽象,是因为他们可能有多种不同的实现,就刚才的例子来说,抽象产品就是BaslerCamera
和SickCamera
。ProductA1
,ProductA2
,ProductB1
,ProductB2
就是对两个抽象产品的具体分类的实现,对应例子中的LinuxBaslerCamera
,WindowsBaslerCamera
,LinuxSickCamera
,WindowsSickCamera
。
AbstractFactory
是一个抽象工厂基类,对应例子中的CameraFactory
,它里面应该包含产品族中每个产品创建的抽象方法。ConcreteFactory1
和ConcreteFactory2
是具体工厂,对应例子中的LinuxCameraFactory
和WindowsCameraFactory
。
对于客户端,通常是在代码中创建一个具体工厂的实例(这个实例就对应着一个产品族),使用这个工厂实例再创建产品族内具体的产品对象。
客户端代码如下:
int main()
{
/*
若在windows平台,只需将本句改为:
CameraFactory* cameraFactory = new WindowsCameraFactory();
*/
CameraFactory* camera_factory = new LinuxCameraFactory();
BaslerCamera* basler_camera = camera_factory->CreateBaslerCamera();
basler_camera->OpenCamera();
SickCamera* sick_camera = camera_factory->CreateSickCamera();
sick_camera->OpenCamera();
return 0;
}
完整代码如下:
class BaslerCamera
{
public:
virtual ~BaslerCamera() = default;
virtual bool OpenCamera() = 0;
};
class LinuxBaslerCamera : public BaslerCamera
{
public:
~LinuxBaslerCamera() override = default;
bool OpenCamera() override
{
return true;
}
};
class WindowsBaslerCamera : public BaslerCamera
{
public:
~WindowsBaslerCamera() override = default;
bool OpenCamera() override
{
return true;
}
};
class SickCamera
{
public:
virtual ~SickCamera() = default;
virtual bool OpenCamera() = 0;
};
class LinuxSickCamera : public SickCamera
{
public:
~LinuxSickCamera() override = default;
bool OpenCamera() override
{
return true;
}
};
class WindowsSickCamera : public SickCamera
{
public:
~WindowsSickCamera() override = default;
bool OpenCamera() override
{
return true;
}
};
class CameraFactory
{
public:
virtual ~CameraFactory() = default;
virtual BaslerCamera* CreateBaslerCamera() = 0;
virtual SickCamera* CreateSickCamera() = 0;
};
class LinuxCameraFactory : public CameraFactory
{
public:
BaslerCamera* CreateBaslerCamera() override
{
return new LinuxBaslerCamera();
}
SickCamera* CreateSickCamera() override
{
return new LinuxSickCamera();
}
};
class WindowsCameraFactory : public CameraFactory
{
public:
BaslerCamera* CreateBaslerCamera() override
{
return new WindowsBaslerCamera();
}
SickCamera* CreateSickCamera() override
{
return new WindowsSickCamera();
}
};
int main()
{
//若在windows平台,只需将本句改为CameraFactory* cameraFactory = new WindowsCameraFactory();
CameraFactory* camera_factory = new LinuxCameraFactory();
BaslerCamera* basler_camera = camera_factory->CreateBaslerCamera();
basler_camera->OpenCamera();
SickCamera* sick_camera = camera_factory->CreateSickCamera();
sick_camera->OpenCamera();
return 0;
}
优点:
缺点:
HuarayCamera
,WindowsHuarayCamera
,LinuxHuarayCamera
三个产品类之外(这是必要的),还要修改CameraFactory
,LinuxCameraFactory
和WindowsCameraFactory
这三个工厂类,违反了开闭原则。CameraFactory* camera_factory = new LinuxCameraFactory();
,若是要更换为Windows平台,则还需手动修改实例化的类型,违反了开闭原则。而且如果客户端不止一个,则每一个客户端都需要手动修改,效率低。对于抽象工厂模式的改进方法,将在下一篇文章中讨论。
1.《大话设计模式》