Strategy mode: Take the changed parts out and encapsulate them

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.