Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Option 2: Elevator

To build the software that controls the movements of an elevator, I would start by designing a
class that represents the elevator itself. This class would have methods for each action that the
elevator can take (e.g. go up, go down, open doors, close doors). These methods would invoke
the corresponding functions in the third-party library that controls the actuators.
I would also design a class to represent the elevator controller, which would be responsible for
managing the state of the elevator and determining which actions to take in response to button
presses or sensor readings. This class could have methods for each button or sensor, which
would be invoked when the corresponding button is pressed or sensor is triggered.
For example, if the up button on the first floor is pressed, the controller could check the current
position and direction of the elevator, and if it is not already moving up, it could invoke the "go
up" method on the elevator. If the elevator is already moving up, the controller could ignore
the button press.
To follow best practices for data structures and design patterns, I would consider using the
following patterns and data structures in the implementation:
• Singleton pattern: To ensure that there is only one instance of the elevator controller, I
could use the singleton pattern to design the controller class. This would ensure that
there is only one point of access to the controller, and that all requests to the elevator
are routed through a single, shared instance.
• Command pattern: To decouple the buttons and sensors from the actions that they
trigger, I could use the command pattern to encapsulate each action as a separate
object. This would allow me to easily change the behavior of the elevator by swapping
out different command objects, without modifying the button or sensor classes.
• Observer pattern: To allow multiple classes to be notified when the state of the elevator
changes (e.g. when the doors open or close, or when the elevator moves to a new
floor), I could use the observer pattern to implement a notification system. This would
allow me to add or remove observers as needed, and to broadcast updates to all
subscribed observers when the state of the elevator changes.
• Queue data structure: To store requests made by passengers to go to a particular floor, I
could use a queue data structure. This would allow me to process the requests in the
order that they were received, and to implement more advanced behavior such as
prioritizing requests from passengers who have been waiting the longest or who are
going to a floor that is closer to the current position of the elevator.

You might also like