Sie sind auf Seite 1von 13

CMSC 5633

Patterns

Assignment 6 – Design Patterns


(25 pts)

Lesson 6 Assignments (25 pts)


1. Build generic Class Diagram for each pattern in lesson (as presented in class)

Template Pattern:

State:

Strategy:
Command:

2. Build generic Interaction Diagram for each pattern in lesson (as presented in
class)

Template Pattern:
State:

Strategy:
Command:

3. Find an example (not used in the class or class books) of the use of one of the
patterns from this lesson and write a 2-3 page description of the use of the
pattern in the example.
• Describe the pattern, describe the problem being solved, describe how
using the pattern solves the problem and describe the benefits of
using the pattern.
• Illustrate with Object Models.
• Use your own words to describe the example not just the words from
the reference.
• Use appropriate references in IEEE style. Including providing a
reference to where you found the example.
Select one from this list of patterns.
• Template
• State
• Strategy
• Command
Strategy Pattern:

Strategy design pattern is one of very commonly used behavioral design

pattern. This design pattern helps us build a family of different but related

algorithms, encapsulate each one of them making them interchangeable

based on the context of their use or conditions. Strategy pattern is probably

the best software design pattern solution for situation when we need to

implement numbers of different algorithms based on different

circumstances.

Here is a class diagram of a generic example of strategy pattern.

We can eliminate many conditional statements using strategy design pattern

which lets us change conditional statements to polymorphism. Strategic

approach can be used as a good alternative to sub classing. The client selects
the actual implementation of the algorithm from the group of algorithms at

run time based on the context.

Here we have a generic example of interaction diagram for Strategy Pattern.

Strategy Pattern is sometimes known as Policy Pattern. The client pass the

appropriate algorithm from the pool of multiple algorithms we defined as a

parameter. Strategy Pattern is similar to State Pattern. One of the key

differences between them is that state exists in the context as an instance

variable whereas there exists strategy in the context that is passed as

argument to the method and the context object does not have any variable to
store it as in case of State Pattern 1. Strategy Pattern captures the abstraction

in an interface and bury the implementation details in derived classes2.

The class diagram below illustrates the example of strategic pattern to

implement a shopping cart application with two strategies for payment

options- CreditCardStrategy and PayPalStrategy which are subclasses which

will implement PaymentStrategy interface by implementing the pay()

operation. The ShoppingCart class calls the pay() method with

paymentMethod of type PaymentStrategy as parameter to select appropriate

Payment Strategy between PaypalStrategy and CreditCardStrategy.

1 P. Kumar, "Strategy Design Pattern in Java - Example Tutorial - JournalDev", JournalDev, 2018.
[Online]. Available: https://www.journaldev.com/1754/strategy-design-pattern-in-java-example-
tutorial. [Accessed: 05- Dec- 2018].
2 "Design Patterns and Refactoring", Sourcemaking.com, 2018. [Online]. Available:

https://sourcemaking.com/design_patterns/strategy. [Accessed: 05- Dec- 2018].


3

The simple example explains clearly how we can implement strategic

approach of design pattern to develop software application. Application of

this design pattern help us avoid having to define conditional statements to

select appropriate payment options and ease the whole process maintaining

good design principles of software engineering.

3P. Kumar, "Strategy Design Pattern in Java – Example Tutorial", journaldev.com, 2018. [Online].
Available: https://cdn.journaldev.com/wp-content/uploads/2013/07/Strategy-Pattern.png.
[Accessed: 05- Dec- 2018].
4. Create a list of all 23 patterns and find an example in Java (standard Java
implementation) of a use of each pattern and explain how it works.

1. Adapter: Adapter converts to an interface to another the client expects to use


Used in java: javax.xml.xpath.XPathFactory#newInstance()

2. Facade: Facade pattern simplifies an interface by proving a interface to a


larger set of interfaces in a subsystem (body of code, such as a class library).
Used in java: javax.servlet.http.HttpServletRequestWrapper
3. Composite: In software engineering, the composite pattern is a partitioning
design pattern. The intent of a composite is to "compose" objects into tree
structures to represent part-whole hierarchies.
Used in java: java.awt.Container#add(Component)
4. Bridge: The bridge pattern is a design pattern used to "decouple an
abstraction from its implementation so that the two can vary independently".
Used in java: new LinkedHashMap(LinkedHashSet<K>, List<V>)
5. Singleton: In software engineering, the singleton pattern is a software
design pattern that restricts the instantiation of a class to one object. This is
useful when exactly one object is needed to coordinate actions across the
system.
Used in java: java.lang.Runtime#getRuntime()

6. Observer: The observer pattern is used when change in one object needs to
reflect in other objects and maintain loose coupling.
Used in java: javax.servlet.http.HttpSessionAttributeListener

7. Mediator: When certain sets of activities are to be performed or coordinated


between objects through a mediating object, Mediator Pattern is used.
Used in java: java.util.concurrent.Executor#execute()

8. Proxy: When we need to hide details of the real implementation by providing


different representation of the address space of the object, Proxy Pattern is
applied.

Used in java: java.lang.reflect.Proxy

9. Chain of responsibility: Chain of responsibility pattern creates a chain of


receiver objects and sets the request handler dynamically without specifying
the receiver explicitly as there are multiple objects that might handle the
request.
Used in java: java.util.logging.Logger#log()

10. Flyweight: Flyweight Pattern help reduce the number of objects and decrease
memory footprint and increase performance. New objects are created only
when needed.
Used in java: java.lang.Integer#valueOf(int)
11. Builder: The builder pattern is an object creation software design pattern.
Unlike the abstract factory pattern and the factory method pattern whose
intention is to enable polymorphism, the intention of the builder pattern is to
find a solution to the telescoping constructor anti-pattern.
Used in jave: java.nio.ByteBuffer#put()
12. Factory method: Define an interface for creating an object, but let subclasses
decide which class to instantiate. The Factory method lets a class defer
instantiation it uses to subclasses.
Used in java: java.nio.charset.Charset#forName()

13. Abstract factory method: Abstract factory returns factory of method which in
turn creates another abstract class or interface.
Used in Java: javax.xml.xpath.XPathFactory#newInstance()

14. Prototype: The prototype pattern is a creational design pattern in software


development. It is used when the type of objects to create is determined by a
prototypical instance, which is cloned to produce new objects. ... avoid
subclasses of an object creator in the client application, like the abstract
factory pattern does.
Used in java: java.lang.Object#clone()
15. Memento: The memento pattern is a software design pattern that provides
the ability to restore an object to its previous state (undo via rollback).
The memento pattern is implemented with three objects: the originator, a
caretaker and a memento.
Used in java: java.io.Serializable
16. Template: The template method pattern is a behavioral design pattern that
defines the program skeleton of an algorithm in an operation, deferring some
steps to subclasses. It lets one redefine certain steps of an algorithm without
changing the algorithm's structure.
Used in java: javax.servlet.http.HttpServlet
17. State: The state pattern is a behavioral software design pattern that
implements a state machine in an object-oriented way. ... This pattern is used
in computer programming to encapsulate varying behavior for the same
object based on its internal state.
Used in java: javax.faces.lifecycle.LifeCycle#execute()
18. Strategy: In computer programming, the strategy pattern (also known as the
policy pattern) is a behavioral software design pattern that enables an
algorithm's behavior to be selected at runtime. The strategy pattern defines a
family of algorithms, ... makes the algorithms interchangeable within that
family.
Used in java: java.util.Comparator#compare()
19. Command: Command pattern is a behavioral design pattern in which an
object is used to encapsulate all information needed to perform an action or
trigger an event at a later time based on the command.
Used in java: java.lang.Runnable
20. Interpreter: Interpreter pattern specifies how to evaluate sentences in a
language. The basic idea is to have a class for each symbol (terminal or
nonterminal) in a specialized computer language.
Used in java: javax.el.ELResolver
21. Decorator: Decorator pattern (also known as Wrapper, an alternative naming
shared with the Adapter pattern) is a design pattern that allows behavior to
be added to an individual object, either statically or dynamically, without
affecting the behavior of other objects from the same class.
Used in java: javax.servlet.http.HttpServletRequestWrapper
22. Iterator: In object-oriented programming, the iterator pattern is a
design pattern in which an iterator is used to traverse a container and access
the container's elements.
Used in java: java.util.Iterator
23. Visitor: Visitor design pattern is a way of separating an algorithm from an
object structure on which it operates. A practical result of this separation is
the ability to add new operations to extant object structures without
modifying the structures.
Used in java: java.nio.file.FileVisitor

5. Put all the above in one Word document and upload.

Lesson 7 Preparation
6. Read Chapter 21-22-23-24 Java Design Patterns (will be on quiz)
7. Read Sections in Design Patterns book (will be on quiz)
• Interpreter p. 243
• Decorator p. 175
• Iterator p. 257
• Visitor p. 331

Das könnte Ihnen auch gefallen