In the previous note on python design patterns, we have seen the need for design patterns in the software development life cycle, types of design patterns, characteristics of design patterns, and how design patterns are associated with object-oriented programming (OOPs) paradigms, and why, we can’t fully leverage the design patterns concept for software development when a programming language does not support OOPs.
In this note, we will revisit the basic concept of object-oriented programming as it is one of the pre-requisite to learning and understanding design patterns.
Object-Oriented Programming (OOP)
Understanding the core concepts of OOP involve learing concepts such as Objects and Classes.
Objects
Objects repesent entities in both problem and solution. In other words, when we are building a piece of sotware, we need to have compoents representing all the moving pieces of our solution. In addition, we need to represent all these entities, interacting with the software in the problem domain.
Classes
Classes are templates to create objects that avoid making them from scratch every time we need them; consider it a cookie-cutter for replicating the objects. Classes define objects in terms of attributes and behaviors.
- An attribute represents the properties and captures the state of the entity.
- A method describes the behaviors of an entity.
For example, take a user registration system, where users are in the problem domain, and the registration or login form is to the solution domain. In this case, users possess specific attributes such as name, age, dob, address, etc. These attributes belong to a problem domain. On the other side, users can behave; differently; the behavior can be in the form of register-me web-interface, login web-interface, cancel registration web-interface, etc. These behaviors belong to the solution domain. And we need both represented to build a complete software.
Inheritance
It establishes a relationship between two classes as parent and child. The capabilities of a child class is explained below:
- A child class keeps all the attributes and methods of its parent.
- A child class can add new attributes and methods apart from entities inherited from the parent.
- A child class can overrides the existing methods of its parent.
In the above example, Animal is a parent class, whereas a Dog and Cat are the child classes that inherit Animal class. It means both Dog and Cat have Name and Sound attributes and Speak method. Both Dog and Cat can override Speak behavior with barking and meow sounds in the child class, respectively. It is the beauty of inheritance; otherwise, we need to do a lot of redundant work for both Dog and Cat classes.
Polymorphism
Polymorphism relies on inheritance and it allows us to instantiate a child class and treat it as same type as its parent. In simple words, polymorphism enables a parent class to be a placeholder for its child classes. For example, if a Dog class is instantiated, sound method should produce barking sound rathar than meow. Thus, the polymorphism produces a true nature of the object.
References
- Design Patterns in Python by Dmitri Nesteruk
- Python Design Patterns by Jungwoo Ryoo
CITE THIS AS:
“Python Design Patterns – OOP” From NotePub.io – Publish & Share Note! https://notepub.io/notes/software-engineering/python-design-patterns/object-oriented-programming-dp/
12,208 total views, 1 views today