1 (More) Pattern Games CS 236700: Software Design Winter 2004-2005/T10.

Slides:



Advertisements
Similar presentations
CHAPTER 4 Queues MIDTERM THURSDAY, OCTOBER 17 IN LAB.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
Bag implementation Add(T item) – Enlarge bag if necessary; allocate larger array Remove(T item) – Reduce bag if necessary; allocate smaller array Iterator.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Design Patterns I 1. Creational Pattern Singleton: intent and structure Ensure a class has one instance, and provide a global point of access to it 2.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
Iterators CS 367 – Introduction to Data Structures.
CS2110 Recitation 07. Interfaces Iterator and Iterable. Nested, Inner, and static classes We work often with a class C (say) that implements a bag: unordered.
JUnit The framework. Goal of the presentation showing the design and construction of JUnit, a piece of software with proven value.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CS 210 Introduction to Design Patterns September 28 th, 2006.
Case Studies on Design Patterns Design Refinements Examples.
04/29/ Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
©Fraser Hutchinson & Cliff Green C++ Certificate Program C++ Intermediate Decorator, Strategy, State Patterns.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Strategy Design Patterns CS 590L - Sushil Puradkar.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Mohammed Al-Dhelaan CSci 253 Object Oriented Design Instructor: Brad Taylor 06/02/2009 Factory Method Pattern.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Patterns Introduction
Using Software Design Patterns Bill Anderson. About me Fox developer since 1987 Fox developer since 1987 Program Director, Los Angeles Visual Foxpro Developers.
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
STRATEGY PATTERN. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Java Programming Persistent Data Types. Persistent Data Structure A persistent data structure is a data structure having an internal state that never.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
CS 210 Adapter Pattern October 17 th, Adapters in real life Page 236 – Head First Design Patterns.
STRATEGY PATTERN By Michelle Johnson. BACKGROUND Behavioral Pattern Allow you to define a family of algorithms, encapsulate each one, and make them interchangeable.
CS 350 – Software Design The Decorator Pattern – Chapter 17 In this chapter we expand our e-commerce case study and learn how to use the Decorator Pattern.
Csci 490 / Engr 596 Special Topics / Special Projects Software Design and Scala Programming Spring Semester 2010 Lecture Notes.
TEMPLATE METHOD DESIGN PATTERN -SWAPNIL SHAH. WHAT IS A DESIGN PATTERN… A design pattern is a general reusable solution to a commonly occurring problem.
Design Patterns: MORE Examples
Strategy: A Behavioral Design Pattern
Design Patterns: Brief Examples
Strategy Design Pattern
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Chapter 10 Design Patterns.
Design Patterns Lecture part 2.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University
Introduction to Design Patterns
Software Design and Architecture
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Introduction to Design Patterns
Design by Abstraction (Continuation) CS 3331 Spring 2005
Design Patterns (Gamma, Helm, Johnson, Vlissides)
Decorator Pattern.
Software Design Lecture : 39.
Software Design Lecture : 28.
Strategy Pattern Jim Fawcett CSE776 – Design Patterns Fall 2014.
Presentation transcript:

1 (More) Pattern Games CS : Software Design Winter /T10

2 Overview of the sample program  A single program Uses various collections of objects Each collection has different properties Where/How objects are stored? What happens when the size limit is exceeded? The basic collection class GenericCollection  The motivation: reconfiguration of GenericCollection Class level Object level  Design pattern Template Method Strategy  Additional patterns (involving the IResizeStrategy class) Flyweight Decorator

3 Participating classes  GenericCollection Roles: Context, Abstract Class  ArrayCollection Role: Concrete Class  IResizeStrategy Roles: Strategy, Component, Concrete Flyweight  UpperLimit Role: Concrete Decorator

4 Interface IResizeStrategy (short) public interface IResizeStrategy { public int newLimit(int index, int size); } public interface IResizeStrategy { public int newLimit(int index, int size); }  Defines the protocol for “choosing” the new size of the collection  Class GenericCollection will use it whenever it needs to be resized  It is possible to define two simple strategies…

5 Interface IResizeStrategy (full, 1/2) public interface IResizeStrategy { public int newLimit(int index, int size); public static final IResizeStrategy FACTOR2 = new IResizeStrategy() { public int newLimit(int index, int size) { return index * ; } }; public static final IResizeStrategy MINIMAL = new IResizeStrategy() { public int newLimit(int index, int size) { return index + 1; } }; } public interface IResizeStrategy { public int newLimit(int index, int size); public static final IResizeStrategy FACTOR2 = new IResizeStrategy() { public int newLimit(int index, int size) { return index * ; } }; public static final IResizeStrategy MINIMAL = new IResizeStrategy() { public int newLimit(int index, int size) { return index + 1; } }; }

6 Interface IResizeStrategy (full, 2/2)  IResizeStrategy provides two ready-made implementations These implementations can be shared by many clients These implementations do not have fields Sate is passed as parameters to method newLimit()  IResizeStrategy is a Flyweight class Shared instances Most of the context (state) is not held by the object

7 Flyweight: intent Use sharing to support large numbers of fine-grained objects efficiently.

8 Class UpperLimit (1/2) public class UpperLimit implements IResizeStrategy { private IResizeStrategy inner_; private int max_; public UpperLimit(IResizeStrategy rs, int max) { inner_ = rs; max_ = max; } public int newLimit(int index, int size) { int result = inner_.newLimit(index, size); return Math.min(result, max_); } public class UpperLimit implements IResizeStrategy { private IResizeStrategy inner_; private int max_; public UpperLimit(IResizeStrategy rs, int max) { inner_ = rs; max_ = max; } public int newLimit(int index, int size) { int result = inner_.newLimit(index, size); return Math.min(result, max_); }

9 Class UpperLimit (2/2)  Class UpperLimit takes an existing IResizeStrategy object and modifies its behavior  It meets two key conditions: Implements the IResizeStrategy protocol Uses an existing IResizeStrategy object to provide most of its behavior  This is a realization of the Decorator pattern

10 Decorator: intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality

11 Class GenericCollection (1/3) public abstract class GenericCollection { private int size_ = 0; public int size() { return size_; } protected abstract Object swap(int index, Object o); public Object get(int index) { if(index >= size()) throw new IndexOutOfBoundsException() Object result = swap(index, null); swap(index, result); return result; } // Cont. on next slide... public abstract class GenericCollection { private int size_ = 0; public int size() { return size_; } protected abstract Object swap(int index, Object o); public Object get(int index) { if(index >= size()) throw new IndexOutOfBoundsException() Object result = swap(index, null); swap(index, result); return result; } // Cont. on next slide...

12 Class GenericCollection (2/3) //... public Iterator iterator() { return new Iterator() { int index_ = 0; public void remove() { throw new UnsupportedOperationException(); } public boolean hasNext() { return index_ < size(); } public Object next() { if(!hasNext()) throw new NoSuchElementException(); return get(index_++); } }; } // Cont. on next slide... //... public Iterator iterator() { return new Iterator() { int index_ = 0; public void remove() { throw new UnsupportedOperationException(); } public boolean hasNext() { return index_ < size(); } public Object next() { if(!hasNext()) throw new NoSuchElementException(); return get(index_++); } }; } // Cont. on next slide...

13 Class GenericCollection (3/3) //... private IResizeStrategy rs_ = IResizeStrategy.MINIMAL; public void setResizeStrategy(IResizeStrategy rs) { rs_ = rs; } protected abstract void setLimit(int limit); public void put(int index, Object o) { if(index >= size()) { size_ = rs_.newLimit(index, size())); setLimit(size_); } swap(index, o); } } // End of class GenericCollection //... private IResizeStrategy rs_ = IResizeStrategy.MINIMAL; public void setResizeStrategy(IResizeStrategy rs) { rs_ = rs; } protected abstract void setLimit(int limit); public void put(int index, Object o) { if(index >= size()) { size_ = rs_.newLimit(index, size())); setLimit(size_); } swap(index, o); } } // End of class GenericCollection

14 Configurability of GenericCollection  GenericCollection offers two standard ways for customization: Overriding the abstract methods: swap(), setLimit() This is the Template Method pattern Changing the resize policy by invoking setResizeStrategy() This is the Strategy pattern  Template Method => vary the behavior of a class at compile-time  Strategy => vary the behavior of an object at run-time

15 Template Method: intent Define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure

16 Strategy: intent Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.

17 Class ArrayCollection public class ArrayCollcetion extends GenericCollection { private Object[] array_ = null; protected Object swap(int index, Object o) { Object result = array_[index]; array_[index] = o; return result; } protected void setLimit(int limit) { Object[] temp = new Object[limit]; if(array_ != null) { for(int i = 0; i < array_.length; ++i) temp[i] = array_[i]; } array_ = temp; } public class ArrayCollcetion extends GenericCollection { private Object[] array_ = null; protected Object swap(int index, Object o) { Object result = array_[index]; array_[index] = o; return result; } protected void setLimit(int limit) { Object[] temp = new Object[limit]; if(array_ != null) { for(int i = 0; i < array_.length; ++i) temp[i] = array_[i]; } array_ = temp; }

18 Client public static void main(String[] args) { GenericCollection c = new ArrayCollcetion(); c.setResizeStrategy(IResizeStrategy.FACTOR2); c.put(0, "zero"); c.put(1, "one"); c.put(3, "three"); System.out.println(c.get(0)); } public static void main(String[] args) { GenericCollection c = new ArrayCollcetion(); c.setResizeStrategy(IResizeStrategy.FACTOR2); c.put(0, "zero"); c.put(1, "one"); c.put(3, "three"); System.out.println(c.get(0)); }

19 Summary PatternAffected BehaviorVariability Template Method Storage: swap(), setLimit() Static Strategy Resize decision: put() Dynamic Decorator Resize decision: IResizeStrategy.newLimit() Dynamic