[Experience] Looking at OO and PO from the elevator

This article involves personal understanding of object-oriented (abbreviation: OO) and process-oriented (abbreviation: PO), and discusses it through the elevator.


Start with a moving elevator

  • The first step our goal is to write an active elevator, the requirements are as follows:
    • Can move to floor by telling him what floor to go to.
    • Can tell if mobile is legal or not
    • Can tell you what floor he is on now.
  • PO writing:
    • Variable: floor: store the current floor.
    • Constant: MAX: Stores the highest floor.
    • function:
      • moveTo: Move the elevator to the target floor.
      • isLegal: Determines whether the elevator target floor is legal.
  • OO writing:
    • class:elevator:
      • Variable · private: floor: store the current floor.
      • Constant · Private: MAX: Stores the highest floor.
      • Function · public:
        • Constructor and Destructor.
        • moveTo: Move the elevator to the target floor.
        • isLegal: Determines whether the elevator target floor is legal.
        • getFloor: Returns the current floor.

There is no big difference between OO and PO here. It can only be seen that OO has one more class than PO, a constructor and destructor, and a getFloor function. Are these redundant?


the first guest in the elevator

  • This time our elevator welcomed the first guest. The task of this meeting is also very simple. We need to send the guest to the designated floor and then pick up the next guest. The requirements are as follows:
    • Accept the direction requested by the guest and move the elevator to pick up the guest.
    • Accept the requested floor of the elevator and move the elevator to see the guests.
    • Repeat the above two steps.
  • PO writing:
    • Analysis: This time we need a new variable goal to store the guest's request on the layer. After that, use moveTo to move it, store the guests to go to several floors, then moveTo, and then put a loop on the outside, and it's OK.
    • Case code:
#include<一堆头文件> 

void moveTo(int goal){
    移动到goal; 
}

bool isLegal(int goal){
    判断是否合法; 
}

int main(){
    int goal;
    int floor;
    
    const int MAX;
    
    while(有输入){
        处理输入;
    } 
    
    return 0;
}
  • OO writing:
    • Analysis: Add a private variable goal to the class, add the public method setGoal, change the method moveTo to private
    • Case code: (non-existent)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325228570&siteId=291194637