I believe that most programmers use only one pattern in Java-developed projects: MVC, which divides the project into Controller,service,dao three layers. No matter how complex business logic is plugged into the service layer, the result is that the service layer's approach is bloated and full of various if and switch logic-judged branches. For a long time, even the developer himself forgot what to do in the method. When the business logic changes, it becomes a very difficult and error-prone thing to change this piece of code.
Unfortunately, business logic is the most frequently changed part of the entire project, so I strongly recommend splitting the service layer to give me an example of the sales of telecommunications products that I have encountered in actual projects:
Telecom products have broadband, number, telephone, traffic and many other kinds of, it is clear that the sales of each product business logic is not the same, such as the sale of broadband products need to record the customer's home address, the appointment of door installation time, sales call only record customer recharge mobile phone number and so on. And the business logic is often changed (often with promotions), which is very well suited to design with a strategy model, and I use a strategy model to deal with this kind of problem. Take a look at the picture:
ProductS Ervice is an abstract class that represents service , which includes a SellBehavior member, Sellbehavior interface encapsulates business logic for product sales, such as one of its implementation classes sellbroadband is the business logic for broadband product sales.
Broadban Dservice inherit < Span lang= "en-US" >productservice , < Span style= "font-size:14px" >service , giving sellbehavior member Assembly Span style= "Font-family:liberation serif,serif" >SellBroadband The class.
It can be seen that the business logic of broadband product sales is encapsulated in the sell method of Sellbroadband , while other products, such as the business logic of phone sales, are encapsulated in Selltelephonefare 's in the Sell method. This makes different product sales methods independent of each other, business logic is clear and easy to change, when the need to add new products, only add a sellbehavior interface Implementation class, there is no impact on existing classes.
Strategy mode: Take the changed parts out and encapsulate them