Vernacular design pattern-Practical Application-chain of responsibility (responsibility chain) and factory pattern
Source: Internet
Author: User
I haven't written it for a long time. I am sorry for the poor writing. I hope that I can communicate with like-minded people.ProgramPersonnel packages are improved together. With regard to the design model, I adhere to the principle of "useful learning, useless putting. I think it is hard to remember a design pattern that you have never used before and have never realized it in practice. Even if you remember it, there is no way to apply it effectively. In the course of learning, I think those classic instances are not really (of course, they are just personal feelings, huh, huh). They feel like frauds, and they are easy to understand when learning, I forgot to take a nap. I will explain the business logic properly when writing these practical design model instances. After all, software is used to solve business problems, the design pattern is ultimately designed to better solve business problems. Today, I am talking about the responsibility chain model and the factory model. I have found that the factory is the most widely used. The end of the year is approaching. Enterprises need to sort out the cost for one year and pay bills to those customers who are in arrears. There is a bill record in the system: "A customer, annual fee, 30 yuan, unpaid ". There is also such a work order: a customer, weeding, 20 yuan, unpaid, waiting for payment. There are other types of records that depend on the cost, and you can start to work only when you have the cost. Then, after the customer pays the money, the system will go back to the job for the payment based on the type of the customer's payment (start it, or record it as paid, or end it, etc, depends on the real business logic ). Here, if the customer pays 30 RMB for the annual fee, find the poster for the annual fee and change the record to "a customer, annual fee, 30 RMB, paid ". If the customer pays 20 yuan for weeding, find the poster for weeding, change the record to "a customer, weeding, 20 yuan, paid, in progress", and print out the work order. Based on the business, we assume that the responsibility chain model is used to solve this problem. After the customer pays, perform a post action. Start to traverse all posters and find the corresponding poster Processing Based on the Payment type. If no poster is found, enter a default poster for default processing. So how to design it? First, we create an abstract poster. All posters dedicated to problem handling inherit from this abstract class: It contains a post pointer nextposter pointing to the next poster; An abstract method post (receipreceipt), which is passed in with a payment order, is not implemented; A boolean hasnext pointer indicates whether a poster exists; Hasnext = false is set in the default constructor; Nextposter = P, hasnext = true in the poster (poster p) constructor. Then we construct two poster, yearlycostposter and workorderposter. The two posters implement the poster abstract classes respectively and use two constructor classes: one is the default constructor and the other is the base class constructor. When the POST method is implemented, the pseudo code is as follows: If is the current type of fee then Processing Else if hasnext = true then nextposter. Post (receept ). Build another defaultposter. Here we can do nothing. If we use a factory to create a poster, we need Create another poster factory posterfactory In the factory, we construct a static constructor getposter to return a poster.CodeAs follows: Defaultposter = new defaultposter ();
Yearlycostposter = new yearlycostposter (defaultposter );
Workorderposter woposter = new workorderposter (yearlycostposter ); Return poster; In this way, an abstract poster is defined in receept, and posterfactory. getposter () is used in the constructor to obtain the desired poster. After receiving a payment order, you can call receipt. Post (). The class diagram is as follows: The code is simple and will not be detailed. Welcome to the discussion.
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