16 october 2007kaiser: coms w4156 fall 20071 coms w4156: advanced software engineering prof. gail...

73
16 October 2007 Kaiser: COMS W4156 Fall 2 007 1 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser [email protected] http://york.cs.columbia.edu/clas ses/cs4156/

Upload: dorcas-jenkins

Post on 18-Jan-2016

221 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 1

COMS W4156: Advanced Software Engineering

Prof. Gail Kaiser

[email protected]

http://york.cs.columbia.edu/classes/cs4156/

Page 2: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 2

Design

• Within a class (or component)– High Cohesion– Completeness– Convenience– Clarity– Consistency

• Across classes (or components)– Low Coupling

Page 3: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 3

Design Pattern

• A general repeatable solution to a commonly occurring problem

• A description of the problem and the essence of its solution

• Should be sufficiently abstract to be reused in different settings

• Designed to avoid re-design• Allow developers to communicate using well-

known, well understood names for software interactions

Page 4: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 4

Naming Conventions

• Most modern programming languages supply their own naming conventions (learn it, use it! – Java, C++, C#)

• Otherwise, choose a scheme at design-time and stick to it at coding-time

• For components, interfaces, classes, types, methods, exceptions, members, parameters, variables, …

Page 5: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 5

Example: Delegation

• An object outwardly expresses certain behavior but in reality delegates responsibility for implementing that behavior to an associated object

• Very general concept, refined in several more specific design patterns

Page 6: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 6

Example: Delegation class A { void f() { System.out.println("A: doing f()"); } void g() { System.out.println("A: doing g()"); } } class C { // delegation A a = new A(); void f() { a.f(); } void g() { a.g(); } // normal attributes X x = new X(); void y() { /* do stuff */ } } public class Main { public static void main(String[] args) { C c = new C(); c.f(); c.g(); } }

Page 7: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 7

Example: Proxy Pattern

• An object functions as an interface to another object

• Provide a surrogate or placeholder that uses an extra level of indirection to support distributed, controlled, or intelligent access to an object

• In its most general form, a proxy is <something> functioning as an interface to <something else>. The <something else> could be anything: a network connection, a large object in memory, a file, or some other resource

Page 8: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 8

Non-Software Proxy Pattern

Page 9: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 9

Software Proxy Pattern

Page 10: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 10

Discussion: Proxy

• Maintains a reference that lets it access the real subject• Provides an interface identical to subject's so that a

proxy can be substituted for the real subject• Controls access to the real subject and may be

responsible for creating and deleting it • May also:

– Count the number of references to the real object so that it can be freed automatically when there are no more references

– Load a persistent object into memory when it's first referenced– Check that the real object is locked before it is accessed to

ensure that no other object can change it– …

Page 11: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 11

Types of Proxies

• Remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space

• Virtual proxies are placeholders for “expensive to create” or “resource hungry” objects, may cache additional information about the real subject so that they can postpone accessing it

• Protection proxies check that the caller has the access permissions required to perform a request and may provide different clients with different levels of access

• Others: copy-on-write, cache, synchronization, …

Page 12: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 12

Proxy Pattern Example

Page 13: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 13

Example: Façade Pattern

• A single class that represents an entire subsystem or library

• Provides a unified interface to a set of interfaces

• May simplify by providing convenient methods for common tasks that internally involve multiple classes/methods

• Often semantic wrapper of existing [legacy] objects

Page 14: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 14

Non-Software Façade Pattern

Page 15: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 15

Software Façade Pattern

Page 16: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 16

Discussion: Façade

• Knows which subsystem classes are responsible for a request and delegates client requests to appropriate objects

• Subsystem classes handle work assigned by façade but have no knowledge of the façade and keep no reference to it

• Reduces dependencies of outside code on the inner workings of a subsystem

• May reduce learning curve for novice users but be insufficient for power users

Page 17: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

Façade Pattern Example

Page 18: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 18

History of “Design Patterns”

• (Building) Architect Christopher Alexander– A Pattern Language (1977)– Several other books– www.patternlanguage.com

“Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice. “

Page 19: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 19

History of Software Design Patterns

• Arose from frameworks like Model-View-Controller used in early OO programming (notably Smalltalk)

• “Gang of Four” (GoF): Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides

• Design Patterns: Elements of Reusable Object-Oriented Software (1995) – described 23 patterns (observed, not invented)

• Many conferences, symposia, books, …

Page 20: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 20

Design Patterns

“A design pattern systematically names, motivates, and explains a general design that addresses a recurring design problem in object-oriented systems. It describes the problem, the solution, when to apply the solution, and its consequences. It also gives implementation hints and examples. The solution is a general arrangement of objects and classes that solve the problem. The solution is customized and implemented to solve the problem in a particular context.” [GoF]

Page 21: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 21

Design Pattern Elements

• Name• Problem description• Solution description

– Not a concrete design but a template for a design solution that can be instantiated in different ways

• Consequences– The results and trade-offs of applying the

pattern

Page 22: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 22

Design Pattern Elements (Expanded)

• name and classification• intent• also known as• motivation• applicability• structure• participants• collaborations• consequences• implementation• sample code• known uses• related patterns

Page 23: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

Original Catalog of Patterns

Purpose

Creational Structural Behavioral

Scope Class Abstract Method Adapter (class) InterpreterTemplate Method

Object Abstract FactoryBuilderPrototypeSingleton

Adapter (object)BridgeCompositeDecoratorFaçadeFlyweightProxy

Chain of ResponsibilityCommandIteratorMediatorMementoObserverStateStrategyVisitor

Page 24: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 24

Creational Patterns

• Concerned with instantiation

• Create objects for you, rather than having you instantiate objects directly

Page 25: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 25

Creational Patterns

• Factory Method – creates an instance of several derived classes

• Abstract Factory – creates an instance of several families of classes

• Singleton – a class of which only a single instance can exist, ensures the class has only one instance and provides a global point of access to it

Page 26: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 26

Factory Method Pattern

• Define an interface for creating an object, but let subclasses decide which class to instantiate

• Lets a class defer instantiation to subclasses• Common in toolkits and frameworks where

library code needs to create objects of types that may be subclassed by applications using the framework

• More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects

Page 27: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 27

Non-Software Factory Method Pattern

Page 28: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 28

Software Factory Method Pattern

Page 29: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 29

Discussion: Factory Method

• Defines a "virtual" constructor• Unlike a constructor, factory methods can

have different and more descriptive names • Unlike a constructor, an existing object

might be reused, instead of a new object created (object pooling)

• The new operator considered harmful (make all constructors private or protected)

Page 30: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 30

Factory Method Pattern Exampleclass Complex { public static Complex fromCartesian(double real, double

imag) { return new Complex(real, imag); } public static Complex fromPolar(double rho, double theta)

{ return new Complex(rho * cos(theta), rho *

sin(theta)); } private Complex(double a, double b) { //... }}

// Same as fromCartesian(-1, 0) Complex c = Complex.fromPolar(1, pi);

Page 31: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 31

Abstract Factory Pattern

• Creates an instance of any of a family of classes• Provide an interface for creating families of related or

dependent objects without specifying their concrete classes

• Useful for families of products and to enforce families of products that must be used together

• Promotes consistency among products • Example: DocumentCreator class that provides

interfaces to create instances of several kinds of documents, e.g., createLetter() and createResume()

Page 32: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 32

Non-Software Abstract Factory Pattern

Page 33: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 33

Software Abstract Factory Pattern

Page 34: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 34

Discussion: Abstract Factory

• Coordinates the instantiation of sets of objects that have varying implementations in such a way that only legitimate combinations of instances are possible, and hides these concrete instances behind a set of abstractions

• Hides from consuming (client) objects:– The number of sets of instances supported by the

system – Which set is currently in use – The concrete types that are instantiated at any point – The issue upon which the sets vary (might be

determined from a config file)

Page 35: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 35

Abstract Factory Pattern Example

Page 36: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 36

Singleton Pattern

• Allow for only one instance of a given class to ever exist (encapsulates that the number of instances is constrained)

• Provide a mechanism to obtain this instance that any client can access

• Examples include objects needed for logging, communication, database access, etc.

Page 37: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 37

Non-Software Singleton Pattern

Page 38: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 38

Software Singleton Pattern

Page 39: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 39

Discussion: Singleton

• Typically instantiated lazily - the instance is not created until it is needed, perhaps never

• If stateful, analogous to a global variable (with many of the same problems as a global variable, e.g., unexpected side-effects)

• May need to ensure thread safety (if it is possible for one thread to be engaged in the creation of the instance while another is checking for null, possibly resulting in two instances)

• Can scale to two, three or more instances for load-balancing

Page 40: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 40

Singleton Pattern Example

Page 41: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 41

Other Creational Patterns

• Builder: separate the construction of a complex object from its representation so that the same construction process can create different representations

• Prototype: specify the kind of objects to create using a prototypical instance, a fully initialized instance is copied or cloned (not the same as prototypes used during software engineering lifecycle requirements phase)

• …

Page 42: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 42

Structural Patterns

• Concerned with composition

• Help you compose groups of objects into larger structures

• Eases design by identifying a simple way to realize relationships between entities

Page 43: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 43

Structural Patterns

• Proxy - a class functioning as an interface to another thing

• Façade - create a simplified interface of an existing interface to ease usage for common tasks

• Adapter – 'adapts' one interface for a class into an interface that a client expects

Page 44: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 44

Adapter Pattern• Convert or wrap the interface of a class into

another interface clients expect• Useful when an already existing class

provides some or all of the services needed but does not provide the interface needed

• Lets classes work together that could not otherwise because of incompatible interfaces

• Example: Convert the interface of a Document Object Model of an XML document into a tree structure that can be displayed

Page 45: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 45

Non-Software Adapter Pattern

Page 46: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 46

Software Adapter Pattern

Page 47: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 47

Discussion: Adapter

• Creates an intermediary abstraction that translates, or maps, the old component to the new system

• Makes heavy use of delegation where the delegator is the adapter (or wrapper) and the delegate is the class being adapted

• Responsible for handling any logic necessary to transform data into a form that is useful for the consumer

• Can wrap either an individual object instance or an aggregation of multiple object instances, and operate at either object or class level

Page 48: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 48

Adapter Pattern Example

Page 49: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 49

Individual Object Adapter

Page 50: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 50

Aggregate Adapter

Page 51: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 51

Object Adapter

Page 52: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 52

Class Adapter

Page 53: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 53

Other Structural Patterns• Bridge – separates a varying entity from a varying behavior,

decouples an abstraction from its implementation so that the two can vary independently (analogous to branching conditional logic)

• Composite – compose objects into a tree structure of simple and composite objects to represent part-whole hierarchies, lets clients treat individual objects and compositions of objects uniformly (e.g., root vs. internal vs. leaf node)

• Decorator – attach additional behavior(s) to an object dynamically, provides a flexible alternative to subclassing for extending functionality (e.g., pre and post processing)

• Flyweight – use sharing to support large numbers of fine-grained objects efficiently (e.g., each character object in a word processor shares reference to same object with font, formatting, etc.)

• …

Page 54: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 54

Behavioral Patterns

• Concerned with communication

• Identify common communication patterns between objects and realize these patterns

• Help you define the communication between objects and how the flow is controlled

Page 55: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 55

Behavioral Patterns

• Observer (Publish/Subscribe or Event Listener) - objects register to observe an event which may be raised by another object

• Mediator - defines simplified communication between classes

Page 56: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 56

Observer Pattern

• Define a one to many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically

• Encapsulate the core (or common or engine) components in a Subject abstraction, and the variable (or optional or user interface) components in an Observer hierarchy

Page 57: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 57

Non-Software Observer Pattern

Page 58: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 58

Software Observer Pattern

Subject Observer

Attach (Observer)Detach (Observer)Notify ()

Update ()

ConcreteSubject

GetState ()

subjectState

ConcreteObserver

Update ()

observerState

return subjectState

for all o in observers o -> Update ()

observerState = subject -> GetState ()

Page 59: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 59

Discussion: Observer

• Useful for dynamic relationships between objects, hook up a new observer while the program is running, unhook it later

• Often associated with the model-view-controller (MVC) paradigm: separates the display of object state from the object itself, e.g., when multiple distinct display views of state are needed

• Possible optimizations such as event compression (only sending a single change broadcast after a series of consecutive changes has occurred)

Page 60: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 60

Styles of Observer Notification

• Push – subject “publishes” a change and observers get notified of the change

• Pull – observers repeatedly poll the subject to note changes

• The subject does not know anything about the observers

• A single observer may monitor multiple subjects

Page 61: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 61

Example: Multiple Displays Enabled by Observer

Subject

A: 40B: 25C: 15D: 20

Observer 1 Observer 2

0

50

25

A B C D

A

B

C

D

Page 62: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 62

Mediator Pattern

• Define an object that encapsulates how a set of objects interact

• Promotes loose coupling by keeping objects from referring to each other explicitly, and allows to vary their interaction independently

• Defines simplified communication between classes where otherwise the interactions may be complex, with code buried inside those classes

• Example: Instant messaging

Page 63: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 63

Non-Software Mediator Pattern

Page 64: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 64

Software Mediator Pattern

Page 65: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 65

Discussion: Mediator

• Design an intermediary to decouple and orchestrate many peers, promotes the many-to-many relationships between interacting peers to "full object status“

• Like façade, provides a unified interface to a set of interfaces in a subsystem, different from façade in that the underlying classes interact with each other through the mediator

• Façade defines a simpler interface to a subsystem, it doesn't add new functionality, and it is not known by the subsystem classes (i.e., it defines a unidirectional protocol where it makes requests of the subsystem classes but not vice versa)

Page 66: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 66

Mediator Pattern Example

Page 67: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 67

More Behavioral Patterns• Chain of Responsibility – a way of passing a request

along a chain of objects, or choosing among a set of objects, avoid coupling the sender of the request to its receiver by giving more than one object a chance to handle the request

• Command – encapsulate a command request as an object, used to parameterize clients with different requests, queue or log requests, and support undoable operations

• Interpreter – implements a specialized computer language grammar to solve a specific set of problems (e.g., SQL)

• Iterator – sequentially access the elements of a collection, access the elements of an aggregate object sequentially without exposing its underlying representation

Page 68: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 68

More Behavioral Patterns

• Memento – capture, externalize and restore an object’s internal state (without violating encapsulation)

• State – alter an object’s behavior when its internal state changes, the object will appear to change its class

• Strategy – define a family of algorithms, encapsulate each one inside a class, and make them interchangeable; let’s the algorithm vary independently from clients that use it

• Template Method – define the skeleton of an algorithm and defer (some) exact steps to subclasses, lets subclasses refine certain steps of an algorithm without changing the algorithm’s structure

• Visitor - defines a new operation on the elements of an object’s structure without changing its class

• …

Page 69: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 69

Where to Get Code Examples• GoF book defines 23, with sample C++ and Smalltalk• Sample C# code for all patterns at

http://www.dofactory.com/Patterns/Patterns.aspx • Sample Java code for all patterns at

http://www.patterndepot.com/put/8/JavaPatternshtm • Sample Java and C++ code for all patterns

http://www.vincehuston.org/dp/, also “Who ya gonna call?”

• Some sample code in various languages at http://en.wikipedia.org/wiki/Design_Patterns, includes many patterns beyond original 23 (includes a new category of Concurrency Patterns)

Page 70: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 70

Summary

• Design Patterns write down and catalog common interactions between objects (or classes, or components) that programmers have frequently found useful

• Primarily applicable to OO programming, but also applies to some non-OO programming

• Intuition: Non-Software Examples of Software Design Patterns

• Another great source of intuition particularly wrt testing and cost-benefit issues, but only covers subset of patterns, at http://www.netobjectivesrepository.com/

Page 71: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 71

First Iteration Progress ReportDue Next Week!

• Tuesday 23 October, 10am

• Assignments posted on course website

• Submit via CourseWorks

• First Iteration Progress Report

Page 72: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 72

Upcoming Deadlines

• First iteration demo “week” October 30th – November 8th (schedule with your TA)

• First iteration final report due November 9th • Midterm Individual Assessment posted Friday

November 9th • Midterm Individual Assessment due Friday

November 16th Reminder: reading Szyperski is optional, but

reading Patton is required (you’ll need for FIA) – bookstore is sending back unsold texts this week

Page 73: 16 October 2007Kaiser: COMS W4156 Fall 20071 COMS W4156: Advanced Software Engineering Prof. Gail Kaiser Kaiser+4156@cs.columbia.edu

16 October 2007 Kaiser: COMS W4156 Fall 2007 73

COMS W4156: Advanced Software Engineering

Prof. Gail Kaiser

[email protected]

http://york.cs.columbia.edu/classes/cs4156/