Search code examples
design-patternsarchitecturecircular-dependencyclean-architecture

Where design pattern should go in 3 three-layer architecture


Our backend follows a three-layer architecture:

- Presentation: This layer contains the endpoints.
- Services: Here, we implement the business logic.
- Data: This layer includes the Data Access Objects (DAOs) 
    responsible for communicating with the database.

In addition to this structure, we have some folders:

- Models: This folder contains interfaces that represent our business objects.
- Utils: Here, we manage logic that is not directly related to 
    the business domain, such as handling dates, strings, and numbers.

Now, regarding the placement of classes that implement the strategy pattern in this architecture:

Considering that the implementation of the strategies involves business logic that may require access to different services, it can be challenging to avoid circular dependencies

I always struggled on where to put those Design Pattern objects (It is similar with my factories, my builders, states ...)


Solution

  • Considering that the implementation of the strategies involves business logic that may require access to different services, it can be challenging to avoid circular dependencies

    It is possible to create a layer which can be called as Common and put all classes that may be required by different services. Having such a library, it is possible to avoid circular dependencies.

    I always struggled on where to put those Design Pattern objects

    You don't have to create a separate library to place design patterns there. Design patterns are the way to solve some design tasks. E.g.: you have a lot of classes and you want to apply some Factory pattern. There is no need to create a separate library for Factory pattern, what you do is just create classes which are implementation of this pattern.