design patterns

Download Design Patterns

If you can't read please download the document

Upload: imedode

Post on 16-Apr-2017

2.634 views

Category:

Technology


0 download

TRANSCRIPT

Design Patterns

Part One

Part Two

Part 1

What design is

What it's not

Why patterns?

(Part 2 is about Patterns en dtail)

What design is

Process

Thought

Methodology

Discussion

Attitude

Simplicity

Finding solutions for a given problem

What it's not

Enforcing schemas

Dictating solutions

Finding problems for a given solution

One Solution Fits All

Why patterns?

reuse

similar problems occur over and over again just reuse the code

separation

when you separate concerns you should have a single point of failure for everything anyway

Why patterns?

reuse

similar problems occur over and over again with small but significant differences between one and the other

separation

separate those parts of the solution that change from those that stay the same

Why patterns?

reuse

a pattern once identified can be used when ever you encounter a similar problem

separation

patterns are ideas and practices, not classes and methods

Part 2

Patterns en dtail

What have we?

the Template

the Strategy

the Observer

the Composite

the Iterator

the Command

the Adapter

the Proxy

the Decorator

the Singleton

the Factory

the Builder

the Interpreter

the DSL

the Meta

the COC

Quite a bunch!

Let's start with theclassic patterns.

Template (Method)

abstract class/module

several virtual methods

one method that runs the virtual methods in a certain order

concrete classes that implement the virtual methods

a call to the template method on the concrete class

Template (Method)

When to use?

there is a certain sequence of actions done ever again but how the actions executed differs from case to case

Examples

Tests

setup()

test_xy()

teardown()

Processors

some object loading

process_object()

Strategy

context

a property containing the strategy

abstract strategy

a virtual method for doing the strategy

concrete strategies

the concrete strategy method

all the methods need to accomplish the task

Strategy

When to use?

There is some action that needs to be done in several different ways which differ far beyond what a Template covers

Examples

Processors

on_message()

Miners

run()

Observer

abstract subject

observers as property

logic to add/remove/inform observers

observer

update method

concrete subject

business logic including informing observers

Observers

When to use?

You want to be informed when some state in the subject changes and react to that.

Examples

ActiveRecord::Observer

Composite

abstract component

virtual methods

concrete component 'leaf'

concrete methods

concrete component 'composite'

property for subcomponents

concrete methods that delegate to the subcomponents

Composite

When to use?

You want to treat some object the same, no matter how complex the operation is you want to have done, but you want to model the operation in as small steps as necessary.

Example

GUIs like FXRuby

windows, frames, buttons, labels

Iterator

internal

method that yields a block on every of the object's properties/elements

external

an object keeps track of which property of another element you are currently reading and if there are more to read

Iterator

When to use?

general

you need access (in order) to each property of an object to do something with it

internal

you want the object to be able to expose its properties on its own

external

you want to decouple the exposition from the object, for use independent of the property container

Examples

Enumerable module

Command

abstract command class

virtual methods

concrete command classes

concrete methods

Command

When to use?

You need something to be done without care of how it is done. You may even delay the operation, aggregate actions or provide ways to undo something.

Examples

Background#do

ActsAsPublished

bulk updates

Migrations

up/down

Adapter

client

knows some target

target's methods

target (adapter)

adaptee

adaptee's methods

methods that call adaptee's methods

or

redefined adaptee class

happens...

Adapter

When to use?

When ever you want you client target to access one of some server classes which have completely different interfaces.

Examples

ActiveRecord::Adapter::XY

abstract search

Proxy

abstract service

virtual method

concrete service

concrete method

service proxy (< abstract service)

property for the concrete service

proxy method calling the concrete method

Proxy

When to use?

protection

making sure some state is checked before the methods get loaded

remoting

your real service is over the net or somewhere and you don't want the client to bother manage the connection

virtualization

you want the concrete service to be loaded first when some method is called on him, not on initialization of the service

Example: DRb

Some thought on a Proxy

virtual proxy means some object is loaded only later

observers load all our model code on initialization of the webserver

why not warp the models in a proxy?

problem: we would perhaps have to use the proxy everywhere

perhaps a Command could help out catching the observe command and being run by the Model on load

Decorator

abstract component

virtual methods

concrete component

concrete methods

decorator component

component property

new methods

methods calling the new methods and then the method on the property component

Decorator

When to use?

On occasion you want something to be done to the parameter before the actual method is called or you want some other things to be executed along with the original method.

Examples

Acts_as_...

alias_method_chain

Singleton

one class

one instance

a private initialize method

other methods

alternatively a module or a class

methods

Singleton

When to use?

You want something to be loaded just once, like a config file, a connection object or the like.

Examples

the communipediaApi configuration

Class style singleton

the sitemap

instance singleton

Factory

abstract factory class

virtual object initialization methods

concrete factory class

concrete methods for initializing several objects

Factory

When to use?

In a certain place you need to be handed instances of different objects depending on some criteria.

Examples

Campaign

new() loads a certain campaign type depending on what you tell it

Builder

product class

properties

builder class

property product

methods to set the products properties

additional methods

director

property builder

Builder

When to use?

You need objects only built in a certain state but you don't what to have the object bother enforcing the right state.

Examples

ActiveRecord models

validations

data is built to fit the chosen DB

Interpreter

client

properties expressions

property context

abstract expression

virtual interpret method

terminal expression

interpret method

nonterminal expression

properties sub expressions

interpret method

Interpreter

When to use?

You want to introduce some specific expression language to a certain task who's code you need to execute.

Examples

Ruby

itself (which is too complex for the pattern actually)

regex

That's it.

Not really.But so much for theclassic patterns.

Ruby patterns.

Now for some moreruby specific patterns.

Domain Specific Language

file filled with (syntactically) correct ruby code

loader class

methods used as keywords in the file

Domain Specific Language

When to use?

You want non-programmers to define e.g. behaviors of something. You like the ease of defining config files without needing an interpreter to load it again.

Examples

campaign

the chain methods could be used as DSL

Meta (Programming)

meta class

methods

classes defined by the meta class methods

methods definded by the meta class methods

Meta (Programming)

When to use?

You need objects with certain behaviour or set of methods depending on some conditions but you don't want to define every possible combination in a separate class.

Examples

ActsAsSolr

pick a file...

Convention over Configuration

classes

intelligent defaults

configurations

items that override the defaults

Convention over Configuration

When to use?

You want a behavior that you can set in certain ways and you know one that fits most of the time or serves as fallback.

Examples

Rails::*

search_backend

That's it.

(finally)