Tiny System Design - Design Patterns - Part 1 (LLD)

Tiny System Design - Design Patterns - Part 1 (LLD)

The Gang of Four Design Patterns are like different recipes for making software. These recipes help programmers solve common problems in an efficient and clean way. Let’s think of these patterns as different types of LEGO sets that help you build different things. The 23 patterns are divided into three categories: Creational, Structural, and Behavioral.

These initial posts consist of simple, stupid examples. But all complex things start simple. Later on we will delve into how these patterns are used to solve real problems.

Creational Patterns (Making Things)

Creational patterns are like LEGO sets that help you build specific things. They help in creating objects in a system.

Singleton Pattern:

Like having only one remote control for your TV. Only one object controls everything.

Use Case: Single printer for everyone in the house.

Use Case: One scoreboard in a game.

Use Case: Single settings menu for a phone.

Factory Method

Like a toy factory that makes different toys but all are categorized as toys.

Use Case: A game character factory creating different characters.

Use Case: A pizza store making different types of pizzas.

Use Case: A toy factory producing different toys.

Abstract Factory

A bigger toy factory with separate sections for making dolls, cars, and robots.

Use Case: A gadget factory with sections for phones, laptops, and tablets.

Use Case: A clothing store with sections for men, women, and kids.

Use Case: A bakery with sections for cakes, cookies, and bread.

Builder Pattern

Like building a LEGO castle step by step, piece by piece.

Use Case: Building a burger with different ingredients.

Use Case: Assembling a computer with different parts.

Use Case: Creating a customized pizza.

Prototype Pattern

Copying a drawing instead of making a new one from scratch.

Use Case: Cloning a game character with different powers.

Use Case: Copying a document.

Use Case: Duplicating a design template.

Structural Patterns (Organizing Things)

Structural patterns help in organizing and connecting different parts, like connecting LEGO pieces together.

Adapter Pattern

Like using a plug adapter to connect your toy's charger to the wall socket.

Use Case: Connecting headphones with a phone using an adapter.

Use Case: Reading different document formats in a single reader.

Use Case: Using different camera lenses on a camera with an adapter.

Bridge Pattern:

Like having a universal remote that can control different devices.

Use Case: A universal charger for different devices.

Use Case: A universal remote for all home appliances.

Use Case: A game controller compatible with different gaming consoles.

Composite Pattern

Like a box of different LEGO sets where each set can also be a box of smaller pieces.

Use Case: Graphic objects that can be shapes or groups of shapes.

Use Case: A music playlist with songs and sub-playlists.

Use Case: A gift box containing gifts and smaller gift boxes.

Decorator Pattern

Like adding stickers to your LEGO castle to make it look cooler.

Use Case: Adding toppings to a pizza.

Use Case: Adding accessories to a game character.

Use Case: Adding filters to a photo.

Facade Pattern

Like a game controller that has few buttons but can do many things.

Use Case: A simple remote controlling a complex smart TV.

Use Case: A single login button for different accounts.

Use Case: A simple user interface for a complex software.

Flyweight Pattern

Like sharing LEGO pieces among friends to build bigger things together.

Use Case: Sharing game assets among different game levels.

Use Case: Sharing book images among different e-book readers.

Use Case: Sharing video streaming service among different devices.

Proxy Pattern

Like having a mini-remote that controls the main remote which controls the TV.

Use Case: Parental controls on a kid's phone.

Use Case: Firewall protecting a computer network.

Use Case: A manager taking client requests and directing them to the right department.

Behavioral Patterns (Working Together)

Behavioral patterns are like rules for how LEGO pieces or sets interact and work together.

Chain of Responsibility

Like passing a note in class until it reaches the right person.

Use Case: Handling customer service requests at different levels.

Use Case: Filtering inappropriate content at different levels.

Use Case: Processing a purchase through different discount offers.

Command Pattern

Like giving a list of chores to your robot and it does them in order.

Use Case: Scheduling tasks on a computer.

Use Case: Programming a smart home system.

Use Case: Creating a to-do list app.

Interpreter Pattern

Like translating your secret language into normal words.

Use Case: Translating programming code into machine language.

Use Case: Converting voice commands into actions.

Use Case: Translating text from one language to another.

Iterator Pattern

Like going through your toy box and playing with each toy one by one.

Use Case: Browsing through songs in a playlist.

Use Case: Scrolling through photos in a gallery.

Use Case: Going through pages in an e-book.

Mediator Pattern

Like having a teacher in class to help students talk to each other properly.

Use Case: Air traffic control coordinating planes.

Use Case: Customer service center connecting customers and support agents.

Use Case: Social media platform connecting users.

Memento Pattern

Like having a save button in your game to continue later.

Use Case: Saving game progress.

Use Case: Auto-saving documents.

Use Case: Bookmarking pages in a web browser.

Observer Pattern

Like your mom knowing everything you do.

Use Case: Getting notifications from social media.

Use Case: Receiving alerts for emergency news.

Use Case: Updating weather information on a weather app.

State Pattern

Like your toy robot having different modes (play, sleep, dance).

Use Case: A phone having different modes (silent, vibrate, ring).

Use Case: Traffic lights changing colors.

Use Case: A game character changing abilities.

Strategy Pattern

Like choosing different strategies to win a game.

Use Case: Selecting different payment methods in an online store.

Use Case: Choosing different routes on a map.

Use Case: Selecting different fighting styles in a game.

Template Method Pattern

Like following a recipe but with different ingredients.

Use Case: Making different types of sandwiches.

Use Case: Drawing different shapes with similar steps.

Use Case: Creating different documents with a similar format.

Visitor Pattern

Like having a guest who visits and interacts with everyone in the house.

Use Case: A teacher checking students' homework

Use Case: A doctor visiting patients in different rooms.

Use Case: A technician checking different machines in a factory.


Reason for Categorization

Now, let’s understand why these patterns are categorized into Creational, Structural, and Behavioral:

  1. Creational Patterns: These patterns are like the birth of LEGO pieces. They help in creating objects. When you want to create an object, but it’s complex or you want to have only one, or you want to decide what type to create at the last moment, you use these patterns. They help in the process of object creation while hiding the creation logic.
  2. Structural Patterns: Once you have LEGO pieces, you need to join them to create something cool. Structural patterns help you connect different pieces together to form a larger structure. They help in defining simple ways to compose objects for creating new functionality.
  3. Behavioral Patterns: Now, you have a cool LEGO structure, but how does it work? Behavioral patterns are like the rules or behaviors of the LEGO structure. They define how the objects interact and work together to perform tasks.

Think of Creational, Structural, and Behavioral patterns like the process of playing with LEGOs: First, you have pieces (Creational), then you connect them (Structural), and finally, you play with the structure (Behavioral).

Some more analogies to remember patterns.

Creational Patterns:

  1. Singleton: Only one principal in a school.
  2. Factory Method: A chef deciding what dish to cook based on the order.
  3. Abstract Factory: A toy factory that can produce both wooden and plastic toys.
  4. Builder: Building a burger with various ingredients.
  5. Prototype: Photocopying a document.

Structural Patterns:

  1. Adapter: Using a travel adapter to plug in devices in different countries.
  2. Bridge: A remote control (abstraction) and devices (implementation) it can control.
  3. Composite: A music playlist containing both individual songs and other playlists.
  4. Decorator: Decorating a Christmas tree with various ornaments or decorating a fort that children make during Diwali (If you remember those days)
  5. Facade: Using a single remote to control all home devices.
  6. Flyweight: A library lending the same book copy to multiple readers.
  7. Proxy: A school gatekeeper ensuring only students enter.

Behavioral Patterns:

  1. Observer: A teacher (subject) informing students (observers) about a schedule change.
  2. Strategy: Choosing a transportation method (bus, bike, walk) based on the situation.
  3. Command: A remote control with buttons (commands) to operate a TV.
  4. Chain of Responsibility: A chain of managers approving a leave request.
  5. Interpreter: Translating a sentence from English to Spanish.
  6. Iterator: Scrolling through a playlist song by song.
  7. Mediator: A traffic signal coordinating vehicles.
  8. Memento: Video game save points.
  9. State: A traffic light changing colors (states).
  10. Template Method: Baking a cake with steps but different ingredients.
  11. Visitor: A zoo visitor interacting with various animals.

Here is the diagram for the above.

Watch out for other posts in these series. Follow the #tinysystemdesign hash on linked-in.


Bhaskar Thouti

Lead Analyst - Software Engineer III at Bank of America Merrill Lynch

1 年

Thank You Rajesh Pillai. It's informative

要查看或添加评论,请登录

社区洞察

其他会员也浏览了