Definition:
An abstract factory provides an interface for creating families of related objects without specifying their concrete classes. Sometimes one wants to construct an instance of one of a suite of classes, deciding between the classes at the time of instantiation. In order to avoid duplicating the decision making everywhere an instance is created, we need a mechanism for creating instances of related classes without necessarily knowing which will be instantiated.
UML Diagram:
Sample Code:
Enums & Factories:
public enum DeviceQuality
{
LowQuality = 1,
AverageQuality = 2,
GoodQuality = 3
}
public enum Device
{
CDPlayer = 1,
DVDPlayer = 2,
CassettePlayer = 3
}
public static class AVDeviceFactory
{
static IAVDevice _device = default(IAVDevice);
public static IAVDevice GetFactory(Device device)
{
switch (device)
{
case Device.CDPlayer:
_device = new CDPlayer();
break;
}
return _device;
}
}
public static class AudioDeviceFactory
{
static IAudioDevice _audioDevice = default(IAudioDevice);
public static IAudioDevice GetFactory(DeviceQuality quality)
{
switch (quality)
{
case DeviceQuality.GoodQuality:
_audioDevice = new GoodQualityAudioDevice();
break;
}
return _audioDevice;
}
}
public static class VideoDeviceFactory
{
static IVideoDevice _videoDevice = default(IVideoDevice);
public static IVideoDevice GetFactory(DeviceQuality quality)
{
switch (quality)
{
case DeviceQuality.GoodQuality:
_videoDevice = new GoodQualityVideoDevice();
break;
}
return _videoDevice;
}
}
{
LowQuality = 1,
AverageQuality = 2,
GoodQuality = 3
}
public enum Device
{
CDPlayer = 1,
DVDPlayer = 2,
CassettePlayer = 3
}
public static class AVDeviceFactory
{
static IAVDevice _device = default(IAVDevice);
public static IAVDevice GetFactory(Device device)
{
switch (device)
{
case Device.CDPlayer:
_device = new CDPlayer();
break;
}
return _device;
}
}
public static class AudioDeviceFactory
{
static IAudioDevice _audioDevice = default(IAudioDevice);
public static IAudioDevice GetFactory(DeviceQuality quality)
{
switch (quality)
{
case DeviceQuality.GoodQuality:
_audioDevice = new GoodQualityAudioDevice();
break;
}
return _audioDevice;
}
}
public static class VideoDeviceFactory
{
static IVideoDevice _videoDevice = default(IVideoDevice);
public static IVideoDevice GetFactory(DeviceQuality quality)
{
switch (quality)
{
case DeviceQuality.GoodQuality:
_videoDevice = new GoodQualityVideoDevice();
break;
}
return _videoDevice;
}
}
Interfaces and Classes:
public abstract class IAVDevice
{
protected IAudioDevice _audioDevice = default(IAudioDevice);
protected IVideoDevice _videoDevice = default(IVideoDevice);
public abstract IAudioDevice AudioDevice
{
get;
}
public abstract IVideoDevice VideoDevice
{
get;
}
}
public interface IAudioDevice
{
int GetSoundQuality();
}
public interface IVideoDevice
{
int GetVideoQuality();
}
public sealed class GoodQualityAudioDevice : IAudioDevice
{
public int GetSoundQuality()
{
return 1;
}
}
public sealed class GoodQualityVideoDevice : IVideoDevice
{
public int GetVideoQuality()
{
return 1;
}
}
public sealed class CDPlayer : IAVDevice
{
public override IAudioDevice AudioDevice
{
get
{
_audioDevice = AudioDeviceFactory.GetFactory(DeviceQuality.GoodQuality);
return _audioDevice;
}
}
public override IVideoDevice VideoDevice
{
get
{
_videoDevice = VideoDeviceFactory.GetFactory(DeviceQuality.GoodQuality);
return _videoDevice;
}
}
}
}
{
protected IAudioDevice _audioDevice = default(IAudioDevice);
protected IVideoDevice _videoDevice = default(IVideoDevice);
public abstract IAudioDevice AudioDevice
{
get;
}
public abstract IVideoDevice VideoDevice
{
get;
}
}
public interface IAudioDevice
{
int GetSoundQuality();
}
public interface IVideoDevice
{
int GetVideoQuality();
}
public sealed class GoodQualityAudioDevice : IAudioDevice
{
public int GetSoundQuality()
{
return 1;
}
}
public sealed class GoodQualityVideoDevice : IVideoDevice
{
public int GetVideoQuality()
{
return 1;
}
}
public sealed class CDPlayer : IAVDevice
{
public override IAudioDevice AudioDevice
{
get
{
_audioDevice = AudioDeviceFactory.GetFactory(DeviceQuality.GoodQuality);
return _audioDevice;
}
}
public override IVideoDevice VideoDevice
{
get
{
_videoDevice = VideoDeviceFactory.GetFactory(DeviceQuality.GoodQuality);
return _videoDevice;
}
}
}
}
No comments:
Post a Comment