The main purpose of using the factory template?

Some people recommend a factory pattern in java. I do not know about that. What is the main purpose of using factory template in java and give me your suggestion what kind of template is useful?

+4
source share
5 answers

There are 2 well-known factory templates:

  • Factory Method Template.
  • Abstract factory template.

The factory template method basically "addresses the problem of creating objects (products) without specifying the exact class of the object to be created." and the Abstract Factory template "provides the ability to encapsulate a group of individual factories with a common theme."

Method

Factory is used to create an Object, mainly by creating a factory interface of methods that subclasses can output and implement a method to create an object. This allows you to encapsulate without having to worry about who created the object, but you have an object made after the method was excluded.

The abstract factory template encapsulates collections together that have the same objective functions to complete the task. For instance. you can have a Button GUI and have a factory, such as WindowFactory , LinuxFactory , AppleFactory , which can create these buttons. Wrap these factories in an abstract factory in such a way that, by providing the OS, it will return the specific OS factory to create the Button .

I hope this is clear. Sorry for not using the correct English constructive sentences.

+2
source

The purpose depends on the type of factory template, for example. Factory annotation: "Provide an interface for creating families of related or dependent objects without specifying their specific classes." from Design Patterns

Most factory templates allow you to loosen the connection between the create request and the class of the created object. This allows you to reserve the right to change your mind.

This approach is commonly used to enable unit testing with stubs or layouts.

+3
source

The factory pattern allows you to separate (reduce dependency) between a class and its consumer. Instead of letting the class create its dependent objects using the constructor, you provide its factory, which knows how to create the object, and uses the factory to create dependent objects. The use of interfaces in conjunction with the factory provides even greater isolation, since it allows you to change the factory to create various specific objects that implement the interface, without the need to change the consumption class. It is often used in unit testing in a way where you provide a false dependency implementation instead of the actual implementation for use in the test. Factories are generally useful when you have potentially several options for a class that implements an interface that can be provided depending on the state or configuration of the program.

+2
source

The main advantage is that you do not need to rewrite / rebuild / redeploy the code when you want to change the specific implementation used in the code. With Factory, you need to change the external configuration parameter (properties file?), Which implementation class to use and / or change the external JAR file containing the implementation.

You see this good reputation in several Java APIs that offer opportunities for creating implementation implementations like JDBC , JAXP , etc.

+1
source

Use the factory pattern (in one of its many forms) when:

  • Creating an object is too complex to allow the object to be responsible for itself.
  • To create an object requires interaction / knowledge of other objects and their creation.
  • Object creation depends on global state / environment variables
  • The applicant of such an object should not be tied to a specific implementation, but rather to an interface (in the case of the Factory test)
0
source

Source: https://habr.com/ru/post/1303334/


All Articles