13
ECE 452 / CS 446 / SE464 ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Design Patterns: Part 2 - Answers Answers A Tutorial By Peter Kim A Tutorial By Peter Kim Partially based on the tutorial by Michał Partially based on the tutorial by Michał Antkiewicz Antkiewicz

ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz

Embed Size (px)

Citation preview

Page 1: ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz

ECE 452 / CS 446 / SE464ECE 452 / CS 446 / SE464Design Patterns: Part 2 - AnswersDesign Patterns: Part 2 - Answers

A Tutorial By Peter KimA Tutorial By Peter Kim

Partially based on the tutorial by Michał Partially based on the tutorial by Michał AntkiewiczAntkiewicz

Page 2: 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 PatternsEclipse 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

Page 3: ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz

Problem 1: Abstract Syntax Tree Problem 1: Abstract Syntax Tree AnalysisAnalysis

● 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

Page 4: ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz

Answer 1: AST Analysis - VisitorAnswer 1: AST Analysis - Visitor

Page 5: ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz

Problem 2: Connecting Viewer to ModelProblem 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)

Page 6: ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz

Answer 2: Connecting Viewer to Model - AdapterAnswer 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

Page 7: ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz

Problem 3: WorkbenchProblem 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

Page 8: ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz

Answer 3: Workbench - SingletonAnswer 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

Page 9: ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz

Problem 4: Subclassing SingletonsProblem 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.

Page 10: ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz

Answer 4a): Using a registryAnswer 4a): Using a registry

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));

}

}

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).

Workbench.getInstance(Environment.get(“WORKBENCH”).method();

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.

Page 11: ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz

Answer 4b): Using AbstractFactory+WrapperAnswer 4b): Using AbstractFactory+Wrapper

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;

}

}

• Different implementations of WorkbenchFactory create an instance of different subclasses of Workbench

• WorkbenchWrapper used to ensure single instance of Workbench

• Disadvantages: Larger code overhead and constructor of a Workbench class cannot be private (due to WorkbenchFactory)

• Loose coupling between subclass management and singleton enforcement/access

Page 12: ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz

Problem 5: ProxyProblem 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.

Page 13: ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz

Answer 5Answer 5

CallManager IHWResources

HWResources HWResourcesProxy

• getIPExtMapping(): Dictionary•isDirty(): boolean

• getIPExtMapping(): Dictionary• isDirty(): boolean

if(hwResources.isDirty())

hwResources = load();

return hwResources.getIPExtMapping();

1 1

0..*1

Cardinality is important!

• Performance (e.g. data caching)

• Space management (e.g. symbolic file links)

• Security (e.g. proxy controls access to the real subject)

• getIPExtMapping(): Dictionary• isDirty(): boolean