"Status Software Development" Learn Note 1. Object-oriented Program Design Thoughts In computer programming, programmers use data that may have many data and processes for these data, these processes and data The relationship is intricate, programmers to deal with these complex relationships often consume great energy, object-oriented design ideas are effectively organized together, form a relatively independent individual-object, The programmer is used inside the object, and the programmer is to generate the connection between objects and processing objects. Although object-oriented techniques have been very close to people's thinking and understanding process, object-oriented only reflects a static process. The same processing process may be triggered or called in different operating conditions. Example Class Carbeginmember: Wheel // Wheel Steering Wheel // Steering Put Engine // Engine DOOR / / DOW Method: startcar // ignition Run // Drive Accelerate / / Acceleration DECELERATE / / Deceleration Closedoor // Close OpenDoor // Open Door End We The initial state that is now required to turn the vehicle speed to 60km / h, and the initial state that may appear is: 1. The speed is now 90km / h; II. The speed is now 30km / h; three, the car stops, the engine is turned off; four, the car stops, the engine Falling, the door is not closed; and so on. So, for these four situations, the operation we have to do is different. The first case is deceleration; the implementation process is: procedure fn1begindelerate // Deceleration END The second case is acceleration; Procedure Fn2BeginAccelerate // Accelerates END third case first is ignition, then accelerate to 60km / h; procedure fn3beginstartcar // Ignition Run // Start accelerate // Acceleration END and the fourth case is the first door, then ignite, and finally accelerate to 60km / h; procedure fn4begincludedoor // Close Startcar // ignition RUN // Start Accelerate // Acceleration END In the four cases, the first, the second is to change the vehicle speed, one is a positive direction, one is an anti-direction, but it belongs to the same operation; the third case, there is more execution process - ignition, the same Contains an operation of changing the speed; the fourth case, there are more shutters, the ignition of two processes, and also contain the operation of changing the speed. Their commonality is all the process of changing speed changes, because this operation is the core operation of the vehicle speed state change. Further analysis of the summary can find that these four situations include a common situation, they must have two common constraints: one. The door must be closed. The engine must be in the ignition state because of the first, second cases implied to satisfy these two conditions, and the third case implies the second condition, and the fourth condition is not satisfied. Based on this feature, we can envisage the following mechanisms: Abstract 3 independent operation of the car: motor state, door state, engine state. Each status is with one operation process and it corresponds. The operation procedure corresponding to each state is responsible for its core operation. The operation process corresponding to each state must be responsible for the detection and setting of its constraint. This will be greatly simplified, and the programmer does not have to consider how to achieve a different process under different conditions, only need to care about the constraint relationship between the individual states in the object, and the core operation of each state change Realize the process.
This is the state-oriented programming idea, which can resolve the correlation between data and data, and transform some complex logical relationships into a simple logical relationship, making the program's design, modification, and maintenance. Examples of the above-oriented idea can be expressed as follows: Class Carbeginmember: Wheel // Wheels SteeringWheel // Steering Put Engine // Engine Door // Doors ... Method: startcar // ignition shutdown // 熄 RUN // Drive Accelerate / / Accelerate DECELERATE // DeceleDoor // Close OpenDoor // Open the door ... setspeed // VETEE SETDOOR // Various door status setEngine // Correspondence engine state ... State: Speed // Speed Status Isdooropen // Door Status iSturnon / / engine state ... ... EndProcedure SetSpeed (X) BeginIsDoorOpen = False // constraints, actual / execution SetDoor (False) IsTurnOn = True // constraints, actual implementation SetEngine (True) If Speed> XDecelerateElse If Speed = 0RunAccelerateElseAccelerateEnd IfEndProcedure SetDoor ( Bool) Beginif IsDooropen! = Boolif ... // Turn on the door constraints, there is no ... end ifif isdooropen = trueclosedooorelseopendoorend iFend iFend
Procedure setEngine (BOOL) Beginif IsTurnon! = BOOLIF ISTURNON = TrueshutdownelsestartCarend IFEND IFEND Requires the speed to 60km / h, we just need to do SPEED = 60 // Execute setspeed (60) readers can try to use structured programming methods and Object-oriented design methods draw the entire process of performing the statement, comparing the description of the pseudo code above, it seems that this simple logical use of other design methods will become complicated. Its root cause is to write this pseudo code above, you don't need to think at all, because this is "reality", this is your thinking. It is your thinking, of course, does not have to reflect. Although the use of a state-oriented method seems to be much longer than the object-oriented method, it is very convenient when using it, it is much simpler, and it is much intuitive. Moreover, this object already contains all current possible different environmental and conditional restrictions. Logically also more in line with human thinking, more simple, modified and maintenance is relatively simple.