Presentation is loading. Please wait.

Presentation is loading. Please wait.

Template, Command, Iterator and Composite CSE 111 5/9/20151Copyright W. Howden.

Similar presentations

Presentation on theme: "Template, Command, Iterator and Composite CSE 111 5/9/20151Copyright W. Howden."— Presentation transcript:

1 Template, Command, Iterator and Composite CSE 111 5/9/20151Copyright W. Howden

2 Template Pattern Context – We have an algorithm that can be used in different situations with different steps Problem – How to reuse the code Solution – Define the steps as abstract functions. You then extend, defining concrete functions as per the context 5/9/2015Copyright W. Howden2

3 Template Methods and Hooks Template methods: If there are some steps in a method that need to be filled in later, encapsulate them in an abstract method, and then extend the class with a definition for them Hooks: If there are some potential steps that you might like to add later, encapsulate them in a concrete method that does nothing. Then extend the class with an alternative concrete class. 5/9/2015Copyright W. Howden3

4 Hooks with Pamela, Fred, Jane and Sally In our souvenir manufacturing examples, all orders came in through Jane. We could add a hook to Jane so that if we wanted to we could generate a log of the orders she delivers, or do some other as yet unanticipated step when an order is fulfilled. To instantiate the hook we extend Jane with a definition for the currently empty hook method. 5/9/2015Copyright W. Howden4

5 DS Templates, and Hooks For a DS example, we could add hooks for logging functions. For example, the DateRequest objects’ execute() method could have a hook at the end that we could use to generate an item in a log Or we could use the same hook to put in code to double-check that the date that is found actually matched the input preferences 5/9/2015Copyright W. Howden5

6 Pamela’s Manufacturer - Factory Pattern Factory Pattern – abstract manufacturer with abstract factory method that returns instances satisfying AbstractFred interface – AbstractFred is the souvenir builder subclass with a concrete Fred tailored to local market – Subclass the manufacturer with a method that calls the constructor for the local Fred 5/9/2015Copyright W. Howden6

7 Factory Pattern Inheritance Hierarchies 5/9/2015Copyright W.E. Howden7

8 Using Templates in the Factory Pattern Back to Pamela’s manufacturing company suppose souvenirs from different areas are the same except for one or two little steps in building them (e.g. adding the city name) Use the same abstract manufacturer with abstract method with AbstractFred interface return type Abstract Fred has a builder function with several undefined steps using abstract methods Subclass AbstractFred with concrete Fred using concrete builder steps 5/9/2015Copyright W. Howden8

9 Which One to Use? Template for subclasses of program class that define alternative steps in a method Factory pattern for defining completely different subclasses with new methods. The subclasses define alternative classes/constructors. Template pattern is for simpler situations 5/9/2015Copyright W. Howden9

10 The Hollywood Principle? “Don’t call us, we will call you” i.e. Higher level components determine how and when lower level components are used In Template, the algorithm calls the concrete instantiations of its abstract methods Other examples – layers system design – object factory 5/9/2015Copyright W. Howden10

11 Command Pattern Context Actions to be performed Problem May need to delay executing, undo them or have them done at a remote location Solution Delink client from actions. Variations on degree of delinking 5/9/201511Copyright W. Howden

12 Details Abstract command class with execute() and possibly undo() operations You subclass this with a command subclass. The command subclasses have parameterized constructor. Parameters instance of a class whose methods will be executed to do the command data needed for command execution 5/9/2015Copyright W. Howden12

13 Command Pattern Terminology Client creates the command, passing in the receiver object as a parameter, plus functional parameters Invoker executes the execute() method in the command Receiver object whose methods are executed by the command execute() method 5/9/2015Copyright W. Howden13

14 Implementing undo() methods in the Command Pattern The execute() of a command, causes a state change in a receiver object x The state S of x is saved in the command as part of the execute() If an undo() is performed, it examines S and performs the methods in x necessary to restore its state to S 5/9/201514Copyright W. Howden

15 Fred, Jane and Sally When Fred wants to make a reservation, he gives the information to Sally and tells her to go see reservations specialist Jane Sally provides Jane with the details and asks for reservation Sally remembers the reservations information If Fred changes his mind, he tells Sally to undo the reservation Sally provides Jane with the cancellation information 5/9/2015Copyright W. Howden15

16 Analysis Sally is an instance of the reservations command subclass. She is instantiated with the reservations specialist (receiver) whose methods she will employ plus some data. Sally is instantiated by (client/invoker) Fred with the reservations information plus the reservations object (receiver) Jane Jane’s methods, when executed with the appropriate data will cause the reservation to be made Fred causes the reservation to be cancelled by causing Sally’s undo() method to be performed – Sally saved the information necessary to undo the reservation in her class variables – She may have saved this and other information for undo() by calling on methods for Jane 5/9/2015Copyright W. Howden16

17 Observations Sally is just a kind of shell that administers the execution and undoing of commands She is a reservations shell because she expects a reservations object, which has the logic for making reservations. Other kinds of Sallys would do other kinds of commands and accept different kinds of receiver objects 5/9/2015Copyright W. Howden17

18 DS Command Example GUI input for updating the data of a member GUI sends message to DL DL creates an update command instance to perform this, and calls its execute() method – passes in update data through constructor – saves the completed command GUI input requests undo GUI sends an undo message to DL DL calls undo() method of last command 5/9/2015Copyright W. Howden18

19 DS Example Details Update command will have to get current info for member from the DataBase in order to save it (in itself). How it gets it depends on implementation of DL update classes/methods Undo() method for the update() command will restore DB using the saved data for the member 5/9/2015Copyright W. Howden19

20 DS Example Analysis The logic for performing the command is inside the command subtype, and is not delinked by having a separate receiver object The DL interface is both the client (creates the command) and the invoker (performs its execute() method The command is its own receiver, since it contains the methods to perform the command 5/9/2015Copyright W. Howden20

21 Additional Applications of Command Pattern Thread pools are used to execute tasks from a task queue – Implement tasks as commands – Client: creates command and puts in queue – Invoker: thread executes the command – Receiver: code to be executed by a thread 5/9/2015Copyright W. Howden21

22 Iterator Pattern Context – compound objects, need to iterate over them Problem – do not want to reveal details of collection design and implementation to outside clients, or allow them to access it Solution – encapsulate the iteration in an iterator class with iteration methods. Each collection has its own createIterator method, which will return an instance of Iterator for any instance of that class. 5/9/2015Copyright W. Howden22

23 Jane, Sally and Iterators Jane and Sally are roommates, and share a closet and shoe storage Jane is neat and Sally is a, well, not-neat Every morning Sally goes through the whole closet, looking at all her clothes to pick one out. She makes a mess of everything. Jane agrees to be the closet-iterator. She responds to get-next() and are-there-more() The same problem exists with the shoe storage so Jane becomes an Iterator for the shoes, satisifying the same Iterator interface. 5/9/2015Copyright W. Howden23

24 DS and Iterators Suppose that we maintain a log of all dates that are made We add a feature wherein a member-user can ask to step thru the list of all dates that have been made for that user We do this with a custom iterator. The log class has a method createIterator(userName) from which it returns an Iterator that satisfies the Iterator interface 5/9/2015Copyright W. Howden24

25 Compound Pattern Context – Collections and subcollections of object Problem – need to process items in collection – arbitrary structures Solution – recursive data structure component can be either a composite or a primitive composites are constructed from components 5/9/2015Copyright W. Howden25

26 Recursive Structures Example char -> A | B |... | Z string -> char | string + char 5/9/2015Copyright W. Howden26

27 Copyright W. Howden27 Composite Class Structure

28 Sally, Jane, Composite and Iterator Sally keeps her shoes in a big trunk. Sometimes the shoes are in a shoebox in the trunk and sometimes not. Sometimes the big trunk has several smaller trunks which can have shoe boxes or shoes in them Jane has to iterate through the shoe collection, returning individual pairs of shoes, when Sally is picking out her ensemble for the day 5/9/2015Copyright W. Howden28

29 DS and Composite Extend the complexity of the information structures that are matched by the dating system. component = composite or primitive composite = [data-name: list of components] primitive = [data-name: choice] choice is string, taken from a specified set of alternatives 5/9/2015Copyright W. Howden29

30 DS Sample Complex Data (Italics = primitive strings) [member-properties: physical, spiritual, interests, favorite activity] [physical: [gender, F] [age, 33] [spiritual: [religion, catholic], [involvement, biweekly] [interests: [intellectual], [Sport, hockey] [favoriteActivity] [intellectual: [favoritePlay, Dr. Faustis], [favoriteActivity, writing UMLDiagrams] 5/9/2015Copyright W. Howden30

31 DS Complex Data Class Structure 5/9/2015Copyright W. Howden31

32 Sample Object Structure 5/9/2015Copyright W. Howden32

33 Iterators and the Composite Pattern Goals: – Define a CompositeIterator, that we can use to return the next primitive object in a composite structure Possible approaches? it uses a stack of iterators its constructor is CompositeIterator(Iterator, iterator) – we pass it the iterator for the compound object at the top of the structure we want to traverse – it places it on the top of the stack.... 5/9/2015Copyright W. Howden33

Download ppt "Template, Command, Iterator and Composite CSE 111 5/9/20151Copyright W. Howden."

Similar presentations

Ads by Google