8
CS 4321 - Video Lecture Expectations Video P3L3: Design Patterns (23 minutes) 1. Define the term design pattern. 2. The authors of the seminal book on design patterns, “Design Patterns: Elements of Reusable Object Oriented Software,” are referred to as the _______________________. (I know this probably seems arbitrary that I am asking you to remember this. However, as the author of the video says, this is THE book on design patterns. I have spent hundreds of hours studying it. Anyone who knows anything about design patterns knows the answer to this question.) 3. Patterns can be classified under a number of categories. _____________ patterns support object creation, _____________ patterns help with composing objects, putting objects together, and _________________ patterns help with how objects interact. 4. Four essential elements of a design patter are its name, its __________________ which is the goal of the pattern, its _____________________ which is a list of the situations in which the pattern is applicable, and its _______________ which is the static model that describes the classes as well as relationships and responsibilities among the classes. 5. The _________________ pattern allows for the creation of object without specifying their class. 6. Draw a class diagram showing the structure of the factory method pattern. 7. The example in clip 6 may not seem to fit the structure defined earlier for the factory method. In fact it doesn’t, there is not interface for creating objects in the example. There is simply a concrete creator that supplies a factory method. The structure of a pattern is useful many times, but what is more important is to be guided by the spirit of the pattern: its intent (allows for creating objects without specifying their class) and applicability (a class cannot anticipate the type of object it must create). 8. The ____________ pattern encapsulates each algorithm in a family of related behaviors in a class and makes them interchangeable. 9. Draw a class diagram showing the structure of the strategy pattern. 1

Valdosta State University€¦ · Web viewThe factory method pattern is classified as a _____ pattern while the strategy pattern is classified as a _____ pattern. Draw a class diagram

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Valdosta State University€¦ · Web viewThe factory method pattern is classified as a _____ pattern while the strategy pattern is classified as a _____ pattern. Draw a class diagram

CS 4321 - Video Lecture Expectations

Video P3L3: Design Patterns (23 minutes)

1. Define the term design pattern.

2. The authors of the seminal book on design patterns, “Design Patterns: Elements of Reusable Object Oriented Software,” are referred to as the _______________________. (I know this probably seems arbitrary that I am asking you to remember this. However, as the author of the video says, this is THE book on design patterns. I have spent hundreds of hours studying it. Anyone who knows anything about design patterns knows the answer to this question.)

3. Patterns can be classified under a number of categories. _____________ patterns support object creation, _____________ patterns help with composing objects, putting objects together, and _________________ patterns help with how objects interact.

4. Four essential elements of a design patter are its name, its __________________ which is the goal of the pattern, its _____________________ which is a list of the situations in which the pattern is applicable, and its _______________ which is the static model that describes the classes as well as relationships and responsibilities among the classes.

5. The _________________ pattern allows for the creation of object without specifying their class.

6. Draw a class diagram showing the structure of the factory method pattern.

7. The example in clip 6 may not seem to fit the structure defined earlier for the factory method. In fact it doesn’t, there is not interface for creating objects in the example. There is simply a concrete creator that supplies a factory method. The structure of a pattern is useful many times, but what is more important is to be guided by the spirit of the pattern: its intent (allows for creating objects without specifying their class) and applicability (a class cannot anticipate the type of object it must create).

8. The ____________ pattern encapsulates each algorithm in a family of related behaviors in a class and makes them interchangeable.

9. Draw a class diagram showing the structure of the strategy pattern.

10. The factory method pattern is classified as a __________________ pattern while the strategy pattern is classified as a ____________________ pattern.

11. Draw a class diagram for the strategy example presented in the video.

12. The __________________ pattern is a way of separating the class(es) that define an object’s structure from the algorithms that operate on those structures. This pattern supports the ability to add new algorithms without modifying the structures.

13. The _______________ pattern supports the ability to add new behaviors to an existing class without modifying the original class.

1

Page 2: Valdosta State University€¦ · Web viewThe factory method pattern is classified as a _____ pattern while the strategy pattern is classified as a _____ pattern. Draw a class diagram

14. The _______________ pattern allows you to access the elements of a collection without knowing the underlying representation of those elements.

15. The ______________ pattern is useful when you have an object of interest and a set of other objects that are interested in the changes that might occur in this first object. Thus, every time that there is a change in the first object, these other objects will be automatically notified.

16. The _____________ pattern controls access to an object.

Some additional notes on the strategy and factory method patterns follow.

Strategy Pattern

1. Intent: Define a family of algorithms, encapsulate each one, and make them interchangeable. By associating the client with a strategy/algorithm we allow different objects to use different algorithms and to swap algorithms at run-time.

2. Example

// Contextpublic class Person {

Dog dog;ExerciseStrategy exStrat;

public Person(Dog dog) {this.dog = dog;exStrat = new WalkDog();

}

public void setExerciseStrategy( ExerciseStrategy exStrat) {

this.exStrat = exStrat;}

public void exerciseDog () {exStrat.exercise(dog);

}}

// UseagePerson p = new Person(new Dog("Juno"));p.exerciseDog();p.setExerciseStrategy(new RunDog());p.exerciseDog();p.setExerciseStrategy(new PlayCatchWithDog());

// Strategy interfacepublic interface ExerciseStrategy {

public void exercise(Dog dog);}

// Concrete Strategypublic class WalkDog implements ExerciseStrategy {

@Overridepublic void exercise(Dog dog) {

System.out.println( dog.getName() + " is walking");

}

}// Concrete Strategypublic class RunDog implements ExerciseStrategy {

@Overridepublic void exercise(Dog dog) {

System.out.println( dog.getName() + " is running");

}}

2

Page 3: Valdosta State University€¦ · Web viewThe factory method pattern is classified as a _____ pattern while the strategy pattern is classified as a _____ pattern. Draw a class diagram

p.exerciseDog();

3. The strategy pattern is found extensively in the Java API. For example, the TreeSet constructor can take a Comparator which defines how elements are to be ordered. Notice that the TreeSet is composed with the Comparator interface. Then, the developer can write as many concrete comparators as needed. TreeMap, and Collections.sort are similar. Note that TreeSet does not allow comparators to be swapped at run-time as the definition of the pattern suggests. However, the designers of TreeSet decided not to allow this, though it could easily have provided this feature.

4. In Java Swing, the JComponent class utilizes the strategy pattern by composing itself with the Border interface and also provides a way to change borders, setBorder. The Container class utilizes strategy by composing itself with the LayoutManager interface. JComponent utilizes the strategy pattern in a number of other places including: ActionMap (locates Actions when a key is pressed) and JPopupMenu (not shown in figure below). In JavaFX, the strategy pattern is similarly found in the relationship between a Stage and a Scene and a Scene and a Pane.

3

Page 4: Valdosta State University€¦ · Web viewThe factory method pattern is classified as a _____ pattern while the strategy pattern is classified as a _____ pattern. Draw a class diagram

5. Homework:

a. Modify the Person/Dog example above so that a person can have multiple exercise strategies for the dog, and when exericseDog is called, they are all executed.

b. Draw a class diagram for this situation that utilizes a design pattern: A graph can be drawn as bar graph, line graph, pie chart, etc. Also, name the pattern.

c. Draw a class diagram for this situation that utilizes a design pattern: Online shopping – A shopping website suggests products in a sidebar, perhaps depending on the type of user: anonymous or registered. Suggestions could be: random, best sellers, clearance, sales items, new products, etc.

Factory Method Pattern

1. Technically, the example presented in the video is a simple factory which is not really a design pattern, but more just something a designer should do based on design principles such as cohesion and encapsulation. The example can basically be summarized with this generalized class diagram:

Here, the createProduct is a static method (hence underlined). However, in everyday use, people tend to call any class that creates an object a use of the factory pattern. Technically, there is not “factory pattern”. There are two official factory patterns: factory method pattern and abstract factory pattern (not considered).

None-the-less, the concept of the simple factory is a useful idiom. It basically says that anytime object creation involves more than a couple of lines of code, it should be encapsulated.

2. The Factory Method pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. The intent of the Factory Method pattern is to relieve a client from having to know which class to instantiate.

4

Page 5: Valdosta State University€¦ · Web viewThe factory method pattern is classified as a _____ pattern while the strategy pattern is classified as a _____ pattern. Draw a class diagram

Many times with this pattern, as shown above, the Creator class has a public concrete method that Client objects use (e.g. anOperation). As part of fulfilling its responsibilities, it must create an object whose type is not known at compile time. Thus, it calls the abstract factory method (e.g. createProduct) deferring object creation to the implementing subclass (e.g ConcreteCreator). Thus, the subclass can be configured with the Client at runtime either by passing the ConcreteCreator to the Client’s constructor, or by having the Client have a setCreator(Creator c) method.

3. Example: Clients use the orderPizza method. As shown on the left below, this method first creates a pizza by calling the factory method, createPizza and then prepares, bakes, etc. the pizza before returning it.

5

Page 6: Valdosta State University€¦ · Web viewThe factory method pattern is classified as a _____ pattern while the strategy pattern is classified as a _____ pattern. Draw a class diagram

6. The iterator method specified in the Collection interface is a Factory Method. The Collection interface specifies that any class that wants to be a Collection must specify an iterator method that returns an instance that implements the Iterator interface.

See the code in ArrayList class, lines 773, 780:http://www.docjar.com/html/api/java/util/ArrayList.java.html

7. Homework:

Draw a class diagram for this situation that utilizes a design pattern: Consider a maze game where a player has to navigate through a maze in one of several modes. There can be a beginner maze, psychedelic maze, etc. The maze game should have a play game method that instantiates a maze and allows the person to play. State the pattern name.

6