Programmer Xiao Ma just graduated to Shanghai, began the life of magic, only a poor little salary every month, because just graduated, finally found a job, a monthly salary of 4500 yuan. For the province rent, so live relatively far, every day to sit two hours of subway commute!
Pony is a foodie, like to eat like travel, after work lie in bed, thinking if this project completed:
If you can pay 5000 bucks , you can get a big dinner from the next-door department girl,
If you send 6000 bucks , you can buy a pair of sneakers.
If you send 7000 pieces , you can go to Hangzhou for a few days, beautiful scenery of the West Lake picturesque
Think about, the heart flattered, simply write a program to remind me how good ah! Light said no practice fake bashi, pony began to write Python code ...
01. Using a simple observer pattern
Although the small horse is just graduated from the rookie yards, but the school is still very hard, Remember the teacher once taught him some of the idea of writing code , think of code can not write too bad ah, to facilitate the expansion, this time simply with the teacher to teach us the design mode to do it!
Observer mode Most are a one-to-many relationship, ! Here we have two patterns, , once the salary is issued, we judge according to the number of wages, we are to eat a big meal, or to travel, it sounds very simple, I code:
1). Listener Object
Salary is the kind of listening class we designed, inside several functions, the get and set method of setting the wage
There are two very important methods, one is to increase the observer, and the other is to notify (once a certain condition is met, it can be immediately notified via this interface)
2). Two observers
First, an observer's parent is abstracted, with an update notification function
Then we write the foodie mode, we rewrite the update function, put the meal and buy new shoes logic in the inside
Finally write the travel mode, but also to rewrite the update function, the travel logic in the inside, more than 7000 pieces can go out the waves ~ ~
02. Write a test code and try it.
It says so much, let's run it in code and see how it turns out.
Initialize 3 objects, wage object salary, foodie object Eatobser and tourist object Travleobser
The salary object is monitored, we will add two observers eatobser and Travleobser
Finally look at the August salary, we randomly a set of wages to look at:
Currently Salary is:5500
>> can go to the next group of Lily mm, eat a big meal, haha!
Currently Salary is:6000
>> the original pair of sneakers are broken, always have to buy a new pair of
Currently Salary is:6500
>> the original pair of sneakers are broken, always have to buy a new pair of
Good, the programmer's life is actually very simple, the programmer is also very lovable, above is a very typical life case uses the python to describe, hoped everybody likes! If you have any good ideas, welcome to speak with Python code, message discussion ~ ~
How does a yard farmer live in Mordor? I used the Python analysis to get the following results