CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

David Hayden [MVP C#]

         .NET Tutorials, Patterns, and Practices

Design Patterns in C# - Construction Pattern - Factory Method

In the following two posts:

we have seen two snippets of code that are considered Factory Methods.

 

Factory Method

When you develop a class, you usually provide class constructors to let clients of you class instantiate it.  There are times, though, when a client that needs an object does not or should not know which of several possible classes to instantiate. The FACTORY METHOD pattern lets a class developer define the interface for creating an object while retaining control of which class to instantiate. [Design Patterns in C#, Steven John Metsker, p. 171]

 

We talked about the code below in the Enterprise Library Data Access Application Block:

 

Enterprise Library: DAAB

Database db = DatabaseFactory.CreateDatabase();

 

The CreateDatabase() method is considered a Factory Method in that the client is isolated from knowing which concrete class to instantiate during runtime.  The concrete class that will be instantiated is located in the dataConfiguration.config file.  I've cut down the config file quite a bit, but you can see the concrete type to be instantiated below as Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase in the assembly Microsoft.Practices.EnterpriseLibrary.Data.

 

dataConfiguration.config
xml version="1.0" encoding="utf-8"?>
<dataConfiguration>

      ...
    
      <databaseTypes>
        <databaseType name="Sql Server"
            type="Microsoft.Practices.EnterpriseLibrary.Data.Sql.SqlDatabase,
                Microsoft.Practices.EnterpriseLibrary.Data, Version=1.0.0.0,
                Culture=neutral, PublicKeyToken=null" />
      databaseTypes>

      ...
    
dataConfiguration>

 

We saw the Factory Method in the Community Server source code as well:

 

Community Server

WeblogDataProvider wdp = WeblogDataProvider.Instance();

 

The Instance() method is the Factory Method in this case. Again, the client code is unaware of the concrete class to be instantiated as the WeblogDataProvider, because it is tucked away in the communityserver.config file:

 

communityserver.config
<add 
    name = "WeblogDataProvider" 
    type = "CommunityServer.Data.WeblogSqlDataProvider,CommunityServer.SqlDataProvider" 
    connectionStringName = "SiteSqlServer" databaseOwnerStringName = "SiteSqlServerOwner"
/>

 

The fact that a method creates a new object does not in itself mean that it is an example of the Factory Method pattern.  The Factory Method pattern requires that an operation that creates an object also isolates the client from knowing which class to instantiate. When a client requests a new object, the precise class of the object that is created depends on the behavior of the object receiving the creation request.



Comments

David Hayden said:

I installed DotNetNuke (DNN) this weekend for the Sarasota, Florida .NET Developer Group website.&amp;nbsp;...
# April 3, 2005 3:59 PM
Check out Devlicio.us!

This Blog

Syndication

News

CodeBetter.Com Home