In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to Python design a leave model, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
Because you do not use some design patterns to optimize your code, so many friends can only rewrite their own code. This time we talk about the design pattern-"responsibility mode" through the request for leave.
I. what is the responsibility model?
For example, if we want to ask for leave, we usually write a request for leave and submit it to the system! The system will automatically find the corresponding supervisor for approval according to your request for leave. The process of asking for leave is one chain after another, just like a chain, and all the people who process the request for leave form a chain of responsibilities.
Responsibility model:
The subtlety of the responsibility model is to decouple the requester from the recipient, that is, to do hierarchical processing! The requestor does not know who processes the request for leave, does not need to know the specific business logic and how many people are on the chain to process the request for leave, and just submits it, so that the flexibility and expansibility of the system are very good. If you do not believe us, we will look at the actual cases.
II. Actual combat cases
The boss asked programmer Xiao Li to design a leave system, how should it be designed? Xiao Li thought about it and used the above responsibility model. Without saying a word, draw a UML diagram to design the business logic relationship.
1)。 Design leave for human beings
We abstract the person asking for leave as an object, in which the attribute must have a name, how many days, the reason for asking for leave, and so on. So we left three parameters (name,dayoff,reason) in the Person interface.
The most important things in this class are the setLeader () and request () functions:
The purpose of setLeader is to submit the leave of absence to the supervisor, but you don't have to worry about the specific level of supervisor.
The purpose of request is to throw out the request for leave, but you don't have to worry about which level of supervisor will approve the request for leave, because the function handleRequest () is used to deal with it.
2)。 Design supervisor class
There are many roles of supervisor, such as team leader, department manager, department director, company boss, HR, executive director and so on. We abstract these people into a base class called the supervisor class.
This Manager class is the base class, mainly dealing with get and set NextHandler. That is, the request for leave is on a chain and needs to be submitted layer by layer. For example, the next nextHandler of the team leader is the department manager, and the next layer of the department manager is the boss of the company.
3)。 Specific role classes
For example, we designed this leave system with TeamLeader,DeptMaanger,Director and so on.
Each has different rights, such as:
TeamLeader approval within 2 days of asking for leave
Ask for leave for 2-10 days. The department manager applies.
If you ask for leave for 10-22 days, the director of R & D will apply.
Let's give an example, such as the group leader. It mainly rewrites the class handlerRequest. (notice that there is a small blue circle on the left side of Pycharm, indicating that the function of the parent class is overridden.)
DeptMaanger is similar, mainly because it is different from the number of days of approval.
The class of HR mainly deals with registration and filing:
Third, test the model
After the heavy design of the above class, our model should be ready to run. Let's test it with a few leave notes:
> >
Leo apply for leave for 2 days, reason: attend Google conference
Agree to Leo and ask for leave. Signed by: Eric, (team leader)
The application for leave has been reviewed and the situation is true! It has been dealt with for the record. Handler Tina: executive director
Susan applied for leave for 10 days, reason: to travel to Europe, but also to Japan to soak in hot springs
Agree to Susan and ask for leave. Signed by: Leo, (R & D Manager)
The application for leave has been reviewed and the situation is true! It has been dealt with for the record. Handler Tina: executive director
Lili apply for leave for 22 days, reason for leave: sick rest
Agree to Lili and ask for leave. Signatory: Lao Wang, (company boss)
The application for leave has been reviewed and the situation is true! It has been dealt with for the record. Handler Tina: executive director
The above content is how to Python design a leave model, have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 274
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.