Design patterns in Software Engineering.
As I go deeper into my Software Engineering journey, I am coming across things that I’ve heard mentioned in bootcamp but never paid much attention to exactly what it entailed. One of those things is design patterns. In software engineering, designs patterns are reusable solutions to commonly occurring problems in object oriented programming. Design patterns can speed up the development process and helps to prevent issues that can cause problems. The three main design patterns are Creational, Structural and Behavioral. Creational design patterns are about different ways to create objects. Structural design patterns are about the relationship between those objects and finally Behavioral design patterns are about the interaction between those objects.
For this blog I’m only going to touch on Creational design patterns. There are several Creational design patterns; Factory Method, Object Pool, Prototype, Builder, Singleton. For the sake of keeping this blog short and keeping you interested I won’t discuss all of them.
Creational:
Factory Method
— This method allows you create all of your instances/objects in one central location. In the below screenshot I’m using plants as an example. Let’s say I own a plant shop(ya’ll know I love my plants), I will of course have all sorts of plants right? But a plant although from the same family can have a different name. Breaking down the code, since we can have many types of plants that go by different names, our function will always take in argument of name & within the function I set the type of plant. Lines 9 through 18 (forgive my formatting, I wanted to get the code in one picture), is our factory which will always have a create method. This function is responsible for creating the different plant objects. The create
keyword takes in a name & a type so that it knows which type of plant I am creating. The switch
keyword is used to perform different actions based on different conditions. Since we have different types, the switch statement tells our create which type I want to create.
Line 24 I create a new instance of my plant factory and line 25 is the empty array that I will push my plants into. Lines 27 & 28, I’m sending a new plants to the “factory” to be created. Lines 20–22 is just to show that my plant factory is in fact working. Lines 29–31, I iterate through my plants array, and use the call()
keyword on my print function. The call()
keyword is a predefined Javascript method, with call()
an object can use a method belonging to another object.
Singleton:
— Limiting an instance of an object to just one. This snippet code is to manage a process. The main components are the process and the process manager. There’s only one process manager, managing many processes(yes I know!) The below code is limiting the number of process manager instances to just one.
Lines 1–3, I’m just passing in state just for demonstration purposes, it can represent anything whether is running, stopped it doesn’t particularly matter for this situation. Lines 5–24 is how the process manager is created and limit it’s instances to 1. I’ve set Singleton to a function expression and within that function, I’ve put in an object constructor. This returns a reference to the object constructor function that created the instance object which is pManager. Lines 12–15 is where I create the instance and assign pManager to a new process manager and return pManager. Line 17–23 allows access into Singleton since pManager is only available locally. Then I have an object that has a method called getProcessManager and within that method it checks if the instance of pManager is null to then create a new instance of pManager. Lines 25 and 26 is where I create two instances of process manager and set both of them to Single.getProcessManager(). Line 28 is how I check that both of these instances are the exact same thing and so it logs true. The Singleton is very confusing but with repetition it starts to make sense. I hope this blog was helpful and not too boring !!
References :