In fact, your question has two meanings.
which should be reused in many classes
This can be the context of a design pattern (reusable component) or memory (instantiation of a class). Speaking from two different points of view:
The cost of memory (I have little experience with this, but let me share my experience)
This section actually covers only 2 types of instance.
Static first (or creating DI at the root of the composition)
- Bright instance creation means that the whole class will be created when the application starts
- One-time instantiation only
Non-static
- Lazy instantiation, means that the class will be created only if necessary
- Once for each use
In short, a static one will be expensive if the class is many, and a non-static one will be expensive if the request is high (for example, inside a loop). But this should not make your application heavy. Most operations in java / csharp create objects.
Class reuse
1 - mega monolithic code (one class of gods is able to do almost everything)
Benefits:
- Itβs easy to search for the code (it still depends on that), you know that every logic lies there, so you just need to look at this large class.
- If it's static, you can just call it anywhere without worrying about instantiating
Disadvantages:
- Any modification to one method creates a risk of error in other places.
- Violates SRP, means that this class can be changed for various reasons, not just one
- In particular, when managing versions, it is more difficult to combine if the modification occurs in separate branches, which leads to increased code synchronization
1a / static class / single template
Benefits:
- Ease of use
- Can be used anywhere (link, call and execution only)
- No need to instantiate an object
Disadvantages:
- Itβs difficult to unit test (itβs difficult to mock, and recently you find that it takes time to prepare the test environment. Especially with the data
- If the state is a state (state), it is difficult to determine the current state during debugging. In addition, it is difficult to determine which function changes the state in which it can be changed everywhere.
- As a rule, there are many parameters (perhaps around 5-11).
Some point about static class: see this question
2 strategy template
Actually it has the same design with 3 or composition over inheritance .
3 dependency injections
Benefits:
- Easy to mock and unit test
Must be stateless . It is easier to debug and unit test if the class has no status.- Support for refactoring
Disadvantage:
- Itβs hard to debug those who are not familiar with the interfaces (every time you redirect a method, it goes to the interface)
- Creates a layering that will display
State / stateless
I think that states play important rules in the design of your application. Typically, developers try to avoid the presence of states in the code of business logic, for example:
// get data if(request.IsDraft){ // step 1 // step 2 } else{ // step 1 // step 3 }
Developers tend to put logic in another stateless class, or at least methods such as:
// get data if(request.IsDraft){ draftRequestHandler.Modify(request); } else{ publishedRequestHandler.Modify(request); }
It will provide better readability and simplify modification and unit tests. There is one state pattern or hierarchial state machine pattern design template, especially for handling such cases.
Principle of shared responsibility
IMHO, this principle has the greatest benefit, if followed. Benefits:
- In the version change it is clear which class has been changed and why
- In DI, you can connect several smaller classes, creating flexibility both in use and in unit test
- Increased modularity, low grip and high grip
TDD (Test Driven Development)
This design does not guarantee that your code will not contain errors. In minus the estimated time at the design stage and overlay it has the advantage of:
- Easy mock object for unit test
- Easier to refactor due to unit test and modularity
- Easier to maintain / expand
Some useful sources
Locator Anti-Virus Antivirus Software
Using Decorator for end-to-end care
My 2 cents:
The advantage of using an interface (also used for composing inheritance)
Perform Design Design / DI Down
Final thoughts
These projects and strategies are not the key that will determine your application structure. He is still the architect who will define him. I prefer to follow some principles, such as SOLID, KISS and GRASP, instead of deciding what is the best structure. Injection Dependency is said to adhere to most of these principles, but too much abstraction and improper component design will result in the same misuse of a singleton pattern.