Templates: Local Singleton vs Global Singleton?

There is a sample that I use from time to time, but I'm not quite sure what it is called. I was hoping the SO community could help me.

The template is quite simple and consists of two parts:

  • A factory method that creates objects based on the arguments passed.

  • Objects created by factory.

This is a standard factory template so far.

However, the problem I'm asking about is that the parent argument in this case maintains a set of links to each child object that it has ever created stored in the dictionary. These links can sometimes be strong links, and sometimes weak links, but they can always refer to any object that he has ever created.

Upon receiving a request for a “new” object, the parent first searches for a dictionary to see if the object exists with the necessary arguments. If so, it returns this object; if not, it returns a new object, and also stores the link to the new object in the dictionary.

This pattern prevents duplicate objects representing the same basic “thing”. This is useful when created objects are relatively expensive. It can also be useful when these objects are processing events or exchanging messages - having one object per item can prevent multiple messages / events from one main source.

There are probably other reasons to use this template, but here I found it useful.

My question is: what to call it?

, , . . , , .

" ". " ". , " ", , ( ), , , , , " ". /p >

, , .

? , ?

...

# 1:

, , ", , , DI, ( -).

, , , , / , .

, , ( "" ) , . , , . , , , (, , ).

"" , , , "" "" , . , , "" , factory (, , ) . , .

, " ", " ".

, , , , . : - ? , , , !

# 2:

, , GoF "Flyweight". :

A 'flyweight factory' () 'flyweight objects' () dofactory. com article, .

Java, - , .

Flyweight hash consing Multiton pattern.

, , / , .

, , .

, - , , . "- "? " "? !

# 3:

TrueWill, :

, -, , DI. , Singleton factory (aka locator), factory. , , factory, ( ). , unit test //. - (, )

, , , , DI/IoC. -.

TrueWill Mark, DI/IoC : " +1. : OP DI/IoC".

TrueWill , DI, -.

, -, , -, , DI ...

, , . -, . -. , , . , .

. " № 1" , . . " № 2", Flyweight, , .

Flyweight -. , , . , TrueWill, , DI/IoC, , , DI/IoC .

.

+3
3

Flyweight GoF. singleton factory -, , . .

+3

, , , , singleton .

Factory?:]

+1

, , DI, ( -).

, Service Locator, Singleton. DI .

+1
source

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


All Articles