D
D
DevStranger2019-04-01 19:31:52
OOP
DevStranger, 2019-04-01 19:31:52

factory method. How to implement it is clear, but why?

Hello. I understand how to implement, I do not fully understand the meaning of using this pattern. Probably I have not yet worked in large enough projects where its use is explained. From what I understand about its use (correct or supplement): 3) Since the process of creating specific objects can be quite complicated and differ for different classes, we create an additional abstraction in the form of a factory, in the specific implementations of which this whole thing will take place . 4) The factory method is the basis for other generating patterns. That is, when you cannot decide which of the generating patterns to use to optimize the code, use the factory method.
1) We remove the process of creating specific objects from the client code, and hide it behind the scenes of the factory. Thus, we implement encapsulation and the open-closed principle.
2) The program at the compilation stage does not know which specific products will be used, everything is programmed in the form of interfaces (base classes). Specific implementations will be in runtime depending on some conditions.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anatoly, 2019-04-01
@t0lik

For example, in the application config, the type of the database is specified as a string, and the factory method on this string produces an instance of the class for connecting to the database, to which the connection string will be passed in the future.

E
Evgeny Surin, 2019-04-01
@jenyaatnow

In the book Java. There is an excellent article about effective programming about this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question