skip to Main Content

Welcome

Say hello to the toggle bar. This is an optional section you can use to display any content you'd like. Simply select a page from the theme panel and the content of the page will display here. You can even use the drag and drop builder to create this! This is a perfect place for your company mission statement, alerts, notices or anything else.

Get In Touch

Email: support@total.com
Phone: 1-800-Total-Theme
Address: Las Vegas, Nevada

Our Location

togglebar-map

Factory Pattern

Post Series: Creational Design Pattern

In Factory pattern, we create object without exposing the creation logic. In this pattern, an interface is used for creating an object, but let subclass decide which class to instantiate. The creation of object is done when it is required. The Factory method allows a class later instantiation to subclasses.

Diagam

 

Product: This is an interface for creating the objects.
ConcreteProduct: This is a class which implements the Product interface.
Creator: This is an abstract class and declares the factory method, which returns an object of type Product.
ConcreteCreator: This is a class which implements the Creator class and overrides the factory method to return an instance of a ConcreteProduct.

Implement

 

Useful

  1. Subclasses figure out what objects should be created.
  2. Parent class allows later instantiation to subclasses means the creation of object is done when it is required.
  3. The process of objects creation is required to centralize within the application.
  4. A class (creator) will not know what classes it will be required to create.

When to use

  1. In class have a “type” and must switch(type) in progress. Create a Factory Pattern to avoid modify this class in the future (this class is Closed principle).

When not use

  1. Therefore, the main disadvantages are cluttering. In most cases you end up with huge files or with many small files.

Resource

https://www.stdio.vn/articles/read/275/design-pattern-factory-pattern

http://www.dotnettricks.com/learn/designpatterns/factory-method-design-pattern-dotnet

https://softwareengineering.stackexchange.com/questions/254923/what-are-the-disadvantages-of-a-simple-factory

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top