Professional Documents
Culture Documents
Section 0:
1. Describe Behavioural Design Patterns according to Gamma et al.?
Answer Guide:
Answer Guide:
Behavioral object patterns use object composition rather than inheritance. Some
describe how a group of peer objects cooperate to perform a task that no single object
can carry out by itself.
Lab exercise:
Create the following classes
DvdReleaseByCategory
import java.util.ArrayList;
import java.util.ListIterator;
DvdSubscriber
class TestDvdObserver {
public static void main(String[] args) {
DvdReleaseByCategory btvs =
new DvdReleaseByCategory("Buffy the Vampire Slayer");
DvdReleaseByCategory simpsons =
new DvdReleaseByCategory("The Simpsons");
DvdReleaseByCategory sopranos =
new DvdReleaseByCategory("The Sopranos");
DvdReleaseByCategory xfiles =
new DvdReleaseByCategory("The X-Files");
btvs.addSubscriber(rgiles);
btvs.addSubscriber(wrosen);
simpsons.addSubscriber(msimps);
sopranos.addSubscriber(jsopra);
xfiles.addSubscriber(smulde);
xfiles.addSubscriber(wrosen);
DvdRelease btvsS2 =
new DvdRelease("DVDFOXBTVSS20",
"Buffy The Vampire Slayer Season 2",
2002, 06, 11);
DvdRelease simpS2 =
new DvdRelease("DVDFOXSIMPSO2",
"The Simpsons Season 2",
2002, 07, 9);
DvdRelease soprS2 =
new DvdRelease("DVDHBOSOPRAS2",
"The Sopranos Season 2",
2001, 11, 6);
DvdRelease xfilS5 =
new DvdRelease("DVDFOXXFILES5",
"The X-Files Season 5",
2002, 04, 1);
btvs.newDvdRelease(btvsS2);
simpsons.newDvdRelease(simpS2);
sopranos.newDvdRelease(soprS2);
xfiles.newDvdRelease(xfilS5);
xfiles.removeSubscriber(wrosen);
xfilS5.updateDvdReleaseDate(2002, 5, 14);
xfiles.updateDvd(xfilS5);
}
}
Tut6-A3. Show how the names in the Observer DP map to DvdRelease program.
3. Which class is the observer, and which class is being observed?
Answer guide:
DvdReleaseByCategory – the class which is observed
DvdSubscriber – the observer
4. Draw the UML class diagrams for all the classes created.
1. Draw a UML class diagram that represents the Visitor design pattern
Answer Guide:
| <- - -- -- -- -- -- - |
| |
| |
| |
| |
| |
| b. (local variable type) dependency |
| |
|- - - -- -- -- -- -- -- -- -- --> - -- -- -- -- -- -- -- -- -- -- |
Answer Guide:
many distinct and unrelated operations need to be performed on objects in an object structure, and you
want to avoid "polluting" their classes with these operations. Visitor lets you keep related operations
together by defining them in one class. When the object structure is shared by many applications, use
Visitor to put operations in just those applications that need them.
l
the classes defining the object structure rarely change, but you often want to define new operations
over the structure. Changing the object structure classes requires redefining the interface to all
visitors, which is potentially costly. If the object structure classes change often, then it's probably
better to define the operations in those classes.
Lab Exercise
Book Info
DVD Info
Game Info
class TestTitleVisitor {
public static void main(String[] args) {
AbstractTitleInfo bladeRunner =
new DvdInfo("Blade Runner", "Harrison Ford", '1');
AbstractTitleInfo electricSheep =
new BookInfo("Do Androids Dream of Electric Sheep?",
"Phillip K. Dick");
AbstractTitleInfo sheepRaider =
new GameInfo("Sheep Raider");
TitleBlurbVisitor titleLongBlurbVisitor =
new TitleLongBlurbVisitor();
System.out.println("Long Blurbs:");
bladeRunner.accept(titleLongBlurbVisitor);
System.out.println("Testing bladeRunner " +
titleLongBlurbVisitor.getTitleBlurb());
electricSheep.accept(titleLongBlurbVisitor);
System.out.println("Testing electricSheep " +
titleLongBlurbVisitor.getTitleBlurb());
sheepRaider.accept(titleLongBlurbVisitor);
System.out.println("Testing sheepRaider " +
titleLongBlurbVisitor.getTitleBlurb());
TitleBlurbVisitor titleShortBlurbVisitor =
new TitleShortBlurbVisitor();
System.out.println("Short Blurbs:");
bladeRunner.accept(titleShortBlurbVisitor);
System.out.println("Testing bladeRunner " +
titleShortBlurbVisitor.getTitleBlurb());
electricSheep.accept(titleShortBlurbVisitor);
System.out.println("Testing electricSheep " +
titleShortBlurbVisitor.getTitleBlurb());
sheepRaider.accept(titleShortBlurbVisitor);
System.out.println("Testing sheepRaider " +
titleShortBlurbVisitor.getTitleBlurb());
}
}
Tut6-B1. Show how the names in the Visitor DP map to the Blurb & TitleInfo program. [1m]
Answer Guide:
Tut6-B2. In applying Visitor design pattern (DP), draw the Class Diagram for Tut6-Section B, which
consists of AbstractTitleInfo with its accept(TitleBlurbVisitor) method, BookInfo with
setAuthor(String), setISBN(String) methods; DVDInfo & GameInfo, TestTitleVisitor,
{TitleLongBlurbVisitor, and TitleShortBlurbVisitor} with their visit(BookInfo), visit(DvdInfo),
visit(GameInfo) methods. [1m]
lwhen common behavior among subclasses should be factored and localized in a common
class to avoid code duplication. You first identify the differences in the existing code and
then separate the differences into new operations. Finally, you replace the differing code with
a template method that calls one of these new operations.
lto control subclasses extensions. You can define a template method that calls "hook"
operations (see Consequences) at specific points, thereby permitting extensions only at those
points.
2. What is the structure of Template Method?
Answer Guide
lspecify, queue, and execute requests at different times. A Command object can have a lifetime
independent of the original request. If the receiver of a request can be represented in an address
space independent way, then you can transfer a command object for the request to a different
process and fulfill the request there.
lsupport undo. The Command's Execute operation can store state for reversing its effects in the
command itself. The Command interface must have an added Unexecute operation that reverses
the effects of a previous call to Execute. Executed commands are stored in a history list.
Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling
Unexecute and Execute, respectively.
lsupport logging changes so that they can be reapplied in case of a system crash. By augmenting
the Command interface with load and store operations, you can keep a persistent log of changes.
Recovering from a crash involves reloading logged commands from disk and reexecuting them
with the Execute operation.
lstructure a system around high-level operations built on primitives operations. Such a structure is
common in information systems that support transactions. A transaction encapsulates a set of
changes to data. The Command pattern offers a way to model transactions. Commands have a
common interface, letting you invoke all transactions the same way. The pattern also makes it
easy to extend the system with new transactions.
Lab exercise
DvDName
CommandAbstract
DvdCommandNameStarsOn
DvdCommandNameStarsOff
1. Create the main file to test the classes, with the following results
as first instantiated
DVD: Jay and Silent Bob Strike Back
DVD: Sponge Bob Squarepants - Nautical Nonsense and Sponge Buddies
stars on
DVD: Jay*and*Silent*Bob*Strike*Back
DVD: Sponge*Bob*Squarepants*-*Nautical*Nonsense*and*Sponge*Buddies
Answer guide
class TestCommand {
public static void main(String[] args) {
DvdName jayAndBob =
new DvdName("Jay and Silent Bob Strike Back");
DvdName spongeBob =
new DvdName("Sponge Bob Squarepants - " +
"Nautical Nonsense and Sponge Buddies");
System.out.println("as first instantiated");
System.out.println(jayAndBob.toString());
System.out.println(spongeBob.toString());
CommandAbstract bobStarsOn =
new DvdCommandNameStarsOn(jayAndBob);
CommandAbstract bobStarsOff =
new DvdCommandNameStarsOff(jayAndBob);
CommandAbstract spongeStarsOn =
new DvdCommandNameStarsOn(spongeBob);
CommandAbstract spongeStarsOff =
new DvdCommandNameStarsOff(spongeBob);
bobStarsOn.execute();
spongeStarsOn.execute();
System.out.println(" ");
System.out.println("stars on");
System.out.println(jayAndBob.toString());
System.out.println(spongeBob.toString());
spongeStarsOff.execute();
System.out.println(" ");
System.out.println("sponge stars off");
System.out.println(jayAndBob.toString());
System.out.println(spongeBob.toString());
}
}
Tut6-D2. Show how the names in the Command DP map to DvdStarsOnOff program.