Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this Factory Method creation pattern?

I have been using factory method creation pattern for awhile now. I was just recently told that this:

public static class ScheduleTypeFactory {     public static IScheduleItem GetScheduleItem(ScheduleTypeEnum scheduleType)     {         IScheduleItem scheduleItem = null;          switch (scheduleType)         {             case ScheduleTypeEnum.CableOnDemandScheduleTypeID:                 {                     scheduleItem = new VODScheduleItem();                     break;                 }             case ScheduleTypeEnum.BroadbandScheduleTypeID:                 {                     scheduleItem = new VODScheduleItem();                     break;                 }             case ScheduleTypeEnum.LinearCableScheduleTypeID:                 {                     scheduleItem = new LinearScheduleItem();                     break;                 }             case ScheduleTypeEnum.MobileLinearScheduleTypeID:                 {                     scheduleItem = new LinearScheduleItem();                     break;                 }         }          return scheduleItem;     } } 

is not a factory method creation pattern by my "Tech" lead without telling me why or giving me her interpretation. I kindly asked for an explanation and she told me she didn't have time. I was told to just rename it. If I am wrong, then I will no doubt accept that I have implemented this incorrectly for years. Is this how YOU would implement the factory method creation pattern? Thanks in advance.

like image 638
user24985 Avatar asked Apr 30 '09 13:04

user24985


People also ask

Is factory method a design pattern?

The factory method is a creational design pattern, i.e., related to object creation. In the Factory pattern, we create objects without exposing the creation logic to the client and the client uses the same common interface to create a new type of object.

What is Factory Pattern with example?

Factory pattern removes the instantiation of actual implementation classes from client code. Factory pattern makes our code more robust, less coupled and easy to extend. For example, we can easily change PC class implementation because client program is unaware of this.

Which are the three types of factory method?

the abstract factory pattern,the static factory method, the simple factory (also called factory).


2 Answers

I would agree to call the method a "Factory Method", though the design is not strictly a "Factory Method Pattern".
Here is a key point (from Wikipedia):

...The Factory method lets a class defer instantiation to subclasses."

Since your class is static and method static (hence non-virtual), there is no "deferring" possible.

Conceptually, notice also, that this implementation, though provides encapsulation, does not decouple/delay any decision.

Having said that, same Wikipedia article does present this schema as a variant of the "Factory Method Pattern".

Summary of the Summary: In my opinion this snippet is not a proper implementation of the "Factory Method OO Design Pattern", since it does not satisfy "a class defer instantiation to subclasses." Though, personally I would freely refer to this solution as "factory method".

To make it real factory method pattern, you need to allow the method to be overridden by subclasses. I.e. factory class (ScheduleTypeFactory) needs to be extensible (i.e. non-static), and GetScheduleItem needs to be virtual.

like image 160
THX-1138 Avatar answered Sep 22 '22 10:09

THX-1138


Sure looks like the factory pattern to me. I don't see anything wrong with your implementation.

From Factory method pattern:

The essence of the Factory Pattern is to "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses."

This is exactly what you are doing.

As a side note: a good rule of thumb is that whenever someone tells you something and is unable or unwilling to provide a rationale for their statement, there is a good chance they are unqualified to make the statement at all.

like image 34
Andrew Hare Avatar answered Sep 24 '22 10:09

Andrew Hare