Download presentation
Presentation is loading. Please wait.
Published byEleanor Booth Modified over 9 years ago
1
Design Patterns Based on The Design Patterns Java Companion by James Cooper http://www.patterndepot.com/put/8/JavaPatterns.htm
2
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
3
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?
4
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
5
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
6
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
7
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
8
Factory Example: Namer Name Factory Application getNamer(text) FirstFirst LastFirst create Depending order of name in text, creates an instance of a subclass of Namer
9
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
10
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
11
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 …
12
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; //clear flag when mouse not over thisComp.repaint(); } }); } …
13
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); } } }
14
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
15
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); }
16
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.