Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4560

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T01:07:08+00:00 2024-11-27T01:07:08+00:00

Design Patterns: Write modular and Concise Code

  • 61k

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

Interface of Factory design pattern
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      } } 
Enter fullscreen mode Exit fullscreen mode

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

Decorator Pattern
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);     } } 
Enter fullscreen mode Exit fullscreen mode

👉 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

designpatternsprogrammingtutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.