Hi,
For this blog post I have chosen the topic design patterns in software construction. I watched the video linked below which is titled Design Patterns in Plain English | Mosh Hamedani by Mosh which does a great job of showing how design patterns in software construction work. https://youtu.be/NU_1StN5Tkk?si=aFdc2v01YIvoGq0m
Firstly, we must understand what exactly a design pattern is with respect to code. Design patterns are reusable solutions to common problems in code. From here we can make the inference that the goal of a design pattern is to build reusable and extensible software. To help us achieve this, there are three main categories which are creational, structural and behavioral patterns. Creational patterns regard different ways to create objects. Structural patterns are about the relationship between those objects. Behavioral patterns are about the interaction/communication between objects. From these categories we would choose which one to use based on the specific problem to implement the design pattern.
To help with the process of a design pattern, you would want to use a UML diagram to better visualize and understand the whole code. This way you will be able to see the type of relationships from one body to another; whether it’s inheritance, composition, and/or dependency to name a few. Something you may see on the UML diagram could be an interface.
An interface is a powerful tool that is often used in design patterns. Furthermore, they promote loosely coupled apps and flexibility. With an interface in the design pattern it strives substantially towards the overall goal which is to build reusable and extensible software.
Mosh then goes on to discuss and show the four principles of object-oriented programming, which are encapsulation, abstraction, inheritance, and polymorphism. Encapsulation deals with bundling data and the methods that operate on it into a single unit or class and hiding the values or state of an object within a class. In coding, abstraction is in regards to reducing complexities by hiding unnecessary details in classes. A great example of abstraction to better understand is thinking of a tv remote control; it shows only what you need to see. The next one is inheritance. Inheritance is a mechanism for reusing code across classes including common behavior. Try thinking of a parent to a child class. The child class inherits the parent class, but also has instances and methods of their own. Polymorphism is the ability of a single object to take many different forms. Mosh stresses that you must know and understand these principles as they are crucial to building design patterns.
Some takeaways are understand the problem you are facing first, have a strong foundation in OOP, and know when not to use a design pattern. Furthermore, creating multiple classes or a new interface just for one or two actions of a given state is simply not worth it; go with the else if statement.
From the blog CS@Worcester – Programming with Santiago by Santiago Donadio and used with permission of the author. All other rights reserved by the author.