Ausnahme gefangen: SSL certificate problem: certificate is not yet valid ๐Ÿ“Œ Design Patterns: Write modular and Concise Code

๐Ÿ  Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeitrรคge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden รœberblick รผber die wichtigsten Aspekte der IT-Sicherheit in einer sich stรคndig verรคndernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch รผbersetzen, erst Englisch auswรคhlen dann wieder Deutsch!

Google Android Playstore Download Button fรผr Team IT Security



๐Ÿ“š Design Patterns: Write modular and Concise Code


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

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

...



๐Ÿ“Œ Design Patterns: Write modular and Concise Code


๐Ÿ“ˆ 73.87 Punkte

๐Ÿ“Œ Design patterns: Event delegation for cleaner, more concise code.


๐Ÿ“ˆ 49.4 Punkte

๐Ÿ“Œ Design Patterns: Make your code modular


๐Ÿ“ˆ 38.48 Punkte

๐Ÿ“Œ CI/CD Software Design Patterns and Anti-Patterns


๐Ÿ“ˆ 35.93 Punkte

๐Ÿ“Œ Model hosting patterns in Amazon SageMaker, Part 1: Common design patterns for building ML applications on Amazon SageMaker


๐Ÿ“ˆ 34.15 Punkte

๐Ÿ“Œ Design Patterns in JavaScript: Creational Patterns


๐Ÿ“ˆ 34.15 Punkte

๐Ÿ“Œ Continuous Integration Patterns and Anti-Patterns


๐Ÿ“ˆ 29.79 Punkte

๐Ÿ“Œ Machine Learning Patterns and Anti-Patterns


๐Ÿ“ˆ 29.79 Punkte

๐Ÿ“Œ Flow & Cadence Best Practices, Patterns, and Anti-Patterns


๐Ÿ“ˆ 29.79 Punkte

๐Ÿ“Œ Patterns 1.3 - Build patterns quickly and effortlessly with syntax coloring.


๐Ÿ“ˆ 29.79 Punkte

๐Ÿ“Œ What is ISO 27001? A Clear and Concise Explanation for 2022 | UpGuard


๐Ÿ“ˆ 27.17 Punkte

๐Ÿ“Œ Power of Modern Programming Languages is That They Are Expressive, Readable, Concise, Precise, and Executable


๐Ÿ“ˆ 27.17 Punkte

๐Ÿ“Œ 13 Rules I Follow When Writing CSS To Make It Concise and Readable๐Ÿš€๐Ÿ’ก


๐Ÿ“ˆ 27.17 Punkte

๐Ÿ“Œ Accessible writing tip | Clear and concise text


๐Ÿ“ˆ 27.17 Punkte

๐Ÿ“Œ Design Patterns Are A Better Way To Collaborate On Your Design System


๐Ÿ“ˆ 26.28 Punkte

๐Ÿ“Œ Top 6 System Design Patterns to Ace Every System Design Interview


๐Ÿ“ˆ 26.28 Punkte

๐Ÿ“Œ Please point me to a concise, but comprehensive resource to the following topics.


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Confused by the EARN IT act? Here's a concise breakdown of what it could mean for privacy online


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Anyone have a link to a clear, concise, high school level presentation of the math behind public key encryption?


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Mastering Communication in Cyber Intelligence Activities: A Concise User Guide


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Arrow Functions in JavaScript: How to Use Fat & Concise Syntax


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Using Tailwind CSS with React.js: A Concise Guide


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Meet einx: A Python Library that Allows Formulating Many Tensor Operations as Concise Expressions Using Einstein Notation


๐Ÿ“ˆ 25.38 Punkte

๐Ÿ“Œ Unlocking JavaScript Design Patterns: Mastering Singleton for Ultimate Code Efficiency


๐Ÿ“ˆ 24.03 Punkte

๐Ÿ“Œ Why SOLID Design Matters: Avoid Code Smells and Write Maintainable Code


๐Ÿ“ˆ 23.91 Punkte

๐Ÿ“Œ mobsfscan v0.1.1 releases: find insecure code patterns in your Android and iOS source code


๐Ÿ“ˆ 23.56 Punkte

๐Ÿ“Œ LG G6 Might Ditch The Semi-Modular Design and Feature An Iris Scanner


๐Ÿ“ˆ 22.39 Punkte

๐Ÿ“Œ LG G6 Might Ditch The Semi-Modular Design and Feature An Iris Scanner


๐Ÿ“ˆ 22.39 Punkte

๐Ÿ“Œ [$] Lockless patterns: more read-modify-write operations


๐Ÿ“ˆ 22.22 Punkte

๐Ÿ“Œ You Don't Need That: Design Patterns and Python


๐Ÿ“ˆ 21.93 Punkte

๐Ÿ“Œ You Don't Need That: Design Patterns and Python


๐Ÿ“ˆ 21.93 Punkte

๐Ÿ“Œ Adapter Design Patterns In Flutter & Dart: Overview, Application, Method and Types


๐Ÿ“ˆ 21.93 Punkte











matomo