200 likes | 215 Views
Learn about Factory Design Patterns, a set of rules for creating objects, encapsulating creational operations, and improving code quality. Understand the history, implementation, and usage examples to enhance your software development skills.
E N D
RedRock Seminar Design Pattern – Factory Patterns Aniruddha Chakrabarti
What is Design Pattern • Addresses a recurring design problem that arises in specific design situations and presents a solution to it. • Constitutes a set of rules describing how to accomplish certain tasks in software development • Focus more on reuse of recurring architectural design themes, while frameworks focus on detailed design and implementation • Identify and specify abstractions that are above the level of single classes and instances or of components. (Gamma, Johnson, and Vlissides, 1993) • Helps in improving code quality
Little bit of History • Began to be recognized more formally in the early ‘90s by Eric Gamma - described patterns incorporated in the GUI app f/w • Continued Discussion & meeting lead to publication of parent book – “Design Patterns: Elements of Reusable Software” by Gof (Erich Gamma, Richard Helm, Ralph Johnson, & John Vlissides) in ‘95 • Had a powerful impact on S/W Development community • Became an all-time bestseller. Describes 23 commonly occurring & generally useful patterns & comments on how and when to apply them. • Other useful books were published later • One closely related book is The Design Patterns Smalltalk Companion • Later books on different language like Java, VB, .net were published
Grouping Design Patterns • Creational patterns create objects for you rather than having you instantiate objects directly. Gives program more flexibility in deciding which objects need to be created for a given case • Structural patterns help you compose groups of objects into larger structures, such as complex user interfaces or accounting data. • Behavioral patterns help you define the communication between objects in your system and how the flow is controlled in a complex program.
Factory Design Patterns • Just what their name implies: they are classes that create or construct something. • Provide encapsulation of code required to render an instance of abstract class type as an implementation class • Provides a simple decision-making class that returns one of several possible subclasses of an abstract base class, depending on the data that are provided. • The factory can initialize, pull in data, configure, set state, & perform nearly any other creational operation needed • Three different flavors of Factory Design Patterns - • Simple Factory • Abstract Factory • Factory Method
Simple Factory • Very popular – could be found again & again • 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. • Not a GoF patterns, but it serves here as an introduction to the somewhat more subtle Abstract Factory & Factory Method GoF pattern
Simple Factory Class Diagram • Two main parts – • Factory Class • Product Class • Factory class renders the Product class • Product class contains data or functionality & is part of a series of class types that can be rendered from Factory class
Problem 1 • Implementation classes with a common base are created in multiple places • No uniformity exists between creational logic .....lives in class FindSuit Suit suit; if(suitType == SuitType.Armani) suit = new Armani(); else if(suitType == SuitType.StripedBusinessSuit) suit = new StripedBusinessSuit(); .....lives in class GetSuit if(suitType == SuitType.PlaidBusinessSuit) suit = new PlaidBusinessSuit(); else if(suitType == SuitType.GolfSuit) suit = new GolfSuit();
Solution 1 • Encapsulate the creation and decisional process of which type of the Suit class to create • Central place to house the conditional code.
Solution 1 – Code Example public sealed class SuitFactory { public Suit CreateSuit(SuitType suitType) {Suit suit; if(suitType == SuitType.Armani) suit = new Armani(); else if(suitType == SuitType.StripedBusinessSuit) suit = new StripedBusinessSuit(); else if(suitType == SuitType.PlaidBusinessSuit) suit = new PlaidBusinessSuit(); else if(suitType == SuitType.GolfSuit) suit = new GolfSuit(); return suit; } } .....refactored in class FindSuit & GetSuit SuitFactory factory = new SuitFactory(); Suit suit = factory.CreateSuit(SuitType.Armani);
Simple Factory usage in RedRock • Encapsulates the logic for instantiating a working implementation of the ILockRequestManager interface • Returns Client Version or Sever Version of ILockRequestManager implementaion depending on where the component resides.
Abstract Factory • Expands the basic Factory pattern • Gives us a way to allow factories with similar methods and access points to be interchangeable.
Abstract Factory Class Diagram • Two main components: • Abstract Factory • Abstract Product. • Expands the basic Factory pattern • Gives us a way to allow factories with similar methods and access points to be interchangeable.
Abstract Factory usage in .net BCL/FCL DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.SqlClient"); DbConnection conn = factory.CreateConnection(); MessageBox.Show(conn.GetType().ToString());
Factory Method used in .net FCL ArrayList al = new ArrayList(); al.Add("asd"); MessageBox.Show(al.GetEnumerator() .GetType().ToString()); Output - Hashtableht = new Hashtable(); ht.Add("key1","asd"); MessageBox.Show(ht.GetEnumerator() .GetType().ToString()); Output -
Parameterized Factory Method used in .net FCL SymmetricAlgorithm symAlgo = SymmetricAlgorithm.Create(“SHA1"); MessageBox.Show(symAlgo.GetType().ToString()); Output - SymmetricAlgorithm symAlgo = SymmetricAlgorithm.Create(“MD5"); MessageBox.Show(symAlgo.GetType().ToString()); Output -
Resources • Website • Usage of factory Method with in .net FCL/BCL- http://www.ondotnet.com/pub/a/dotnet/2003/08/11/factorypattern.html • Design Patterns in C# and VB - http://www.dofactory.com/Patterns/Patterns.aspx • Books • Design Patterns – GOF • Design Patterns – Christpher G Lasater • Software Factories – Jack Greenfield and Keith Short
Questions If (Questions == null || Questions.Count == 0) { Thank you; }