I am not so much used to OOP designing, So I took one of the very commonly used interview question , "Designing the Elevator System". Below is the prototype.
I would like to get some feedback on the design like
- Best practices which will have a particular aspect
- Flexibility in the code
- Some important generic feature missing
- Areas to concentrate to write better code and design
I know that these kind of things have no finite answer or just one answer but I would like to know my required areas of improvement.
Original Design:
class elevator{
private:
//The Lift box the elevator controls
liftbox box;
//The total number of levels
const int LEVEL;
//The request for various floors
set<int> req;
//Triggers the search to move to the next floor if required
void moveLiftToNext();
//Instructs the lift box to move to that floor
void moveLiftTo(int);
public:
//Adds request to the queue
void addRequest(int);
//Gets the total levels the box can move
int getLevel();
//For Emergency
void setEmergency();
void unsetEmergency();
};
class liftbox{
private:
dailpad pad;
elevator ele;
int currLevel; //Current position
bool direction; //True - up ; False- Down
public:
//Instruction to move the box to certain level
void Move(int x){
if(x < 0 || x >= ele.getLevel())
return; //invalid number
//Move the lift
//update direction and currLevel
}
//returns the current level. Used by Elevator
int getCurrLevel();
//returns the current direction of movement.Used by Elevator
int getDirection();
void setEmergency(){
//Do the required
ele.setEmergency();
}
void unsetEmergency(){
ele.unsetEmergency();
}
//can passed to the elevator
void addRequest(int);
};
class dailpad{
private:
//Some DS to represent the dailpad
//Lift box is belongs to
liftbox box;
public:
void recieveCommand(int x){
//Depending on the value we can do the following
box.setEmergency();
//or
box.unsetEmergency();
//or
box.addRequest(x);
}
};
Updated Design:
class elevator{
private:
//The Lift box the elevator controls
liftboxControlUnit & mLiftBoxCtrlUnit;
//constructor
elevator(int Level=1, int NoOfBanks =1 );
//Destructor
~elevator();
//Triggers the search to move to the next floor if required
void moveLiftToNext();
public:
//Adds request to the queue
void addRequest(int FloorNumber){
//Add the request to the queue. The single button outside the elevator door
mLiftBoxCtrlUnit.addRequest(FloorNumber);
}
//For Emergency. Should be accessible to everyone !
void setEmergency();
void unsetEmergency();
};
typedef enum Direction{
UP,
DOWN
}direction;
class liftboxControlUnit{
private:
//The request for various floors
set<int> mRequestQueue;
//The various banks for the whole system
vector<Bank> mBanks;
//The total number of levels. Remains the same for one building
const int mTotalLevel;
//Instruction to move the box to certain level
void processRequest(){
//Do the logic to move the box.
}
//can passed to the elevator
void addRequest(int x){
mRequestQueue.insert(x);
}
//Can be set by elevator class
void setEmergency(){
//Do the required
//Set Emergency on all Banks
}
void unsetEmergency(){
//UnsetEmegency on all banks
}
void emergencyListener(){
//Listen to all the banks if emergency has been set
}
void BankFreeListener(){
//Listen to the banks if any is free
//If so then
processRequest();
}
public:
//Constructor
liftboxControlUnit(int TotalLevels, int NoOfBanks): mTotalLevel(TotalLevels){
for(int i=0 ; i lessthan NoOfBanks; ++ i)
mBanks.push_back(Bank(0,UP));
}
friend class elevator;
};
class Bank{
private:
//The dailpad inside the bank
dailpad & mpad;
//Current Location
int mPresentLevel;
//Current direction of movement
direction mDirection;
//Currently moving
bool mEngaged;
//Manipulate the bank
void move(int NoOfMoves){
setEngaged();
//Move the elevator
unsetEngaged();
}
//getters
int getPresentLevel() const;
int getDirection() const;
//setters
void setPresentLevel(int);
void setDirection(direction);
//Manipulate the engaged flag
bool isEngaged() const;
bool setEngaged();
bool unsetEngaged();
//For emergency
void reset();
//Dailpad Listener
void dailpadListener(){
}
public:
Bank(int StartingLevel, direction Direction): mPresentLevel(StartingLevel),
mDirection(Direction),
mEngaged(false),
mpad()
{
}
//For emergency . Should be available for all.
void SetEmergency();
void UnsetEmergency();
bool isEmergency();
friend class liftboxControlUnit;
};
class dailpad{
private:
//Some DS to represent the state . probably a 2D Array.
void renderDisplay();
public:
//Constructor
dailpad();
void getCommand(int x){
//Depending on the value we can do the following
//Make necessary changes to the display
renderDisplay();
}
friend class Bank;
};
elevator design
interview question is more about design than actual coding. It is designed to see if you understand how different parts in a complex system can interact with each other. \$\endgroup\$