Design Patterns: Write modular and Concise Code
server side digest
Posted on April 12, 2023
In earlier Post we discussed about two patterns: Strategy and Observer Pattern.
In your daily use, you'll mostly come across Strategy and Factory Pattern. Observer pattern is more use case specifically. But today we'll take other two most important design patterns i.e. Factory and Decorator pattern.
Yes, you read right. If your code design/structure is bad it means you are not a well behaved person š
Now, the point is you don't need to remember all the design patterns out there in the market. We'll cover four patterns in total and will move on to Designing the real world problems like:-
- Parking Lot System
- Instagram Design
- Netflix Low level design and more....
š Factory Design Pattern
Coming to the Factory design pattern, It is a kind of factory which produces products and return to us. In more technical terms let's take an example of a Logistics Platform.
Let's say you started a Logistics company that is a platform. In the start you only support delivery by Truck and you have defined a Truck class which handles the delivery through the truck.
Now, in future our company got bigger and we are getting demand to deliver using ships. Pretty good news for the company right š„³? But for the developers it might not be a good news, Because to add one method of delivery we need to change a large chunk of code.
Now, We created the design of our code like:-
Image Ref: Refactoring Guru
Now we can get different implementations from the factory as per our need. In future whenever we need to add new delivery method we just have to implement the interface and can add functionality further.
Image Ref: Refactoring Guru
This is the most common pattern that you will come across in your day to day development
public interface Transport{
public void deliver();
}
public class Truck implements Transport{
private String truckType;
@Override
public void deliver(){
// Delivery by Truck
}
}
public class Ship implements Transport{
private String shipType;
@Override
public void deliver(){
// Delivery by Ship
}
}
Here, you can see to add new method of delivery we just have to implement the interface.
š Let's take an another great example.
Image Ref: Refactoring Guru
Here, we have created one factory called Dialog which returns different buttons depending on the use case. Either it can return widows or linux button. This factory kept extendable so that we can further extent it to make Windows specific dialog and linux specific dialog.
š Let's discuss Decorator Pattern
Decorator is a pattern which is used to add features on top of something. Let say there are two Pizzas
- Cheesy Seven Pizza
- Margherita Pizza
Now, here we want to add/decorate the pizzas with extra cheese and toppings. Then there will be pizzas like Margherita with extra cheese, Cheesy Seven Pizza with extra cheese and stuffing. So, it is like adding features on top of some entities.
š Here, decorator pattern comes into play. It is a pattern to add additional functionality to the existing entities without altering their structure. It is a wrapper on top of some classes.
š Let's see an example:-
Image Ref:- Tutorials Point
š Here, you can see we have a Shape interface and two concrete classes Circle and Rectangle. Now, someone wants to decorate the Circle and Rectangle with Red color. So, instead of extending individual shape we defined a decorator.
Decorator extends the Shape and have a shape included. So, it has two relationships:-
- has-a Relation (Has an object of shape)
- is-a Relation (extending the Shape Object)
Now, we can extend decorator to define different decorators on top of any shape.
In the below code example which can be seen as analogy to the above example of Shape, we have Ice Cream(Shape) and two concrete classes ChocolateIceCream (Circle) and ButterScotch Icecream (Rectangle) and a Icecream decorator which is extended by two classes which are
- RainbowSprinklesDecorator (Read Color decorator in the earlier example)
- ChocolateSyrupDecorator (Other decorator)
public abstract class Icecream{
String description;
String getDescription(){
return description;
}
abstract int cost();
}
public class ChocolateIceCream extends Icecream{
@Override
String getDescription(){
return "Chocolate";
}
@Override
int cost(){
return 70;
}
}
public class ButterScotch extends Icecream{
@Override
String getDescription(){
return "Butter Scotch";
}
@Override
int cost(){
return 60;
}
}
public abstract class IceCreamDecorator extends Icecream{
private Icecream icecream;
abstract int cost();
}
public class RainbowSprinklesDecorator extends IceCreamDecorator{
Icecream icecream;
RainbowSprinklesDecorator(Icecream icecream){
this.icecream = icecream;
}
@Override
String getDescription(){
return icecream.getDescription()+" with Rainbow Sprinkles";
}
@Override
int cost(){
return icecream.cost() + 20;
}
}
public class ChocolateSyrupDecorator extends IceCreamDecorator{
Icecream icecream;
ChocolateSyrupDecorator(Icecream icecream){
this.icecream = icecream;
}
@Override
String getDescription(){
return icecream.getDescription()+" with Chocolate Syrup";
}
@Override
int cost(){
return icecream.cost() + 30;
}
}
public class Customer{
public static void main(String[] args){
Icecream icecream = new ButterScotch();
icecream = new RainbowSprinklesDecorator(new ChocolateSyrupDecorator(icecream));
// print(icecream);
}
}
š Now, you know How you can make your code modular and scalable.
In our next series, We will be designing:-
- A Parking Lot System
- Instagram and much more...
Follow on š„ Twitter for more updates and daily learnings
Posted on April 12, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.