Tiny System Design - Design Patterns - Part 1 (LLD)
Rajesh Pillai
Co-Founder, Software Engineer and CTO @Algorisys Technologies - Building Products and Upskilling upcoming generations
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:
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:
Structural Patterns:
Behavioral Patterns:
Here is the diagram for the above.
Watch out for other posts in these series. Follow the #tinysystemdesign hash on linked-in.
Lead Analyst - Software Engineer III at Bank of America Merrill Lynch
1 年Thank You Rajesh Pillai. It's informative