Patterns. Design comes from Modeling (requirements, analysis, problem) Mending (patch, refactoring)...

48
Patterns

Transcript of Patterns. Design comes from Modeling (requirements, analysis, problem) Mending (patch, refactoring)...

Patterns

Design comes from

Modeling (requirements, analysis, problem)

Mending (patch, refactoring)

Memory (patterns, recall)

Patterns

Things that repeat.

Solutions to a problem in a context.

Patterns for coding/low-level design / architecture/user interface/problem domain

Coding patterns

Smalltalk Best Practice Patterns

Kent Beck

Prentice-Hall, 1997

OOD patterns

Design Patterns: Elements of Reusable Object-Oriented Software

Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides

Addison Wesley, 1995.

Architectural Patterns

Pattern-Oriented Software Architecture

F. Buschmann, R. Meunier, H. Rohnert, P.Sommerlad, M. Stal

Wiley, 1996

http://www.cs.wustl.edu/~schmidt/POSA/

Business Software

Patterns of Enterprise Application Architecture

Martin Fowler et. al.

Addison-Wesley, 2002

http://www.martinfowler.com/books.html#eaa

Domain Driven Design

Eric Evans

Addison-Wesley, 2004

http://domaindrivendesign.org/

Security patterns

Munawar Hafiz

https://netfiles.uiuc.edu/mhafiz/www/research/patterns/index.htm

Replication

Keep a copy of data on several computers.

Every time one computer makes a change, make a change on the replicas, as well.

Peer-to-peer games

GUI

Model

GUI

Model

eventseventsbroker

Peer-to-peer games

Event-oriented, not object-oriented

Easy to convert from single-cpu to distributed

Easy to debug

Easy if only one person can make a move at once

Must synchronize simultanious moves

Hard to make secure

Fault tolerance

Primary and backup

Messages go to everyone, only primary responds

If primary doesn’t respond, backup takes over

Croquet

3D virtual world

Rendering takes place on local machine

Events must be synchronized, then all copies of the world are changed

Opencroquet.org

Persistence

Keep everything in memory

Replicate for backup

Synchronize with a single lock

http://prevayler.org

Very fast

No SQL

Must fit in memory

Replication

Advantages:

Speed – local copy is faster to access

Persistence – copy on disk

Fault tolerance – backup can respond immediately

Disadvantages?

Replication

Disadvantages:

Complexity

Can be hard to detect changes

If changes can occur in more than one

place, must synchronize.

Must communicate changes – can reduce response time

How to document patterns

Motivating example

Definition (problem, solution)

Worked out examples

How to document

Get an idea.

Look for good example.

Write draft. Flesh out with more examples.

Get feedback. Improve. Repeat as needed.

Pattern Language

Patterns that a person or a group uses

Christopher Alexander

“A Timeless Way of Building”

“A Pattern Language”

A genetic code for human acts of building

Pattern Language

Describes set of patterns

One pattern leads to another

High-level come first, then lower-level

Complete set of patterns for a particular problem domain

Patterns

Help you gain experience, but not a substitute for experience

Can be misused - useful for experts to direct

Should describe pitfalls, costs

Where to find patterns

Books

Conferences (PLoP, EuroPLoP)

Web sites (http://hillside.net)

My research in patterns

Security patterns with Munawar Hafiz

Safety patterns with Lui Sha

Parallel programming patterns

upcrc.illinois.edu/patterns

Summary

Design is hard

Design is about tradeoffs

There is no perfect solution, just a solution whose defects don’t bother you.

Patterns are no panacea, but fit people.

Smalltalk Coding Patterns

mostly from

Smalltalk Best Practice Patterns

Kent Beck

Prentice-Hall, 1997

The Book

1. Introduction

2. Patterns

3. Behavior (Today

4. State (Next time

5. Collections

6. Classes

7. Formatting

Coding Standards

Standards

improve communication

let code be the design

make code more habitable

change

Coding Standards for Smalltalk

Variables have no types

Names can be any length

Operations named with keywords

Pretty printer

Names

Names should mean something.

Standard protocols

Object (printOn:, =)

Collection (do:, add:, at:put:, size)

Standard naming conventions

Intention Revealing Selector

Readability of message send is more important than readability of method.

Name should specify what method does, not how.

Method Names

If there is already a standard name, use it instead of following these rules.

Three kinds of methods

• change state of receiver

• change state of argument

• return value from receiver

Change state of receiver

method name is verb phrase

- translateBy:

- add:

Change state of argument

Verb phrase ending with preposition like on or to.

- displayOn:

- addTo:

Return value from receiver

method name is noun phrase or adjective, a description rather than a command

- translatedBy:

- size

- topLeft

Method NamesSpecialized names for specialized purposes.

Double-dispatching methods

Accessing methods

Query methods

Boolean property setting

Converter methods

Accessing Methods

Many instance variables have accessing methods, methods for reading and writing them.

Accessing methods come in pairs.

name, name:

width, width:

x, x:

When to use Accessing Methods

Two opinions:

Always, including an object’s own instance variable

lazy initialization, subclassing is easier

Only when you need to use it.

better information hiding

Query Method

Methods that return a value often describe the type of the value because they are noun phrases.

Query methods are not noun phrases, but are predicates. How can we make the return type clear?

Query Method

Prefix every query method with "is".

isNil

isControlWanted

isEmpty

Put query methods in protocol “testing”

Boolean Property Setting

Don't make accessing methods whose only argument is a boolean.

Create two methods beginning with ”be". (or “make”) Add "toggle" if necessary.

• beVisible / beInvisible / toggleVisible

• beDirty / beClean

Converter Method

Often you want to return the receiver in a new format.

Prepend "as" to the name of the class of object returned.

• asSet (in Collection)

• asFloat (in Number)

• asComposedText (in Text)

Converter Constructor Method

A class can get cluttered with converter methods that have no relationship with its main responsibility.

Make a Constructor Method in the target class that takes the object to be converted as an argument.

Date fromString: ‘09/10/01’

Constructor Method

Class methods that create instances are in category "instance creation methods".

Creation followed by initialization is the most flexible.

- Point new x: 0; y: 0

Important to preserve invariants and avoid ill-formed objects.

Constructor Method

Instance creation methods should create well-formed instances. Pass all required parameters to them.

- Point x: 0 y: 0

- SortedCollection sortBlock: aBlock

- Set new

Constructor Parameter Method

How should a Constructor Method initialize new object?

Separate setters are most flexible

x: aNumber y: anotherNumber

^self new x: aNumber;

y: anotherNumber

Must maintain invarients.

Creation Parameter Method

Provide a single method that sets all the variables. Preface its name with "set", then the names of the variables.

x: aNumber y: anotherNumber

^self new setX: aNumber y: anotherNumber

Shortcut Constructor Method

It is verbose to keep mentioning class names, and they are duplicated.

Point x: width y: height

width @ height