Factory Design Template and keyword 'new'

I am starting a programmer. I know the basics of OOP, but I don’t yet know the "best practices." For example, one paradigm that continues to evolve in programming is the abstract Factory pattern, which seems pretty simple. One of the key points behind it is to avoid the keyword β€œnew” because it is considered harmful. I have never heard of this in courses that I taught in programming. Can anyone clarify this? Why do we want to avoid instantiating objects in this form?

+6
source share
6 answers

Consider the client / caller in your class that you write:

Vehicle v = new Car("BMW"); 

If your code is similar to the above, you will always get a car. In the future, if you really need a plane, you will have to update the client code.

In addition, you use the factory template , you code something like:

 Vehicle v = Factory.getVehicle(); 

Now you can save the logic of car retraction (free connection) from the client, and your client will never need to change if you need to update the final car that you receive. Only the factory implementation will be implemented, and your customers will work as is.

+9
source

I would not say so far that new is considered harmful. What the factory abstract template is trying to do is solve the problem that new not redefinable (i.e. is not compatible with virtual dispatch, at least in languages ​​like Java and C #).

Consider this code example (C #):

 class Sender { private ISendChannel channel; public Sender() { } public void Connect(Uri endpointAddress) { // !! Sender is tightly coupled to TCP implementation // !! even though it doesn't apparently have to be. this.channel = new TcpSendChannel(endpointAddress); } /* ... */ } 

In this code, we have the basic ISendChannel interface to provide direct communication with any endpoint. However, the implementation, as indicated, is fixed to always use the TCP channel no matter what. This is undesirable, because now if you need an HTTP sender, you need to either change the Sender class or add new methods to it. This is a "bad connection."

Instead, you can use the factory to create channels and pass it to the sender. The sender will ask the factory to create a channel and thereby waive this responsibility. A new implementation might look like this:

 class Sender { private readonly ISendChannelFactory factory; private ISendChannel channel; public Sender(ISendChannelFactory factory) { this.factory = factory; } public void Connect(Uri endpointAddress) { // Sender does not have to care what type of channel it is. this.channel = this.factory.CreateSendChannel(endpointAddress); } /* ... */ } 

Now, to use the HTTP channel, you can create an instance of the sender using another type of factory, for example. new Sender(new HttpSendChannelFactory(/* ... */)); . Then, the HttpSendChannelFactory could return the HttpSendChannel (the specific type obtained from ISendChannel ) from its CreateSendChannel method.

+4
source

I suggest reading this article: http://www.codinghorror.com/blog/2005/09/head-first-design-patterns.html

key part: The best way to learn how to write simple code is to write simple code! Templates, like all forms of complexity, should be avoided until they become absolutely necessary. . The first thing that beginners need to start. Not the last.

I agree with this - you need a factory only if you really need to.

Well, returning to the topic, the new keyword may be evil, but in most cases it certainly is not and should not be a source of headache. Seeing

 List <Person> persons = new ArrayList<Person>(); 

Excellent. Don't even dare think of a factory by creating an ArrayList or LinkedList . It will be completely overloaded.

"I saw many systems that used the factory pattern. For example, if every object in the system is created using Factory instead of a direct instance (for example, a new StringNode (γƒŽ)), the system probably has an excess of plants." @ Joshua Kerievsky

Do not add templates prematurely if you are not sure if this is a good idea, and you cannot be sure of it without enough experience. Starting to write code with patterns in your mind - view: Factory, that's great, let's see when I can post it! - This is not a good idea:). It will be better to add patterns to places in your code that are unpleasant when you feel (smell :) - smell of code) that this can be done somehow better. This is refactoring for templates .

There is a wonderful book about this, and I will give you a link to the chapter " Move knowledge creation to Factory "

http://www.informit.com/articles/article.aspx?p=1398606&seqNum=2

I know this for a long time, but please read it, it is definitely worth the effort

+4
source

To understand this pattern or what's wrong with the new keyword, you are missing the main programming paradigm, which is Programming versus Interface

Study this, and once you understand what it is, you will realize that the only way to really provide / follow it is to avoid using new in your code, i.e. explicitly create specific objects in your code that has the unpleasant effect of code coupling.

+1
source

The idea of ​​an abstract factory template should not know anything about a particular implementation, including constructors.

Thus, the standard factory pattern returns a specific class, for example.

SomeType SomeVar = SomeFactory.CreateSomeType ();

Where as an abstract template factory will be

SomeInterface SomeVar = AbstractFactory.CreateSomeInterface ();

Thus, instead of SomeType being exposed to the consumer, only the interface is used. Just a higher level of abstraction, which is useful, but only harmful, if you do not want the factory consumer to know about SomeType.

0
source

Well, as far as I know, there is nothing wrong with the new keyword. I speak as a PHP developer. There is nothing wrong with the new keyword, and there are no security issues.

Factory pattern in PHP is used for automatic query, makes an instance of a class and returns this class. Not because there is something wrong with the new keyword, but because of it, one static function is launched much faster, and then it is required to use a new keyword for each object that you need.

And in the background, you will still use the new keyword :)

So there is nothing wrong with the new, and there is no need to avoid it. Its a joust, which with a factory draws it much faster to get an instance of a class, and then require a class, and then initiate it with a new keyword.

-1
source

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


All Articles