Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Creational Pattern - Abstract Factory

0.00/5 (No votes)
20 Feb 2002 1  
Abstract Factory Design Pattern and sample implementation in C# and VB .NET

Introduction

Design patterns make it easier to reuse successful designs and architectures. Expressing proven techniques as design patterns makes them more accessible to developers of new systems.

Design patterns help you choose design alternatives that make a system reusable and avoid alternatives that compromise reusability.

Creational design patterns abstract the instantiation process. They help make a system independent of how its objects are created, composed, and represented. A class creational pattern uses inheritance to vary the class that's instantiated, whereas an object creational pattern will delegate instantiation to another object.

An abstract factory provides an interface for creating families of related objects without specifying their concrete classes

Context

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.

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: A Simple Abstract Factory is an abstract class defining Factory methods to answer instances of concrete subclasses. The choice of which subclass to instantiate is completely defined by which method is used, and is unknown to the client.

B The second form of Abstract Factory is an abstract class defining a common protocol of Factory methods. Concrete subclasses of the abstract factory implement this protocol to answer instances of the appropriate suite of classes. See below for an example, both VB.net and C# implementation

Applicability

A Need to abstract from details of implementation of products -The system shall be independent of how its constituent pieces are created, composed, and represented. B Need to have multiple families of products - The system shall be configured with one of multiple families of products. C Need to enforce families of products that must be used together - A family of related product objects is designed to be used together, and you need to enforce this constraint. D Need to hide product implementations and just present interfaces - You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

Characteristics

A An abstract factory is an object maker. B It typically can produce more than one type of object. C Each object that it produces is known to the receiver of the created object only by that object's interface, not by the object's actual concrete implementation. D The different types of objects that the abstract factory can produce are related -- they are from a common family E An abstract factory isolates concrete classes F It makes exchanging product families easy G It promotes consistency among products H It supports adding new kinds of products and their families.

Example

The 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# Implementation

To run the example from console first make the exe and then run it by using

csc /out:AbstractFactory.exe AbstractFactory.cs //Creates AbstractFactory
AbstractFactory cd //CD Family
AbstractFactory 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 Implementation

To run the example from console first make the exe and then run it by using:

vbc /out:AbstractFactory.exe AbstractFactory.vb 'Creates AbstractFactory
AbstractFactory cd //CD Family
AbstractFactory dvd //DVD Family
Imports System

Public Interface IAVDevice
    Function GetAudio() As IAudio
    Function GetVideo() As IVideo
End Interface

Public Interface IAudio
    Function GetSoundQuality() As String
End Interface

Public Interface IVideo
    Function GetPictureQuality() As String
End Interface

Class CCd
    Implements IAVDevice

    Public Function GetAudio() As IAudio Implements IAVDevice.GetAudio
        GetAudio = New CCdAudio()
    End Function

    Public Function GetVideo() As IVideo Implements IAVDevice.getVideo
        GetVideo = New CCdVideo()
    End Function
End Class

Class CDvd
    Implements IAVDevice

    Public Function GetAudio() As IAudio Implements IAVDevice.GetAudio
        GetAudio = New CDvdAudio()
    End Function

    Public Function GetVideo() As IVideo Implements IAVDevice.GetVideo
        GetVideo = New CDvdVideo()
    End Function
End Class

Class CCdAudio
    Implements IAudio

    Public Function GetSoundQuality() As _
           String Implements IAudio.GetSoundQuality
        GetSoundQuality = "CD Audio is better then DVD Audio"
    End Function
End Class

Class CCdVideo
    Implements IVideo

    Public Function GetPictureQuality() As _
           String Implements IVideo.GetPictureQuality
        GetPictureQuality = "CD video quality is not as good as DVD"
    End Function
End Class

Class CDvdAudio
    Implements IAudio

    Public Function GetSoundQuality() As _
           String Implements IAudio.GetSoundQuality
        GetSoundQuality = "DVD Audio is not as good as CD Audio"
    End Function
End Class

Class CDvdVideo
    Implements IVideo

    Public Function GetPictureQuality() As _
           String Implements IVideo.GetPictureQuality
        GetPictureQuality = "DVD video quality is better then CD"
    End Function
End Class

Public Class CAVMaker
    Public Function AVMake(ByVal xWhat As String) As IAVDevice
        Select Case xWhat.ToLower
            Case "cd"
                AVMake = New CCd()
            Case "dvd"
                AVMake = New CDvd()
        End Select
    End Function
End Class

public Class Client
    Public Shared Sub Main(ByVal CmdArgs() As String) 
        Dim objFactMaker As New CAVMaker()
        Dim objFact As IAVDevice
        Dim objAudio As IAudio
        Dim objVideo As IVideo
        Dim strWhat as String
        strWhat = CmdArgs(0)
        objFact = objFactMaker.AVMake(strWhat)
            objAudio = objFact.GetAudio
            objVideo = objFact.getVideo
            Console.WriteLine(objAudio.GetSoundQuality)
            Console.WriteLine(objVideo.GetPictureQuality)
    End sub
End Class

References

E. Gamma et al., "Design Patterns: Elements of Reusable Object-Oriented Software" ISBN 0-201-63361-2, Addison Wesley, 1995.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here