130 likes | 282 Views
ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz. Eclipse and Patterns. Tutorial based on Erich Gamma, Kent Beck, “Contributing to Eclipse, Principles, Patterns and Plug-ins”, Addison-Wesley, 2004
E N D
ECE 452 / CS 446 / SE464Design Patterns: Part 2 - AnswersA Tutorial By Peter KimPartially based on the tutorial by Michał Antkiewicz
Eclipse and Patterns • Tutorial based on • Erich Gamma, Kent Beck, “Contributing to Eclipse, Principles, Patterns and Plug-ins”, Addison-Wesley, 2004 • Rod Waldhoff’s blog, Implementing the Singleton Pattern in Java, http://radio.weblogs.com/0122027/stories/2003/10/20/implementingTheSingletonPatternInJava.html • Extensive use of design patterns • Plug-in architecture
Problem 1: Abstract Syntax Tree Analysis • In Java Development Tools (JDT) Java code is parsed and represented internally as an abstract syntax tree • ASTNode represents any node in an abstract syntax tree • Problem: how to encapsulate various AST analysis algorithms such that it is easy to add new algorithms. Assume that the presented class hierarchy is stable • Note that analysis algorithm needs to maintain certain information during tree traversal
Problem 2: Connecting Viewer to Model • JFace plug-in provides viewers for standard SWT widgets such as tree, table and list. The viewer's main responsibility is to populate a widget with objects from the model. To do that, a viewer needs to traverse the model and retrieve strings (labels) and icons that the widget can present • On the other hand a viewer is completely independent from the model it is displaying and the model knows nothing about being displayed (that means viewer and model interfaces are incompatible) • Good solution is Model-View-Controller pattern • Which pattern is the most appropriate to connect the viewer (controller) with any model? (viewer needs information such as children, parent, image, text)
Answer 2: Connecting Viewer to Model - Adapter • Concrete adapters MyModelLabelProvider and MyModelTreeContentProvider are domain specific (know how to access information from the concrete model) • Adapters implement interfaces necessary for the viewer to operate independently from particular model it is displaying
Problem 3: Workbench • In Eclipse, the workbench class represents the top of the Eclipse user interface. Its primary responsibility is the management of workbench windows, dialogs, wizards, and other workbench-related windows. • What pattern can be used to • ensure no one can create instances of Workbench, • only one instance of Workbench is accessible globally, • ensure, there can be at most one instance. • Present appropriate fragments of code
Answer 3: Workbench - Singleton • Private constructor forbids direct creation of instances • getInstance() returns the one and only instance • Public createAndRunWorkbench(...) used by Platform calls private constructor, which ensures only one instance is created
Problem 4: Subclassing Singletons Workbench is to have subclasses, such as EnterpriseWorkbench (for high-end business use) and LightweightWorkbench (for novice use). Assume that the subclass may be chosen at any point in time prior to accessing the Workbench singleton. Show two ways of subclassing the singleton and explain a disadvantage of each way.
Answer 4a): Using a registry Before runtime, each subclass of Workbench is placed into the registry. This is the only place to call the constructor. E.g. LightweightWorkbench would do Workbench w = new LightweightWorkbench(); (note, in Java, the class loading sequence seems to be non-detemrinistic…may have to register all direct/indirect subclasses of Workbench at once (through reflection, for example). public class Workbench { static protected Dictionary registry = new Hashtable(); static { Workbench w = new Workbench(); registry.put(w.getClass().getName(), w); } protected Workbench() { … } static public Workbench getInstance(String workbenchClassName) { return (Workbench)(registry.get(workbenchClassName)); } … } At runtime, the particular singleton desired is referenced by its class name. Disadvantage: a singleton for every Workbench class is created, but only one is actually used. Workbench.getInstance(Environment.get(“WORKBENCH”).method();
Answer 4b): Using AbstractFactory+Wrapper • Different implementations of WorkbenchFactory create an instance of different subclasses of Workbench public interface WorkbenchFactory { public Workbench createWorkbench(); } final public class WorkbenchWrapper { private static Workbench workbench; private static WorkbenchFactory factory; public static void setFactory(WorkbenchFactory factory) { this.factory = factory; } public static Workbench getInstance() { if(workbench == null) workbench = factory.createWorkbench(); return workbench; } … } • WorkbenchWrapper used to ensure single instance of Workbench • Loose coupling between subclass management and singleton enforcement/access • Disadvantages: Larger code overhead and constructor of a Workbench class cannot be private (due to WorkbenchFactory)
Problem 5: Proxy Give 3 types of situations where the Proxy design pattern may be applicable and provide an example for each. Think of at least one situation that’s relevant to your project.
Answer 5 • Performance (e.g. data caching) 1 1 CallManager IHWResources • getIPExtMapping(): • Dictionary • isDirty(): boolean if(hwResources.isDirty()) hwResources = load(); return hwResources.getIPExtMapping(); HWResources HWResourcesProxy • getIPExtMapping(): • Dictionary • isDirty(): boolean 1 0..* • getIPExtMapping(): • Dictionary • isDirty(): boolean Cardinality is important! • Space management (e.g. symbolic file links) • Security (e.g. proxy controls access to the real subject)