Factory Refinement Method

My understanding of the Factory method template ( Correct me if I am wrong )

Factory Method Template

"The Factory Method allows the client to delegate product creation (instance creation) to a subclass."

There are two situations in which we can go to create a Factory method template.

(i) When the customer is limited to creating a product (instance).

(ii) Several products are available. But you need to decide on which instance of the product should be returned.

If you want to create an abstract method template

  • You need to have an abstract product
  • Concrete product
  • Factory Way to return the corresponding product.

Example:

public enum ORMChoice { L2SQL, EFM, LS, Sonic } //Abstract Product public interface IProduct { void ProductTaken(); } //Concrete Product public class LinqtoSql : IProduct { public void ProductTaken() { Console.WriteLine("OR Mapping Taken:LinqtoSql"); } } //concrete product public class Subsonic : IProduct { public void ProductTaken() { Console.WriteLine("OR Mapping Taken:Subsonic"); } } //concrete product public class EntityFramework : IProduct { public void ProductTaken() { Console.WriteLine("OR Mapping Taken:EntityFramework"); } } //concrete product public class LightSpeed : IProduct { public void ProductTaken() { Console.WriteLine("OR Mapping Taken :LightSpeed"); } } public class Creator { //Factory Method public IProduct ReturnORTool(ORMChoice choice) { switch (choice) { case ORMChoice.EFM:return new EntityFramework(); break; case ORMChoice.L2SQL:return new LinqtoSql(); break; case ORMChoice.LS:return new LightSpeed(); break; case ORMChoice.Sonic:return new Subsonic(); break; default: return null; } } } **Client** Button_Click() { Creator c = new Creator(); IProduct p = c.ReturnORTool(ORMChoice.L2SQL); p.ProductTaken(); } 

How do I understand the Factory method?

+6
design design-patterns
source share
3 answers

In fact, you have more Abstract Factory Template , only if you Factory ( Creator ) is not abstract. The factor method template is especially useful for a subclass:

 class A { public: A() : m_Member( GetMember() ) { } protected: virtual ISomeInterface * GetMember() { // default impl here } private: ISomeInterface * m_Member; } 

Subclasses A can now override GetMember to force the superclass to use a specific implementation of ISomeInterface .

+1
source share

Define an interface to create an object, but let subclass which class to instantiate. The Factory method allows the class to defer instantiation of subclasses.

more detailed information and an example there: http://www.dofactory.com/Patterns/PatternFactory.aspx

0
source share

Yes, this is the right way to implement this, albeit rather simplified. In fact, you can consider the possibility of passing various parameters, which may not always be consistent for all types. Dictionaries / Lists / Hashtables / etc .. are useful for this, as these are serialized elements and / or XML and other dynamic things.

-one
source share

All Articles