2 12 15
play

2/12/15 Template Method Design Patterns Problem: Some classes - PDF document

2/12/15 Template Method Design Patterns Problem: Some classes have a similar algorithm, but it is a little different for each class. Solution: Define the skeleton of the algorithm as a method in a superclass, deferring some steps to


  1. 2/12/15 Template Method Design Patterns ❚ Problem: Some classes have a similar algorithm, but it is a little different for each class. ❚ Solution: Define the skeleton of the algorithm as a method in a superclass, deferring some steps to subclasses. Template Method, Façade, Adapter, Observer, Command 427-22 2 CS361 1 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); paintChildren(co); ❚ Template Method separates the invariant part of an algorithm from the parts that vary with } each subclass 427-22 3 427-22 4 Where do template methods come from? Making Template Methods ❚ Two classes, each with a paint() method. ❚ Given two methods with same name in ❚ All getComponentGraphics and then different classes createSwingGraphics , but do different things ❙ Mark places that are DIFFERENT with them. ❙ Extract those places into separate methods in same ❚ Some have border, some don’t. Some have children, classes some don’t. ❙ Methods are now the same - move to superclass paintComponent(co); paintBorder(co); paintChildren(co); 427-22 5 427-22 6 1

  2. 2/12/15 Façade Design Pattern Why Façade? ❚ Hide complexity of subsystem Provide a unified interface to a set of interfaces in a subsystem. Define a higher-level interface that makes ❚ Make a single API instead of a set of classes the subsystem easier to use. ❚ Allow subsystem to change without affecting clients Facade 427-22 7 427-22 8 Adapter Design Pattern Adapter Pattern Intent: Convert the interface of a class into another Target Client interface clients expect. Adapter lets classes work together that couldn ’ t otherwise because of incompatible interfaces. Adapter Adaptee An adapter object will make the adaptee act like it has the target interface. 427-22 9 427-22 10 Observer Design Pattern Example ❚ Intent: when one object changes state, all the dependent objects are notified and updated. ❚ one-to-many dependency between objects ❙ Allows for consistency between related objects without tightly coupling classes ❘ i.e., “ reduces coupling between objects ” 11 CS361 1-12 2

  3. 2/12/15 Key Players Observer ❚ Subject Observer Observable ❙ Knows its observers – provides interface for adding/ removing observers Observer ❙ Defines an interface for notifying the subjects of changes to * Subject observer/dependent the object Update AddObserver ❚ Observer RemoveObserver ❙ Defines an updating interface Notify() ❚ ConcreteSubject ❙ Sends notification to observers when state changes PieChart BarChart DataObject ❚ ConcreteObserver Update Update ❙ Implements Observer interface to keep state consistent with subject CS361 13 CS361 14 Using Observer Dependence ❚ Decide whether object is Subject , or ❚ If package A depends on package B then you can ’ t run the tests for A Observer unless you also have B ❚ Subjects must call notify() when they change state ❚ Observers must define update() ❚ “ A depends on B ” means you can ’ t use A unless you have B ❚ Observers must register with Subjects CS361 15 CS361 16 Cycles of dependence Eliminating dependence If package A depends on package B then ❚ The Observer pattern eliminates package B should NOT depend on dependence. package A. ❚ Example: ❙ Suppose that “ FBIAgent ” is an observer of If classes C and D both depend on each Mobster. Mobster is a subclass of Subject, other (what?) so it can have observers. Class FBIAgent probably depends on class Mobster, but put them in the same package. Mobster does not depend on FBIAgent . CS361 17 CS361 18 3

  4. 2/12/15 Observer Observer example from Eclipse: Pattern Tracking Java Element Changes Subject - JavaCore Subject Observer * addElementChangedListener() addDependent: Update() removeDependent: Observer - ElementChangedListener() notify() elementChanged(ElementChangedEvent) ElementChangedEvent has a set of Mobster FBIAgent JavaElementDeltas robBank Update() driveCar CS361 19 CS361 20 Tracking in SWT Code Example public interface Subject { Just like Swing public void addObserver( Observer o ); 
 Subject - Button public void removeObserver( Observer o ); addSelectionListener() public void notify(); } Observe r - SelectionListener() widgetSelected(SelectionEvent) public interface Observer { widgetDefaultSelected(SelectionEvent) public void update( Subject o ); } CS361 21 CS361 22 Activity: implement code for Observer pattern Activity class IntegerSubject implements Subject{ List observers; - 1 Subject: the user inputs an int number (read from keyboard) public void addObserver(Observer ob){ - 3 Observers: one displays (e.g., prints to console) the number in observers.add(ob); Hex format, another in Octal format, another in Binary format } public void setState( int in ) { state = in; notify(); class IntegerSubject } implement addObserver,removeObserver,notify private void notify() { for (int i=0; i < totalObs; i++) { class HexObserver, OctObserver, BinaryObserver observers.get(i).update(this); implement update } CS361 23 CS361 24 } 4

  5. 2/12/15 Activity Command Pattern ❚ Intent: encapsulate a request as an object class HexObserver implements Observer { command ❙ you can pass the "command" around until you public void update(Subject subj) { can find which object can handle it , System.out.print( “” + ❙ you can undo it , Integer.toHexString(subj.getState()) ); ❙ you can put it on a queue } } ❚ Command will have an execute () and an undo () method ❚ Each command is a separate subclass of CS361 25 Command CS361 26 Command Order Waiter Execute() Command Execute() Invoker Undo() Hamburger Hot Dogs Fries Execute() Execute() Execute() Creator ConcreteCommand Execute() Undo() Chef Make Food() CS361 27 CS361 28 When to use Command When to use Command ❚ The Command object can be used when ❚ Support undo you need to tell the program to execute ❘ Commands can store additional state so the command later. that they can reverse the effect of a ❙ In such cases, you are saving commands as objects previous execute operation to be executed later ❚ Sending command objects over a network or saving them in a collection class CS361 29 CS361 30 5

  6. 2/12/15 Examples of Command pattern in Eclipse ❙ Every Refactoring is a command (performChange returns the undo change) ❙ Copy/Paste/Cut and most of the menu actions that change the source code CS361 1-31 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