uml & strategy pattern - university of california, san...

16
Discussion 11/2/2016 UML & Strategy Pattern

Upload: others

Post on 30-Apr-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

Discussion 11/2/2016

UML & Strategy Pattern

Page 2: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

UML – What and Why?

• “Universal Modeling Language”

• Emerged in the mid 90’s to standardize software design notation

• Language-independent visual representation of object-based software design

• Methods representing both structural and behavioral design of software*

*These slides only cover structural UML.

Page 3: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

UML Notation – Classes

Type

Name

Access Level

Can be followed by “ : <Type> “ to denote return type. This method is void.

Page 4: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

UML Notation – Inheritance (Is-A)

Page 5: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

Example (1.1) – Simple Animals

Animals as OBJECTS (Instantiated Classes)

Page 6: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

UML Notation – Interfaces

* Key distinction between abstract classes and interfaces:abstract classes are realized via inheritance, interfaces are realized via implementation. Interfaces should be used used to define behaviors which classes implement.

Page 7: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

Example (1.2) – Simple Animals

Notice dashed line for implementation

Page 8: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

UML Notation – Aggregation, Composition, and Association (Has-A)

Aggregation (ClassAcontains ClassB)

Composition (ClassAcontains ClassB)

Association (Bi-Directional)

Multiplicity* Any number1 Exactly 1n Exactly n

0..1 Zero OR 11..* One or moren..m Any value n through m

Page 9: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

Example (2.1) – Airports & Universities

Here a “route” is a path flown by a passenger (ex: San Diego to New York), where a route is an aggregation of direct flights. You could take a direct flight or have a connecting flight in Chicago. Each flight only has one plane for a specific gate and time, but a plane may have multiple flights.

A university is composed of departments, which will cease to exist if the university is closed. In the previous example, a flight will still exist even if it is not encapsulated by a route.

Page 10: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

Strategy Pattern

Strategy Goals

(1) Define a family of algorithms

(2) Encapsulate each algorithm

(3) Make algorithms interchangeable within family

Page 11: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

Strategy – In UML

Page 12: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

Example (3.1) Strategy Situations: Pitcher

Page 13: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

Example (3.2) Strategy Situations: Audio Playback

Page 14: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

In Java: Strategy & Concrete Strategies

// encapsulates family of algorithms

public interface Strategy {

public void algorithm();

}

// a strategy for implementing an algorithm

public class ConcreteStrategyA implements Strategy {

// implementation of the algorithm

public void algorithm() {

// do stuff one way

System.out.println("using strategy A");

return;

}

}

// a second strategy for implementing an algorithm

public class ConcreteStrategyB implements Strategy {

// implementation of the algorithm

public void algorithm() {

// do stuff another way

System.out.println("using strategy B");

return;

}

}

Page 15: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

In Java: Context

public class Context {

// interface to set strategy

public void setStrategy(Strategy strategy) {

s = strategy;

return;

}

// execute algorithm

public void execute() {

s.algorithm();

return;

}

// NOTE: set strategy before using

private Strategy s;

}

Page 16: UML & Strategy Pattern - University of California, San Diegocseweb.ucsd.edu/.../applications/ln/UMLStrategyPatternDiscussion.pdf · Strategy Pattern Strategy Goals (1) Define a family

In Java: Client Code

public class Test {

// demo the strategy

public static void main(String[] args) {

Context c = new Context();

// set and use strategy A

c.setStrategy(new ConcreteStrategyA());

c.execute();

// set and use strategy B

c.setStrategy(new ConcreteStrategyB());

c.execute();

// ... and let's go back to strategy A

c.setStrategy(new ConcreteStrategyA());

c.execute();

return;

}