Consider a scenario in which we need to deliver a project to the customer and the project is about to be completed or is already completed. Now the customer is always changing their mind and they continue to change the requirements. When we added the new code we encountered the following:
So design patterns are useful here. We need to design the code in such a way that, when changes to the requirements happen, the changes to the code should be minimumal. Patterns can be used across multiple domains and used to solve recurring problems, multiple domains means in our daily life when possible.
Key Concepts used for Design Patterns
Abstraction
Focusing upon key or essential characteristics of objects, the object does not hold all of the characteristics. An object is an entity that has a specified number of characteristics. Abstraction is used for hiding the unwanted data or characteristics and giving only relevant data.
Encapsulation
It hides the internal details of objects. Encapsulation is hiding the code and data into a single unit to protect the data from the outer world.
Abstraction and encapsulation are complimentary to each other. Abstraction is a thought process, it's nothing, but what is necessary. Encapsulation is the actual implementation in which abstraction is implemented.
Polymorphism
Polymorphism means many forms of a subject or an object.
In polymorphism we will declare methods with the same name and different parameters in the same class or methods with the same name and same parameters in different classes. Polymorphism has the ability to provide various implementations of methods that are implemented with the same name.
Polymorphism is of the following two types:
Compile Time Polymorphism (called Early Binding or Overloading or static binding).
Run Time Polymorphism (called Late Binding or Overriding or dynamic binding).
Modularity
Modularity divides a system into cohesive and loosely coupled modules. Modules can be considered to be divided into sub-modules. For example, a typical banking application can have various modules such as funds transfer, accounts, cheque payment and so on.
Each module should be self-contained and most of the modules would work independently and if dependent on other modules. Each module can work together to achieve the objectives of the application or finally get the job done.
Namespace
Namespaces group classes together logically into one logical structure.
Inheritance
Inheritance is the property of getting all the properties and behaviours from the base class to a child class. Inheritance gives us code reuse, it extends and modifies the behaviour defined in the base class.
Association
Association is a relationship where two components are linked to each other. This kind of relation is also referred to as a using relationship, where one class instance uses the other class instance or vice-versa, or both may be using each other.
They don't have no owner, have their own lifetime and child objects are all independent of each other.
Example
Employee–Skill set relationships, Student-Course relationship.
(An employee can have multiple skills and each skill can be owned by multiple employees.)
Aggregation
Aggregation is the same as association but with the additional point that there is an ownership of the instances. The inner object lifetime is not controlled by the outer object. For example, a classroom to student relationship. if the classroom is destroyed, the student will be part of a new classroom and will not be destroyed. Here are some other examples.
Student-Classroom
Engine-Car
Composition
Composition means one object is contained in another object. A child object does not have their own life cycle, it depends on the parent's life cycle. The parent class is responsible for creating or destroying the child class. The inner object lifetime is controlled by the outer object.
Here are some examples:
Books-Library (If the library is destroyed then the books are also destroyed.)
companyLocation-Company (If the company is closed then the locations are also closed.)
Buttons-Windows (If the window is closed then the buttons are also closed.)
OrderDetails-Order (If the order is cancelled then it makes no sense to look at the order details.)
Diagrams for Association Composition and Aggregation