Reference: E. Gamma et al., "Design Patterns: Elements of Reusable Object-Oriented Software"ISBN 0-201-63361-2, Addison Wesley, 1995.ContextAn 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. Solution Create an Abstract Factory class to answer instances of concrete classes (usually subclasses). The class of the resultant instance is unknown to the client of the Abstract Factory.There are two types of Abstract Factory:
Characteristics
ExampleThe Example here has an implementation of an Abstract Factory as an Interface IAVDevice that has methods that can create an Audio object and a Video object. The client Codes against IAVDevice and gets IAudio and IVideo interfaces. Passing "cd" in the command line creates a family of cd objects (Audio and Video) and "dvd" creates a family of dvd objects (Audio and Video). The client doesn't care which object (cd audio video or dvd audio video), IAVDevice interface returns as it codes against IAudio and IVideo interface.
C# ImplementationTo run the example from console first make the exe and then run it by using csc /out:AbstractFactory.exe AbstractFactory.cs //CreatesAbstractFactoryAbstractFactory cd //CD FamilyAbstractFactory dvd //DVD Family using System;public interface IAVDevice{ IAudio GetAudio();IVideo GetVideo();} public interface IVideo{ string GetPictureQuality();}public interface IAudio{ string GetSoundQuality();}class CCd:IAVDevice{public IAudio GetAudio(){return new CCdAudio();}public IVideo GetVideo(){return new CCdVideo();} }class CDvd:IAVDevice{public IAudio GetAudio(){return new CDvdAudio();}public IVideo GetVideo(){return new CDvdVideo();} }class CCdAudio:IAudio{ public string GetSoundQuality(){return "CD Audio is better then DVD Audio";}}class CCdVideo:IVideo{public string GetPictureQuality(){return "CD video quality is not as good as DVD";}}class CDvdAudio:IAudio{public string GetSoundQuality(){return "DVD Audio is not as good as CD Audio";} }class CDvdVideo:IVideo{ public string GetPictureQuality(){return "DVD video quality is better then CD";}}class CAVMaker{ public IAVDevice AVMake(string xWhat){switch (xWhat.ToLower()){case "cd":return new CCd();case "dvd":return new CDvd();default:return new CCd(); }}}public class AbstractFactory{static void Main(string[] args){CAVMaker objFactMaker = new CAVMaker();IAVDevice objFact;IAudio objAudio;IVideo objVideo;string strWhat;strWhat = args[0];objFact = objFactMaker.AVMake(strWhat);objAudio = objFact.GetAudio();objVideo = objFact.GetVideo();Console.WriteLine(objAudio.GetSoundQuality());Console.WriteLine(objVideo.GetPictureQuality());}}VB.Net ImplementationTo run the example from console first make the exe and then run it by usingvbc /out:AbstractFactory.exe AbstractFactory.vb 'CreatesAbstractFactoryAbstractFactory cd 'CD FamilyAbstractFactory dvd 'DVD Family Imports SystemPublic Interface IAVDeviceFunction GetAudio() As IAudioFunction GetVideo() As IVideoEnd InterfacePublic Interface IAudioFunction GetSoundQuality() As StringEnd InterfacePublic Interface IVideoFunction GetPictureQuality() As StringEnd InterfaceClass CCdImplements IAVDevicePublic Function GetAudio() As IAudio Implements IAVDevice.GetAudioGetAudio = New CCdAudioEnd FunctionPublic Function GetVideo() As IVideo Implements IAVDevice.getVideoGetVideo = New CCdVideoEnd FunctionEnd ClassClass CDvdImplements IAVDevicePublic Function GetAudio() As IAudio Implements IAVDevice.GetAudioGetAudio = New CDvdAudioEnd FunctionPublic Function GetVideo() As IVideo Implements IAVDevice.GetVideoGetVideo = New CDvdVideoEnd FunctionEnd ClassClass CCdAudioImplements IAudioPublic Function GetSoundQuality() As String ImplementsIAudio.GetSoundQuality()GetSoundQuality = "CD Audio is better then DVD Audio"End FunctionEnd ClassClass CCdVideoImplements IVideoPublic Function GetPictureQuality() As String ImplementsIVideo.GetPictureQuality()GetPictureQuality = "CD video quality is not as good as DVD"End FunctionEnd ClassClass CDvdAudioImplements IAudioPublic Function GetSoundQuality() As String ImplementsIAudio.GetSoundQuality()GetSoundQuality = "DVD Audio is not as good as CD Audio"End FunctionEnd ClassClass CDvdVideoImplements IVideoPublic Function GetPictureQuality() As String ImplementsIVideo.GetPictureQuality()GetPictureQuality = "DVD video quality is better then CD"End FunctionEnd ClassPublic Class CAVMakerPublic Function AVMake(ByVal xWhat As String) As IAVDeviceSelect Case xWhat.ToLowerCase "cd"AVMake = New CCdCase "dvd"AVMake = New CDvdEnd SelectEnd FunctionEnd ClassPublic Class ClientPublic Shared Sub Main(ByVal CmdArgs() As String)Dim objFactMaker As New CAVMakerDim objFact As IAVDeviceDim objAudio As IAudioDim objVideo As IVideoDim strWhat As StringstrWhat = CmdArgs(0)objFact = objFactMaker.AVMake(strWhat)objAudio = objFact.GetAudioobjVideo = objFact.getVideoConsole.WriteLine(objAudio.GetSoundQuality)Console.WriteLine(objVideo.GetPictureQuality)End SubEnd Class
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: