New UML & Strategy Pattern · 2016. 11. 8. · object-based software design •Methods representing...
Transcript of New UML & Strategy Pattern · 2016. 11. 8. · object-based software design •Methods representing...
Discussion 11/2/2016
UML & Strategy Pattern
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.
UML Notation – Classes
Type
Name
Access Level
Can be followed by “ : <Type> “ to denote return type. This method is void.
UML Notation – Inheritance (Is-A)
Example (1.1) – Simple Animals
Animals as OBJECTS (Instantiated Classes)
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.
Example (1.2) – Simple Animals
Notice dashed line for implementation
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
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.
Strategy Pattern
Strategy Goals
(1) Define a family of algorithms
(2) Encapsulate each algorithm
(3) Make algorithms interchangeable within family
Strategy – In UML
Example (3.1) Strategy Situations: Pitcher
Example (3.2) Strategy Situations: Audio Playback
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;
}
}
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;
}
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;
}