A terminal-based stopwatch application written in Python.
This project demonstrates several Object-Oriented Programming (OOP) principles:
1. Inheritance
- Applied by creating a base class
Timerand a child classStopwatch. - Location:
class Stopwatch(Timer)instopwatch.py. - Benefit: Allows code reuse and logical organization of timer-related functionality.
2. Polymorphism
- Achieved by overriding abstract methods
startandstopin theStopwatchclass. - Location:
def start(self)anddef stop(self)inStopwatch. - Benefit: Enables different timer types to implement their own behaviors while sharing a common interface.
3. Encapsulation
- Private attributes (e.g.,
__last_elapsed) and getter/setter methods are used to protect internal state. - Location:
self.__last_elapsedandget_last_elapsed()inStopwatch. - Benefit: Prevents direct modification of sensitive data, improving reliability and maintainability.
4. Abstraction
- The base class
Timeris defined as an abstract class with abstract methods. - Location:
class Timer(ABC)and@abstractmethoddecorators instopwatch.py. - Benefit: Hides implementation details and enforces a contract for subclasses, making the codebase easier to extend and understand.
These enhancements make the project more modular, extensible, and easier to maintain, while providing a clear structure for future improvements.