As a software engineer or programmers when we work on big projects we may face a lot of problem some are small and some are big that we can not solve it by our own. That why in this digital area we see a lot of website or apps like stackOverflow was created in a purpose to allow developer to share their solution because they may face the same problem.
It seem not difference for design pattern is the repeatable solution that the programmers always meet in the development phase. So in this article i will share some design pattern that i think it is importance to implement.
1.Factory Design pattern
Factory design pattern it is a creation design pattern that use to create the object dynamicly through interface.
+ When to use this Design pattern?
- when we want to create a dynamic class
- we don’t want the user to know all the subclass
- we don’t know before what type of class that we are going to create
- when we want to centralize the creation of object within a class(factory class)
- To encapulate the creation of ob
+ Real life Example
For example we create a system for an international school that have all asian students so when we want to add a new student we don’t know before hand what nationality of that student we need to ask them. In this case we will use if else / switch case to create object. if we write all if else statement in the main it will mess up our code. So that why factory design pattern came in because we can write if else statement in our factory class.
This is the github link that implement Factory design pattern: Factory design pattern
2. Builder Design pattern
Builder design pattern is a design pattern that simplify the object creation when we have complex attribute of object . It will separate each attribute to a builder.
+ When to Use
- When an object has a lot of attribute
- number of attribute is dynamic
- when the user want to customize the attribute of the object
- hide the createtion from client only the builder class know
+ Real Life Example
- we use this type of problem when we want to get one object by building it step by step
- For example : when we want to build a robot and we want a stakeholder to decide what type of robot they want (oldstyle,modern,fast) after get the requirement from customer Engineer analyse it and choose which team of builders will doing this task. So it Engineer choose oldStyle Builder they will built an old style Robot step by step until finish.
This is the github link that implement Factory design pattern: builder design pattern
3. Adapter Design pattern
Adapter design pattern is a design pattern that allow two uncomparetable interface working together by using adapter interface.
- simple definition: adapter allow us to convert from one type to another type
When To use Adapter?
we use adapter when :
- we want to subclass of difference interface can use the same function
- when client has an expected interface or type
+Real life Example
we should use adapter when we want two difference type of object working together and can use the same functionality
Example: we save a document from the internet. The document was in PDF file but we want to edit it . How we do it? we will use some converter to convert that file to doc file so we can edit it.
This is the github link that implement Factory design pattern: Adapter Design pattern
4. Facade Design pattern
Facade design pattern allow us to hide a lot of complexity step to achieve the goal by showing only simple step.
+ When to use this Design pattern?
- we want user more easy to use application by using simple function
- to hide some functionality from user
+ Real life Example
For example : In order to close a computer or open the computer . computer has done a lot of things. But to make it easy and friendly with user we should hide a lot of complexity function that the computer does like (loading screes, electricity…).
This is the github link that implement Factory design pattern: Facade Design pattern
5.Decorator design pattern
Decorator design pattern is a design pattern that allow us to add functionality to our object dynamicly. Not to add in the original class but to create new code.
+ When to use this Design pattern?
- we want to modify functionality of object at runtime
- we don’t want to mess up with our original code by adding new functionality
- rather that rewrite old code we can extends new code
+ Real life Example
we should use decorator design pattern when we want to add or change functionality of object dynamicly:
For Example: in a coffee shop we are selling a coffe then we calculate the cost for creating the coffee but let’s say a customer they want additional meterial to coffee like fresh milk…etc.
Should we add new functionality to original class? if we do this we will add new functionality add remove it each time the user no need that anymore. To avoid this we use decorator design pattern so we will able to create new functionality class then add it to our original object when we need.
This is the github link that implement Factory design pattern: Decorator Design pattern
6. Chain Of responsibility
it is a design pattern that allow us to create a group of object the work together in a form of chain. That mean when one object can not complete the work. it will send the work to next chain.
+ When to use this Design pattern?
- we want a group of object work together in a form of chain.
- one object can work instead of one object
+ Real life Example
Example: In school we may have a president if a president give some task he will give to class monitor but if this task can not complete by class Advicer. Class Advicer will give the task to class monitor if class monitor can do the task he will do if not he will send to forum leader . That why we call this is as chain of responsibility.
This is the github link that implement Factory design pattern:
7.Strategy Design pattern
It is a design pattern that allow us to use one of serveral behaviour in the list dynamicly.
+ When to use this Design pattern?
- we have many object have similiar behaviour in the list
- when we want to use serveral behaviour in the list dynamically
+ Real life Example
for example we have a zoo and in that zoo we will have animal have difference behaviour so we will not create new object or subclass each time we just create a list of behaviour and all the object can use
This is the github link that implement Factory design pattern: Stratergy design pattern