2 10 15
play

2/10/15 Today s goals Design Patterns What are Design Patterns? - PDF document

2/10/15 Today s goals Design Patterns What are Design Patterns? Announcements: Thank you for your Early Informal Feedback Composite, Visitor, HW3 Phase 2, peer reviews due this Thu at Template Method, Faade, 5pm


  1. 2/10/15 Today ’ s goals Design Patterns ❚ What are Design Patterns? ❚ Announcements: ❙ Thank you for your Early Informal Feedback Composite, Visitor, ❙ HW3 – Phase 2, peer reviews due this Thu at Template Method, Façade, 5pm Adapter Examples from Eclipse cs361 2 cs361 1 Defining Patterns How to Design Design Patterns – expert solutions to recurring problems in a certain domain ❚ Problem Description usually involves problem definition, driving forces, ❙ Model the problem, analysis, requirements solution, benefits, difficulties, related patterns. ❚ Patterns ❙ Remember similar solutions Pattern Language - a collection of patterns, guiding the users ❚ Patch through the decision process in building a system ❙ Test and fix, repair Patterns are related (high level-low level) cs361 3 4 Example Design Space Software Patterns “Lunch” Pattern “Eat” Pattern Language Definition ❚ Experts build up a set of techniques - we get hungry every lunch hour. - usually around 11:30am – 1:00pm. ❚ “ Best practices ” - good enough practices Wake-Up - must resolve hunger. Driving Forces ❚ Patterns - appetite, intensity of hunger, nearby restaurants Breakfast ❙ Recurring arrangements of elements Solution ❙ Documenting expertise Brunch - eat at nearest restaurant that satisfies minimum appetite requirement. Lunch Benefits ❙ An excuse to describe something that doesn ’ t fall - hunger is resolved Difficulties into standard categories (algorithm, data structure) Dinner - must not fall asleep afterwards. Related Patterns - dinner, breakfast, brunch, and snack cs361 6 5 1

  2. 2/10/15 Design Patterns: Elements of Lots of books on patterns Reusable Object-Oriented Software ❚ Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides ❚ Addison-Wesley 1995 ❚ 23 object-oriented patterns cs361 7 cs361 1-8 Composite design Fundamental OO design pattern principles ❚ Context: ❚ Open-Closed principle: ❙ Developing OO software ❙ “classes should be open for extension, but ❚ Problem: closed for modification” ❙ Complex part-whole hierarchy has lots of similar classes. ❚ Distribute the implementation ❘ Example: document, chapter, section, ❙ Don’t have one object that gets too paragraph. complicated and too smart. Each object ❘ Want to be able to seamlessly add new parts knows how to do it’s small part. cs361 1-9 cs361 10 Composite Forces ❚ Idea: make abstract "component" class. ❚ Alternative 1: every component has a ❙ power -- create new kind of part by (possibly empty) set of components. composing existing ones ❚ Problem: many components have no ❙ simplicity -- treat composition of parts like a part components. * ❙ safety -- no special cases, treat everything Component Children the same Chapter Paragraph ... cs361 11 cs361 12 2

  3. 2/10/15 Composite Two o des design ign alt alter ernat nativ ives es for or Pattern Par art-of -of Composite and : 1. Component does not know what it is a part of Component have the + Component can be in many composites. exact same interface. Component * - Component can be accessed only through its container composite. • enumerating children children: • Component has empty 2. Component knows what it is a part of iterator Leaf Composite - Component can be in only one composite. • only Composite adds + Component can be accessed directly. removes children. cs361 13 cs361 14 Part-of Ensuring Consistency ❚ Rules when component knows its single ❚ The public operations on components and composite. composites are: ❙ A is a part of B if and only if B is the composite of A. ❙ Composite can enumerate components. ❚ Duplicating information is dangerous! ❙ Component knows its container. ❙ Add/remove a component to/from the ❚ Problem: how to ensure that pointers composite. from components to composite and composite to components are consistent? cs361 15 cs361 16 addChild() in Composite public void addChild(Component child) { ❚ The operation to add a component to a ensureCapacity(); composite updates the container of the childArray[numChildren++] = child; component. There should be no other way child.container = this; to change the container of a component. } cs361 17 cs361 18 3

  4. 2/10/15 Book Example A book ❚ Book * DocumentComponent ❙ Chapter ❘ Section • Paragraph • Paragraph Paragraph Composite ❘ Section • Paragraph ❙ Chapter Book Section ❘ Section Chapter • Paragraph cs361 19 cs361 20 Composite in Eclipse UI (SWT) Composite in Eclipse Nodes ❚ Widget (abstract class) ❚ IJavaElement (interface) ❙ Control ❙ Leaf nodes IMember: ❘ Button, Label, ProgressBar, … IField, IMethod, … ❙ Composite ❙ Composite nodes: ❘ Browser, Canvas, Table, Tree, List, … ICompilationUnit, IPackage..... cs361 21 cs361 22 When to Centralize Visitor pattern Algorithm Use centralized algorithm when you need to: Visitor lets you centralize algorithm, lets • change entire algorithm at once you create a family of algorithms by • look at entire algorithm at once inheritance, and lets you define a new • work with only a few kinds of components operation without changing the classes • change algorithm often, but not add new classes of the elements on which it operates. of components - change the algorithm without changing the components (open-closed principle) Major problem is that adding a new kind of parse node requires adding a new function to each visitor. 427-21 23 427-21 24 4

  5. 2/10/15 Related Patterns ❚ Several patterns are often used with Interpreter/Visitor ❙ Composite - to make tree ❙ Iterator - to make traversal more abstract ❙ Template Method - to put reusable code in abstract class cs361 1-25 427-21 26 Who is responsible for the 1: The Components traversal algorithm ? If the component handles traversal, it looks like: The components? public Object accept(Visitor visitor) { The Visitor? visitor.visitA(this); for (Enumeration e = children(); A separate iterator in the client? e.hasMoreElements) { item = (Item) e.NextElement(); item.accept(visitor); } } Otherwise, it looks like: public Object accept(Visitor visitor) 427-21 27 427-21 28 {visitor.visitA(this);} 3: An Iterator from 2: The Visitor Client code If the visitor handles iteration, it looks like: If client calls iterator, it looks like: public Object visitA(ComponentA c) { visitor = new ConcreteVisitor. // do something with c for (Iterator i = component.iterator(); for (Iterator i = c.children(); !i.isDone(); i.Next()) { !i.isDone(); i.next()) { ((Component) i.CurrentItem()).accept(visitor); ((Component) } i.CurrentItem()).accept(visitor); } } } Otherwise Visitor visitA just interacts with Otherwise, the client looks like: componentA. ConcreteVisitor visitor; treeRoot.accept(visitor); 427-21 29 427-21 30 5

  6. 2/10/15 Visitor in Eclipse Visitor in Eclipse public public abstract class abstract class ASTVisitor { class Assignment { public public void void visit(Assignment node) { return true; public void void accept(ASTVisitor visitor){ } boolean boolean visitChildren = visitor.visit( visitChildren = visitor.visit(this this); ); if if (visitChildren) { (visitChildren) { several dozens more // visit children in normal left to right reading order acceptChild(visitor, getLeftHandSide()); } acceptChild(visitor, getRightHandSide()); } Many Eclipse tools (e.g., refactoring, code completion) walk } over the AST nodes 427-21 31 427-21 32 Visitor in Eclipse Template Method ❚ Problem: Some classes have a similar algorithm, but it ❚ Visitor works best when there are no new is a little different for each class. subclasses ❚ Solution: Define the skeleton of the algorithm as a method in a superclass, deferring some steps to ❚ Visitors work well for compilers. Why? subclasses. 427-21 33 427-22 34 In Swing’s JComponent Template Method public void paint(Graphics g) { ❚ A template method calls abstract methods. Graphics cg = getComponentGraphics(g); ❚ Subclasses provide concrete implementations Graphics co = of abstract methods. SwingGraphics.createSwingGraphics(cg); ❚ Template method in superclass calls methods paintComponent(co); in subclass paintBorder(co); ❚ Template Method separates the invariant part paintChildren(co); of an algorithm from the parts that vary with } each subclass 427-22 35 427-22 36 6

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend