Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 (More) Pattern Games CS 236700: Software Design Winter 2004-2005/T10."— Presentation transcript:

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

2 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 3 Participating classes  GenericCollection Roles: Context, Abstract Class  ArrayCollection Role: Concrete Class  IResizeStrategy Roles: Strategy, Component, Concrete Flyweight  UpperLimit Role: Concrete Decorator

4 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 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 * 2 + 10; } }; 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 * 2 + 10; } }; public static final IResizeStrategy MINIMAL = new IResizeStrategy() { public int newLimit(int index, int size) { return index + 1; } }; }

6 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 7 Flyweight: intent Use sharing to support large numbers of fine-grained objects efficiently.

8 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 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 10 Decorator: intent Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality

11 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 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 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 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 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 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 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 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 19 Summary PatternAffected BehaviorVariability Template Method Storage: swap(), setLimit() Static Strategy Resize decision: put() Dynamic Decorator Resize decision: IResizeStrategy.newLimit() Dynamic


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

Similar presentations


Ads by Google