Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » Design & Architecture » Simple Factory Pattern side by side with Abstract Pattern

Simple Factory Pattern side by side with Abstract Pattern

This article will discuss the Simple Factory Pattern and how to use it with Abstract Factory Patter discussed in the previous article.

Author Rank :
Page Views : 13628
Downloads : 163
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
FactoryPatterns.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Introduction:

This article will discuss the Simple Factory Pattern and how to use it with Abstract Factory Patter discussed in the previous article.

About Simple Factory:

A Simple Factory pattern is one that returns an instance of one of several possible classes, depending on the data provided to it. Usually all of the classes it returns have a common parent class and common methods, but each of them performs a task differently and is optimized for different kinds of data. 

 

Simple Factory Structure:

How Simple Factory Works:

To understand how Simple Factory pattern works, have a look at the diagram above. Product is a base class for classes ProductA and ProductB . The ProductFactory class decides which of these subclasses to return, depending on the arguments you give it. On the other hand, we define a CreateProduct method to be one that passes in some value through spec parameter. The method should return some instance of the class Product. Which one it returns doesn't matter to the developer, since they all have the same methods but different implementations. How it decides which one to return is entirely up to the factory. It could be some very complex function, but it is often quite simple.

Where Simple Factory is applied:

Simple factory is applied in may places and it is widely used, but here we will show one good place of its applications.

If you got the chance to work or examine the Enterprise Library built by microsoft, you'll notice that this concept is used. For example in Data Access Application Block of the Enterprise Library, there is class named Database (The Product Class), the classes is base class for SqlDatabase & OracleDatabase classes (The ProductA & ProductB classes). Now our factory class is named DatabaseFactory (The ProductFactory class). The DatabaseFactory class has a method named CreateDatabase(string name) (CreateProduct(spec)). CreateDatabase method is responsible to create an instance of type Database, this can be SqlDatabase or OracleDatabase based on the value send in the name parameter. One this you may need to notice, is that the CreateDatabase method is static method. The developer doesn't care of which instance is returned (SqlDatabase or OracleDatabase) as long as they all have the same methods but different implementations.

How you can use Simple Factory side by side with Abstract Factory:

Referring to my previous article, we've discussed the Abstract Factory Pattern and its real implementation inside ADO.NET 2.0 in the database provider model. To link between Abstract Factory & Simple Factory, first we must be reminded that Abstract Factory is a factory object that returns one of several groups of classes, so we might use Simple Factory to decide which class to return from those groups. Taking the ADO.NET 2.0 Provider model as an example of Abstract Factory, you may decide to use a Simple Factory class to return a specific ADO.NET 2.0 Provider.

From the diagram above, SimpleProviderFactory is supposed to be the Simple Factory class. It contains CreateProvider method with input parameter providerName, this parameter will be responsible to decide which DbProviderFactory implementation to be returned. Of course implementation of CreateProvider method will use this input parameter to return the specific provider factory class.

In real world applications, providerName parameter will be configurable through application configuration file in .NET Framework.

Conclusion:

Simple Factory returns instances of classes that have the same methods. They may be instances of different derived subclasses, or they may in fact be unrelated classes that just share the same interface. Either way, the methods in these class instances are the same and can be used interchangeably.

References:

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Muhammad Mosa
Muhammad M. Mosa Soliman: Software Engineer, graduated from the Faculty of Computers & Information Systems year 2003-Ain Shams University- in Cairo. Working with Microsoft .NET technology since early beta releases. Main experiance based on ASP.NET, SharePoint Portal 2003 & SQL Server. Worked as trainer for Microsoft .NET for 2 years in Cairo. Likes to read about new technologies and self-learning. Extremly Hard worker when motivated. MCT MCSD.NET MCTS: .Net 2.0 Web/Windows Applications MCPD: Enterprise Application Developer MCTS: WSS 3.0 & MOSS 2007 Config
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Team Foundation Server Hosting
Become a Sponsor
 Comments
Please provide Implementation of the Simaple design Pattern by terli_m On April 18, 2007
Please provide Implementation of the Simaple design Pattern
Reply | Email | Modify 
Re: Please provide Implementation of the Simaple design Pattern by Muhammad On April 18, 2007
I'll try to write something about that. but actually these days I'm really full
Reply | Email | Modify 
Re: Please provide Implementation of the Simaple design Pattern by Muhammad On April 18, 2007
I'll try to write something about that. but actually these days I'm really full
Reply | Email | Modify 
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.