Is this Factory Method creation pattern?

前端 未结 20 812
再見小時候
再見小時候 2020-12-12 17:12

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

public static class ScheduleTypeFactory
{
    public st         


        
相关标签:
20条回答
  • 2020-12-12 17:41

    It is the factory method pattern, at least according to Wikipedia: http://en.wikipedia.org/wiki/Factory_method_pattern

    0 讨论(0)
  • 2020-12-12 17:44

    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.

    0 讨论(0)
  • 2020-12-12 17:44

    Your lead is right (sorry for the formatting, but this answer need some of it in order to be seen between the main line that is stating the lead is a moron): neither by the GOF book nor Head First this a factory method.

    GoF :

    “Define an interface for creating an object, but let the subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.”

    In your example, it's not a subclass that is deciding.

    Have you implemented it incorrectly all these years? No, you just haven't implemented the factory pattern, but what is sometimes referred to as the 'Simple Factory Pattern', which probably has done the job just fine.

    0 讨论(0)
  • 2020-12-12 17:44

    The simplest explanation of the factory pattern, which I learned from a patterns and practice class, was that if you rely on another class to create the instances of your class, then you're using the factory pattern.

    Of course, often times you want to add a layer of indirection by making your class abstract or an interface.

    This is very simplified view of it, but either way, you are using the factory pattern.

    0 讨论(0)
  • 2020-12-12 17:44

    Thats a textbook Factory Pattern.

    Some texts call it a Simple Factory Patteren, but I've never seen any criteria for what "Simple" means.

    In my practice, a Factory Pattern is any intelligent creation of a concrete class coresponding to a desired interface.

    But why fight your tech lead over naming a method. Rename it, move on with your life.

    0 讨论(0)
  • 2020-12-12 17:45

    Looks like a factory pattern to me. Tell your tech lead you don't have time to explain why she is wrong.

    0 讨论(0)
提交回复
热议问题