Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable...

22
Spring 2010 CS 225 1 Design Patterns
  • date post

    21-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable...

Spring 2010 CS 225 1

Design Patterns

Spring 2010 CS 225 2

What is a Design Pattern?• "a general reusable solution to a commonly occurring problem in software design" (Wikipedia)

• "descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context" (Gamma et al.)

Spring 2010 CS 225 3

Examples from Other Fields

• Architecture: You can build many houses from the same floor plan without having any that are identical.

• Creative Writing: – Macbeth and Hamlet are both variations of

the "tragically flawed hero" story– Genre fiction (e.g. romances) are based on

a common sequence of plot elements.

Spring 2010 CS 225 4

Simple Programming Examples

• Console menu: programs which are based on a loop which waits for a user to select from a limited number of choices and acts on that selection

• Maintaining a collection:

• Two-person game:

Spring 2010 CS 225 5

Elements of a design pattern• Pattern name• Problem for which the pattern provides

a solution• Solution - elements that make up the

design– relationships– responsibilities– collaborations

• Consequences

Spring 2010 CS 225 6

Pattern Categories• Creational

– Factory Method– Singleton

• Structural– Composite– Decorator

• Behavioral– Iterator– Strategy– Observer

Spring 2010 CS 225 7

Iterator

• Problem: a client class need access to individual elements of an aggregate object

• Solution: Define an iterator class that steps through the elements one at a time and remembers where it is in the sequence.

Spring 2010 CS 225 8

Factory Method

• Allows instantiation of an object to be deferred to a subclass– iterator method of a collection class is a

factory method

Spring 2010 CS 225 9

Components and Containers

• Components are grouped together into containers

• Containers can also be added to other containers– Containers need to be components as well

• Composite pattern provides a solution

Spring 2010 CS 225 10

Composite

• Combines several objects into an object with the same behavior as the parts

Spring 2010 CS 225 11

Layout Managers

• Consider the problem of organizing components within a container– Different layout managers use different

rules for how to organize the components

• This is an example of the strategy pattern

Spring 2010 CS 225 12

Strategy

• Define a family of algorithms which are interchangeable

• Uses– Allows one class to have different

behaviors– Allow the use of variants of an algorithm– Allow algorithms to hide data that client

doesn't need to see

Spring 2010 CS 225 13

Decorator• Enhances the behavior of a single

component– e.g. scroll bars

Spring 2010 CS 225 14

Singleton

• Used to insure that a class only has one instance

• The one object should be globally accessible

Spring 2010 CS 225 15

Command

• Encapsulate a request as an object

Spring 2010 CS 225 16

Frameworks

• Framework is a set of cooperating classes that implement mechanisms essential to a particular problem domain

• Frameworks can be customized to a particular application within the domain

• Java has a collections framework

Spring 2010 CS 225 17

Model-View-Controller Architecture

• Consider a program that shows you two different views of the same data

• MVC architecture has three parts– Model - the data, not visible– View - a visual representation of the data– Controller - Processes user interaction

• Observer pattern is used for the view(s)

Spring 2010 CS 225 18

Observer Pattern

• Define a one-to-many dependency between objects

• Changes in the one object are reflected in all the dependent objects

• Used to maintain consistency between objects

Spring 2010 CS 225 19

Visitor Design Pattern• This is a common way of handling the

process of performing an action on each element of a collection

• The Visitor interface has a single method– visit( E element)

• The collection class can have traversal methods that take a Visitor as a parameter

Spring 2010 CS 225 20

Adaptor

• You have a class that has all the functionality required by a client but doesn't match the required interface

Spring 2010 CS 225 21

Antipatterns

• Designs that should be avoided– Blob - a class with many unrelated

responsibilities– Poltergeist - creates short-lived objects

with no significant responsibilities

Spring 2010 CS 225 22

References

• Design Patterns: Elements of reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides

• Object-Oriented Design and Patterns by Cay Horstman

• http://hillside.net/patterns