|
|
|
|
|
|
|
Author Rank :
|
|
|
Page Views :
|
6912
|
|
Downloads :
|
0
|
|
Rating :
|
Rate it
|
|
Level :
|
Advanced
|
|
The ABSTRACT FACTORY PATTERN comes under the classification of Creational Patterns. The creational patterns deals with the best way to create objects. The Abstract Factory provides an interface to create and return one of several families of related objects.
"Provide an interface for creating families of related or dependent objects without specifying their concrete classes" -- "Design Patterns" Gamma et al., Addison-Wesley, ISBN:0-201-63361-2"
Non-software Example
This pattern is found in the sheet metal stamping equipment used in the manufacture of Japanese automobiles. The stamping equipment is an Abstract Factory, which creates auto body parts. The same machinery is used to stamp right hand doors, left hand doors, right front fenders, left front fenders, hoods, etc. for different models of cars. Through the use of rollers to change the stamping dies, the concrete classes produced by the machinery can be changed within three minutes. [Michael Duell, "Non-software examples of software design patterns", Object Magazine, Jul 97, p54]
The abstract factory is a factory object that returns one of several factories. It can be used to return one of several related classes of objects, each of which can return several different objects on request.
The abstract factory pattern can be interpreted and implemented in many ways. The following is a simples interpretation and implementation of this pattern.
In this case the interface Factory has two concrete implementations, ConcreteFactory1 and ConcreteFactory2. The getObject() inside these concrete classes returns Derived1 and Derived2 objects respectively. The client can decide which ConcreteFactory class has to be used during the run-times.
The following is a more complicated interpretation and implementation of this pattern. Here those Factory class methods are used for returning objects of two different class hierarchies.

C# Implementation
// Creational Pattern: Abstract Factory Pattern //Author: rajeshvs@msn.com /* In the following snippet, Factory is an interface. The concrete implementation of this interface ConcreteFactory1 and ConcreteFactory2 implements the method getObject so that it returns Derived1 and Derived2 objects respectively. The Base is an interface and Derived1 and Derived2 are the concrete implementations of the base class. The client (MyClient class) always uses the Factory implementations to create an instance of the Base classes. Actually the derived classes of Factory interface decided which object (either Derived1 or Derived2) has to be created. */
using System; interface Factory { Base GetObject(); } //This class is responsible for creating objects of the class Derived1. class ConcreteFactory1 :Factory { public Base GetObject() { return new Derived1(); } } //This class is responsible for creating objects of the class Derived2. class ConcreteFactory2 : Factory { public Base GetObject() { return new Derived2(); } } interface Base { void DoIt(); } class Derived1 : Base { public void DoIt() { Console.WriteLine("Derived 1 method"); } } class Derived2 : Base { public void DoIt() { Console.WriteLine("Derived 2 method"); } } /* Client class Client class needn't know about instance creation. The creation of Product is deferred to he ConcreteFactory1. */ class MyClient { public static void Main() { Factory factory = new ConcreteFactory2();//Decides which object must create. Base obj = factory.GetObject(); obj.DoIt(); } }
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
Rajesh VS
Rajesh V.S is a software engineer in the area of C/C++/JAVA for the last 5 years. Currently he is interested in core C# language. He is Sun Certified Java Programmer. His other area of interest includes Design Patterns and CORBA. He is also a writer of numerous articles for many technical Web sites.
|
|
|
|
|
|
|
|
|
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!
|
|
|
|
|
|
|
|
|
|
|
|
|