CS 4773 Object Oriented Systems Introduction to Design Patterns
Skim through the first 5 chapters of the Design Patterns Book.
Read Chapters 6 and 7
Introduction
- Origin of design patterns: architecture and anthropology
- How do you recognize a good design?
- What is present in a good quality design that is not present in a poor
quality design?
- What is present in a poor quality design that is not present in a good
quality design?
- A pattern describes a problem that occurs over and over again.
- Architectural design patterns based on the writings of
Christopher Alexander.
- Software design patterns based on the writings of the
Gang of Four:
Design Patterns: Elements of Reusable Object-Oriented Software by
Gamma, Helm, Johnson and Vlissides.
- The book:
- applied the idea of patterns to software design,
calling them design patterns
- described a structure within which to catalog and describe
design patterns.
- cataloged 23 patterns
- postulated object-oriented strategies and approaches based on these
design patterns.
- Why study design patterns:
- reuse solutions
- establish common terminology
- Some strategies in creating good designs:
- Design to interfaces
- Favor aggregation over inheritance
i.e. has-a rather than is-a
- Find what varies and encapsulate it
The Facade Pattern
Original short description:
Provide a unified interface to a set of interfaces in a subsystem.
Facade defines a higher-level interface that makes the subsystem easier to use.
The facade pattern gives a simplified interface that hides the complexity
of the underlying system.
Example 1:
A client needs to make certain restrictive queries to a database.
The client does not need to know about the details of how to make general
database queries.
Example 2:
A client needs to be able to ask a question and return
a yes or no answer from an interactive user.
Solution:
- Pop up a window containing the question and two buttons, one for
yes and one for no.
- Design a class, QuestionAsker that has a constructor with
a String parameter (the question).
- Two public methods will be:
public boolean getAnswer()
public String getQuestion()
- How would you implement this?
First implementation:
- The QuestionAsker extends JFrame and implements
ActionListener.
- The constructor sets the size appropriately, puts in the message and 2
buttons.
- The getAnswer method makes the frame visible, waits for a
button to be pushed and returns true or false.
How well does this do?
What are the public methods of QuestionAsker?
Second implementation:
- The QuesitonAsker extends Object and implements
ActionListener
- The constructor creates a private class that extends JFrame that has the question and buttons. Actions are sent back to QuestionAsker.
- This is a has-a rather than an is-a.
- The rest is the same.
- What are the public methods of this class?
Third implementation:
- The QuesitonAsker extends Object
- The constructor creates a private JFrame and a private class that
extends JPanel
and implements ActionListener.
- The rest is the same.
- This class has only one public method and the entire inner workings
of the class are private (encapsulation).
The Adapter Pattern
Original short description:
Convert the interface of a class into another interface that the clients expect.
Adapter lets classes work together that could not otherwise because of
incompatible interfaces.
The Adapter pattern is used when the client's interface is already determined
but does not match the existing interface needed class.
Example:
Before Java existed there was an animation package called XTango.
- It allowed a C program to do graphics by outputting lines to standard
output and having them redirected to the XTango package.
- This package created a window in which you could draw.
- It allowed for drawing of lines, boxes and circles.
- Coordinates were given as floating point numbers with (0,0) at the
upper left and (1,1) at the lower right.
- To draw a circle you would specify the center and the radius.
- To implement this in Java so that the calling program did not have to change
required making a new interface.
- The draw_circle(double x, double y, double r)
would call the standard
Java drawOval(int x, int y, int width, int height)
- This had to be done for each of the drawing routines.
- There were also routines to move an object from one place to another.
This had to be implemented separately for each type of object, but
provided a polymorphic interface.
Comparison of Facade and Adapter Patterns
Facade | Adapter |
Provides a wrapper for existing classes |
Provides a wrapper for existing classes |
No predefined interface for the client |
A predefined interface for the client exists |
Simplifies the interface to existing classes |
Changes the interface to existing classes |
Hides the interface of the existing classes |
Not required to hide the existing interface |
Not interested in polymorphic behavior |
May be interested in polymorphic behavior |
Often hides some functionality of existing classes |
Usually does not hide functionality of existing classes |