Design Pattern
Download
Report
Transcript Design Pattern
Design Patterns
Based on The Design Patterns
Java Companion by James Cooper
http://www.patterndepot.com/put/8/JavaPatterns.htm
Design Pattern
A mechanism for naming and
communicating classic solutions to
programming problems
Example: How can you go sequentially
through all of the elements in a
container?
Solution: Iterator
Behavioral Pattern: Iterator
Problem: need to be able to move through
container elements
Get first element
Test for completion (past last element)
Get next element
Get the current element
Issues:
Effect of adding/deleting elements to container
What is the “next element” in some containers?
Iterator Pattern: Java Implementation
public interface Enumeration
{
public boolean hasMoreElements();
public Object nextElement();
}
How do we get the first element?
How do we get the current element?
How would we use it?
Issues: Object as return type
Variation: Filtered iteration
History
Design patterns have been around since early
80’s as a term
Formalized by Design Patterns: Elements of
Reusable Software by Gamma, Helm,
Johnson, and Vlissides (1995)
“Gang of Four”
Presented 23 design patterns in three
categories
Types of Patterns
Creational
Solve problems in object creation
Structural
Solve problems in how objects are combined to
form structures
Behavioral
Solve problems in object behavior and interobject
communication
Factory Pattern: Creational
Problem: need to be able to create
objects but the type of object varies
Usually the new objects have a common
parent class and common methods
Factory Example: Namer
create
FirstFirst
Name
Factory
getNamer(text)
Application
LastFirst
Depending order of name in text, creates an
instance of a subclass of Namer
Decorator Pattern: Structural
Problem: need to be able to add
something to objects
Could do this by creating a new
subclass, but could get messy
Instead, create a Decorator class that
contains the object to be modified
Decorator Example: CoolDecorator
public class Decorator extends Component {
public Decorator(Component c){
setLayout(new BorderLayout());
add(c,BorderLayout.CENTER);
}
}
Now subclass Decorator with behavior we want
Decorator Example: CoolDecorator
public class CoolDecorator extends Decorator
{
boolean mouse_over;
//true when mouse over component
Component thisComp;
public CoolDecorator(Component c)
{
super(c);
mouse_over = false;
thisComp = this;
…
//save this component
Decorator Example: CoolDecorator (contd.)
//catch mouse movements in inner class
c.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent e) {
mouse_over=true;
//set flag when mouse over
thisComp.repaint();
}
public void mouseExited(MouseEvent e) {
mouse_over=false;
thisComp.repaint();
}
});
}…
//clear flag when mouse not over
Decorator Example: CoolDecorator (contd.)
//paint the component
public void paint(Graphics g) {
super.paint(g);
//first draw the component
if(! mouse_over) {
//if the mouse is not over the component
//erase the borders
Dimension size = super.getSize();
g.setColor(Color.lightGray);
g.drawRect(0, 0, size.width-1, size.height-1);
g.drawLine(size.width-2, 0, size.width-2, size.height-1);
g.drawLine(0, size.height-2, size.width-2, size.height-2);
} } }
Façade Pattern: Structural
Problem: need a single, simplified
interface to a complicated subsystem
Create a Façade class that provides the
user with only the functionality needed
Advanced users can still access other
functionality
Façade Example: Database
class Database {
public Database(String driver);
public void close();
public void Open(String url, String cat);
public String[] getTableNames();
public String[] getTableMetaData();
public String[] getColumnMetaData(String tablename);
public String[] getColumnNames(String table);
public String getColumnValue(String table, String columnName);
public String getNextValue(String columnName);
public resultSet Execute(String sql);
}
Pros and Cons of Design Patterns
Pros
Provide a vocabulary for understanding
and discussing designs
Improve performance or flexibility of code
Cons
Can increase complexity because of
overdesign