Why does the factory pattern work the way it does?

I never looked at the Factory template and today I decided to spend time and create a quick sample based on this article ( http://msdn.microsoft.com/en-us/library/ee817667.aspx ) to finally plunge into it.

The source code works fine in three separate builds, which are clearly labeled Product, Factory, and Client.

The main advantage (as I understand it) of the Factory template is the abstraction of an instance of the class "product" from the class "Client". Therefore, in the above example, the product instance never changes regardless of any changes made to the product class, you still have to make changes to the client class in order to transfer the new values โ€‹โ€‹needed to create the updated product. Should this data appear from somewhere?

In another example that I read, it was said that after the class is implemented, and the loads of other classes use it directly, changes made to the "product" class will require changes to each instance of this class, for example, for example, if a new variable was required in its constructor.

From what I can understand, the Factory pattern really ensures that an instance of this class never changes, if you want to pass a new variable to the product constructor, you just need to pass these new variables to the updated Factory.

Therefore, this clearly does not solve the problem, but simply moves it and at the same time adds additional complexity.

Given that this is an installed template, I obviously missed something. Hence this post: Please explain to me what I am missing.

thank

+3
source share
4 answers

A Factory , , , . , . Factory: .

, Factory: Factory Factory . , .

Abstract Factory . , ; , Windows MacOS. , ; , , , Linux, , , - Factory, Linux, . Lo , Linux, , , ( - , , : -)

: , , . , , , switch if/else. , , , , , .

- , , , , . - ?

. , , Factory. Factory. Factory , , .

+8

Factory Dependency Inversion, .

, :

class Client {
    private DatabaseReader reader = new DatabaseReader();
    public void read() {
        reader.read();
    }
}

DatabaseReader - . , :

class Client {
    private Reader reader = new DatabaseReader();
    ...
}

:

class Client {
    private Reader reader = ReaderFactory.getInstance.getReader();
    ...
}

, DatabaseReader, MemoryReader ..... ReaderFactory.

Factory, .

class Client {
    @Inject 
    private Reader reader;
    ...
}

/ .

+3

factory:

, . , , , , , , .

, , , .

, factory . , ( - ). , Product , .

, factory , , .

, : new MyClass(some_arg). , , , . factory Factory.createMyClass(some_arg).

+1

++ . , Java. , , , . , .

A sample factory solves this problem. You only need to create a specific factory once. Then you can transfer the link to the abstract factory (from which the specific factory is derived) in one common code and use it whenever you need to create a specific object, not knowing exactly which object it will create.

There are other benefits. Since one has only one location from which an object is created, it is easy to save a list of all created objects.

+1
source

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


All Articles