0

I know this question has been asked many times, but I still have a very simple question, what is the purpose of using a factory method if the initiation of an object is simple.

Interface Animal{
    eat();
}

class Dog implements Animal{

     public void eat(){System.out.println("dog eat");}
}

Assume I have a concrete Cat class and fish class implement the Animal interface.

So in this case, is it necessary to make 3 Factory to create the animals? I think we only use the factory method when the initialization is difficult.

abcXYZ
  • 119
  • 2
  • 7
  • A factory decouples the client from a concrete implementation. Loose coupling is the purpose. – jaco0646 Nov 29 '20 at 03:17
  • @jaco0646 But in this case the client will couple with the factory and the concrete animals will also couple with the factory, I didn't see how the factory decrease the coupling. – abcXYZ Nov 29 '20 at 07:40
  • 2
    What do mean by _Factory Pattern_? Some people say _Factory Pattern_, but there isn't any design pattern named _Factory Pattern_. In Design Pattern, there are similar names like _Factory Method_, _Abstract Factory_ and so on. Which do you mean? – Ali Soltani Nov 29 '20 at 07:56
  • @AliSoltani I mean Factory method, I will edit the post – abcXYZ Nov 29 '20 at 08:05
  • Does this answer your question? [Applicability for the Factory Method Pattern](https://stackoverflow.com/questions/63822775/applicability-for-the-factory-method-pattern) – jaco0646 Nov 29 '20 at 13:45
  • Apologies for my initial comment. I took a guess at which factory pattern, when I should have asked the question that Ali Soltani did. There are many [factory patterns](https://stackoverflow.com/a/62218649/1371329) and their implementations are wildly different. I didn't mean to confuse the topic with my guess. – jaco0646 Dec 04 '20 at 15:56

2 Answers2

0

No. You use the builder pattern if initialization is difficult.

The purpose of the factory pattern(s) (factory method and abstract factory) is to remove the creation from an abstraction layer. It's typically important if you have related classes on the same level.

For instance: IHttpRequest -> HttpRequestWithOnlyParsedHeaders -> HttpRequestFullyParsed

The first class can be used when proxying or diverting requests. In that case, it's a waste of resources to parse the content and allocate objects for it.

So when a reply is going to be sent back you do not want to create response object manually since that would generate something like:

if (request is HttpRequestWithOnlyParsedHeaders)
    return new HttpResponseWithOnlyHeaders;
else 
    return new HttpResponseWithContent;

That code is fragile and only works until a new class is introduced in the inheritance hierarchy.

An alternative is to introduce a factory method in the IHttpRequest inferface: IHttpResposne CreateResponse().

That way, you will never have to change any existing code. Any new implementations are responsible for creating the correct HTTP response implementation.

jgauffin
  • 99,844
  • 45
  • 235
  • 372
0

both Builder and Factory are "Creational" patterns. They differ from the fact that the "Builder" is essentially used to configure and return an object instance.

The Factory instead can get dependencies from the outside, or directly in the Create() method (if common to all the implementations) and abstract away the call to the class constructor.

A Builder will return a specific class. A Factory will return an interface, the caller won't know what's the implementation.

David Guida
  • 950
  • 9
  • 19