Design Patterns: Write modular and Concise Code

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 de…


This content originally appeared on DEV Community and was authored by Lovepreet Singh

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.

Design 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.

Logistics Low Level design

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:-

Factory Design Pattern

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.

Interface of Factory design pattern

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.

Factory Design Pattern

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:-

Decorator Pattern

😎 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


This content originally appeared on DEV Community and was authored by Lovepreet Singh


Print Share Comment Cite Upload Translate Updates
APA

Lovepreet Singh | Sciencx (2023-04-12T19:07:14+00:00) Design Patterns: Write modular and Concise Code. Retrieved from https://www.scien.cx/2023/04/12/design-patterns-write-modular-and-concise-code/

MLA
" » Design Patterns: Write modular and Concise Code." Lovepreet Singh | Sciencx - Wednesday April 12, 2023, https://www.scien.cx/2023/04/12/design-patterns-write-modular-and-concise-code/
HARVARD
Lovepreet Singh | Sciencx Wednesday April 12, 2023 » Design Patterns: Write modular and Concise Code., viewed ,<https://www.scien.cx/2023/04/12/design-patterns-write-modular-and-concise-code/>
VANCOUVER
Lovepreet Singh | Sciencx - » Design Patterns: Write modular and Concise Code. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/04/12/design-patterns-write-modular-and-concise-code/
CHICAGO
" » Design Patterns: Write modular and Concise Code." Lovepreet Singh | Sciencx - Accessed . https://www.scien.cx/2023/04/12/design-patterns-write-modular-and-concise-code/
IEEE
" » Design Patterns: Write modular and Concise Code." Lovepreet Singh | Sciencx [Online]. Available: https://www.scien.cx/2023/04/12/design-patterns-write-modular-and-concise-code/. [Accessed: ]
rf:citation
» Design Patterns: Write modular and Concise Code | Lovepreet Singh | Sciencx | https://www.scien.cx/2023/04/12/design-patterns-write-modular-and-concise-code/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.