1 / 13

Eclipse and Patterns

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

neorah
Download Presentation

Eclipse and Patterns

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECE 452 / CS 446 / SE464Design Patterns: Part 2 - AnswersA Tutorial By Peter KimPartially based on the tutorial by Michał Antkiewicz

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

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

  4. Answer 1: AST Analysis - Visitor

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

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

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

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

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

  10. 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();

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

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

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

More Related