design patterns - university of albertajm/300/16_design patterns.pdf · 2012. 11. 12. · design...
TRANSCRIPT
Design Patterns
A Introduction to Object Orientated Design Patterns
Design Patterns - Why? Designing OO software is hard
Designing reusable OO software – harder
Experienced OO designers make good design choices?
New designers tend to fall back on non-OO techniques used before
Experienced designers know something ….
what is it?
Design Patterns - Why? Expert designers know not to solve every problem from first principles
They reuse solutions
Careful –
they reuse ideas NOT exact solutions
These patterns make OO designs more flexible, elegant, and ultimately reusable
Re-Use Code re-use
Don’t reinvent the wheel
Requires clean, elegant, understandable, general, stable code
leverage previous work
Design re-use Don’t reinvent the wheel
Requires a precise understanding of common, recurring designs
leverage previous work
What is a Design Pattern?
A design pattern
abstracts a recurring design structure
comprises a class or classes
dependencies,
structures,
interactions, or
conventions
distills design experience
Some Design Patterns
Strategy
Observer
Model View Controller (MVC)
Strategy Make algorithms interchangeable---”changing the guts”
Choice of implementation at run-time
Use when there are two or more ways of manipulating the same “data”
Strategy Operation()
ConcreteStrategy2 Operation()
ConcreteStrategy1 Operation()
means
generalization
or inheritance
Strategy Example Different ways to move a chess piece
MoveChessPiece
move(pointA, pointB)
TextualMove
move(pointA, pointB)
GraphicalMove
move(pointA, pointB)
NetworkMove
move(pointA, pointB)
Observer
Intent
Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Observer
Observer
Observer – In Action
Model-View-Controller (MVC) Pattern
MVC consists of three kinds of objects:
Model – the application object
View – UI (screen presentation)
Controller – defines the way the UI reacts to user inputs
MVC – What Is the Problem?
The same data needs to be accessed when presented in different views: e.g. HTML, JFC/swing, XML
The same data needs to be updated through different interactions
Supporting multiple types of views and interactions should not impact the components that provide the core functionality of the application
MVC – Solution
Separate core model functionality from: the presentation and
control logic
that uses this functionality
Allows multiple views to share the same data model
Makes supporting multiple clients easier to implement, test, and maintain
MVC Basics
Model. The model manages the behavior and data of the application domain, responds to
Requests for information about its state (from the view),
Responds to instructions to change state ( from the controller).
View. The view manages the display of information.
Controller. The controller interprets inputs from the user, informing the model and/or the view to change as appropriate.
Many … Many Views: Two examples are the rows and columns view of a spreadsheet and the pie chart view of some column in the same spreadsheet. We end up with a hierarchy of views.
Many Controllers: examples from Word are normal interaction, short cut keys for Word Perfect users, short cut keys adapted from different keyword layouts.
MVC Structure Model-View-Controller is made up of a
number of smaller design patterns.
The relationship between views and models is the ObserverPattern.
The controllers are StrategyPattern of the views.
Patterns vs “Design”
Patterns are High Level Design
Patterns transcend the “identify classes and associations” approach to design
Instead: learn to recognize patterns in the problem space and translate to the solution
Patterns can capture OO design principles within a specific domain
Patterns provide structure to “design”