You are on page 1of 44

Playing Legos in Codes

Designing highly flexible software


Design Patterns

Tried and tested toolkits for common problems in software design


Common language to help team communicate efficiently
Strategy Pattern
Decorator Pattern
Template Method
Chain of Responsibility
Strategy Pattern
Scenario

Shipping Rates Fetching for Multi Shipping Options


Problem

Shipping Options
DHL
Gojek
JNE
Business Intention

Show shipping price to our users before they check out


Case study:
Strategy Pattern Shipping Rates Fetching

Code example
Decorator Pattern
Scenario

Fetching List of Book from Cache or DB


Problem

We need to handle our increasing traffic with cache

We need to enable/disable our cache

It should be easy to change our cache later


Business Intention

Fetch list of book from data source


Case study: DB & Cache
Decorator Pattern

Code example
Template Method
Scenario

Sales Report for Different Recipients


Problem

Four different recipients: BD, Finance, Investor, Seller

Each recipient demand different requirements


Business Process

Found common steps for each recipient

1. Fetching order data


2. Compiling the report
3. Storing the report
4. Sending the report to specified recipients
5. Storing report activity results to internal-dev monitoring system
Case study:
Sales Report for Different Recipients
Template Method

Code example
Chain of
Responsibility
Scenario

Multiple Options of Checkout Processes


Problem

We already have basic checkout process

BD wants us to expand serving Distributors

Upcoming Big-Promo need different checkout process


Business Process
Basic Checkout Big-Promo Checkout

1. Product validation 1. Promo validation


2. Payment-process preparation 2. Fraud validation
3. KYC validation
Distributor’s Checkout
4. Product validation
3. Minimum quantity validation 5. Payment-process preparation
4. KYC validation
5. Shop eligibility validation
6. Location validation
7. Product validation
8. Payment-process preparation
Make each checkout-step modular

and can be chained with other checkout-step


Case study: Different
Chain of Responsibility Checkout Processes

Code example
Personal Tips

Before starting to code

1. Break down requirements


2. Analyze patterns
3. Design & plan
Recap
Decorator Pattern Chain of Responsibility

Wrapping new behavior on another behavior Passing request along a chain of handlers

Strategy Pattern Template Method

Switch between different set of algorithms A skeleton of steps implemented by subclasses


Samples

github.com/gentra/golang-design-patterns
Thank You

You might also like