advertisement

Design Patterns

33 %
67 %
advertisement
Information about Design Patterns

Published on July 29, 2008

Author: imedo.de

Source: slideshare.net

Description

A short overview about the design patterns described in Russ Olson's book "Design Patterns in Ruby".
advertisement

Design Patterns Part One Part Two

Part One

Part Two

Part 1 What design is What it's not Why patterns?

What design is

What it's not

Why patterns?

(Part 2 is about Patterns en détail)

What design is Process Thought Methodology Discussion Attitude Simplicity Finding solutions for a given problem

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

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

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

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

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 détail

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

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 the classic 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

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()

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

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()

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

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

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

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

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

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

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

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

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...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 the classic patterns.

Ruby patterns. Now for some more ruby specific patterns.

Domain Specific Language file filled with (syntactically) correct ruby code loader class methods used as keywords in the file

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

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 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...

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

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

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)

Add a comment

Related pages

Entwurfsmuster – Wikipedia

Entwurfsmuster (englisch design patterns) sind bewährte Lösungsschablonen für wiederkehrende Entwurfsprobleme sowohl in der Architektur als auch in der ...
Read more

Design Pattern Katalog - Homepage von Philipp Hauer ...

Design Pattern Vorschau Anwendung; Strategy: Das Strategy Design Pattern ermöglicht einer Klasse das flexible Wechseln von alternativen Verhalten. Mehr ...
Read more

Design Patterns - SourceMaking

Design patterns. Design patterns source code examples. Design patterns tutorials and guides.
Read more

Design Patterns - DoFactory

Data & Object Factory helps developers succeed with .NET Design Patterns through training, products, and a .NET Design Pattern and Practices community
Read more

Design Patterns | Object Oriented Design

Singleton pattern should be used when we must ensure that only one instance of a class is created and when the instance must be available through all the code.
Read more

Design Patterns: Elements of Reusable Object-Oriented ...

Buy Design Patterns: Elements of Reusable Object-Oriented Software on Amazon.com FREE SHIPPING on qualified orders
Read more

Design Patterns & Refactoring

Design Patterns. Patterns are higher-order designs, which occur repeatedly in object-oriented design. They have been formalized, and are generally ...
Read more

Design Pattern - Overview - Tutorialspoint

Design Patterns Overview - Learning java design patterns in simple and easy steps : A beginner's tutorial containing complete knowledge about an java ...
Read more

Design patterns

Design patterns provide a common language between designers. They allow for debate over alternatives, where merely mentioning the name of a design ...
Read more

Design Patterns: Elements of Reusable Object Orientated ...

Erich Gamma - Design Patterns: Elements of Reusable Object Orientated Software jetzt kaufen. ISBN: 9780201485370, Fremdsprachige Bücher - Programmieren
Read more