Design pattern for using various class / method implementations

Sometimes we develop several algorithms to get the same results. For example, I wrote a class that stores my data in trees, and another class that stores about the same data, for example, in linked lists.

I will publish an interface (abstract class) called ThingStore, and will subclass it into TreeThingStore and ListThingStore, each of which uses trees or linked lists.

However, since I am publishing an abstract class, I need someone to decide which implementation to use ( EDIT : so the caller does not care about this), and I have no problem that it is hardcoded. I needed this more than once, but I looked at GoF and other Design Patterns catalogs unconvincingly. The most similar picture is “Strategy”, but it achieves various goals.

So, is there a design template for this intention? If not, can someone create someone or tell me why this should not be done (or better ways to achieve the same results)?

+3
source share
3 answers

, ​​. , ? , , , , . , ( , , ). , , . , , , - .:)

, Factory Factory . Inversion of Control pattern, , , , .

+3

Bridge. , , .

Java API . ArrayList LinkedList List. List , . ArrayList LinkedList, , .

Factory, , , .

+2

Factory ( , ). exitsting, factory .

factory is probably your closest template as it allows you to create projects similar to:

ThingStore theThingStore = ThingStoreFactory.GetStore("tree");
0
source

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


All Articles