20
Tho Q Luong thoqbk.github.io Design Pattern in Action

Design pattern in action

Embed Size (px)

Citation preview

Tho Q Luongthoqbk.github.io

Design Pattern in Action

Client - Server Application

Mimic Commands: .

Written in Java

Use Design Patterns

ls cd pwd cat python

Demo

Requirements for Connection Management

+ Open, close and send/receive data to/from server+ Handshaking step: exchange connection configs:

+ Encrypt data ?+ Compress data?

+ Retry when losing connection:+ Maximum: M times+ After N seconds

Connection States

Fail State

Active State

Close

Connection

State

Closed State

Receive

Close

Close connection successfully

Close

Retry to

open

connection

Open

Connection

Fail

Exception

1. Open

Connection

Successfully

Start2. Send Handshaking Request

3. Receive Handshaking Ack

Sleep N s If Retry Times is less than M

Open

Connection

State

Retry State

Retry

Times

is

greater

than or

equal M Stop

State Machine Pattern

● Allow an object to alter its behavior when itsinternal state changes. The object will appear tochange its class.

● Context:○ Defines the interface of interest to clients○ Keeps reference to the current state.

● State:○ Defines an interface for encapsulating the

behavior associated with a particular state ofthe Context.

State Machine

Demo: State Machine Implementation

Interceptor Pattern

An Interceptor Pattern is a software design pattern thatis used when software systems or frameworks want tooffer a way to change, or augment, their usualprocessing cycle.

Example: typical processing sequence for a web-server:

Receive index.html

Map index.html to disk

Read file content

Send its content to the browser

Template engine

Interceptor Design in Netty

1. Receive data → UpStream Handler

2. Send data → DownStream Handler

3. Build ChannelPipeline:

JSON Interceptor

Compress Interceptor

Binary Interceptor

Encrypt Interceptor

Receive Message

Connection.send(JSONObject)

Compress data

Encrypt data

Add Header

IO.write(binary)

Remove Header

Decrypt data

Decompress data

MessageListner.on(JSONObject)

Interceptor List

Interceptors Design

Connection w/ Pipeline

Demo: Interceptor Implementation

Factory Pattern

Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Builder Pattern

+ Large numbers of optional parameters+ Constructor requires many parameters+ Validate parameters before constructing object+ Want to make object immutable

Dependency Path:

Without Dependency Injection

Class A Class B

Class C

Class D

Class E

# B depends on C, D and Eclass A:

def __init__(self, c, d, e):self.b = B(c, d, e)#do something to init self.b

a = A(c, d, e)

Class F

# B depends on C, D, E and Fclass A:

def __init__(self, c, d, e, f):self.b = B(c, d, e, f)#do something to init self.b

a = A(c, d, e)

Dependency Injection

Dependency Injection is a design pattern that implements inversion of control for resolving dependencies

A dependency is an object that can be used (a service).

An injection is the passing of a dependency to a dependent object (a client) that would use it.

ApplicationIoC

Container

Configuration

Metadata

Application

Configuration

Services

Demo: Buncha IoC Container

https://www.npmjs.com/package/buncha

Summary

● State Machine

● Interceptor

● Factory

● Builder

● Dependency Injection

Thank you