mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
davhojt
1cf114eb85
|
1 year ago | |
---|---|---|
.. | ||
README.md | 1 year ago |
README.md
Strategy
Instructions
Now let's implement the Visitor Design Pattern
classDiagram
class Context {
-OperationStrategy operationStrategy
+Context()
+changeStrategy(OperationStrategy operationStrategy)
+execute(int a, int b) int
}
class OperationStrategy {
+execute(int a, int b)* int
}
class AddStrategy {
+execute(int a, int b) int
}
class ConcatStrategy {
+execute(int a, int b) int
}
class MaxStrategy {
+execute(int a, int b) int
}
Context o-- OperationStrategy
OperationStrategy <|.. AddStrategy
OperationStrategy <|.. ConcatStrategy
OperationStrategy <|.. MaxStrategy
Here is the matching class diagram. Create the matching classes in the matching files.
Here is the description :
- For
Context
:operationStrategy
field should be initialized with aAddStrategy
changeStrategy
should update theoperationStrategy
fieldexecute
should return the result ofoperationStrategy.execute
- For
AddStrategy
:execute
should return the sum of the two integers (e.g. a = 2, b = 4, then should return 6)
- For
ConcatStrategy
:execute
should return the concatenation of the two integers (e.g. a = 2, b = 4, then should return 24)
- For
MaxStrategy
:execute
should return the max of the two integers (e.g. a = 2, b = 4, then should return 4)
Usage
Here is a possible ExerciseRunner.java to test your function :
public class ExerciseRunner {
public static void main(String[] args) {
Context context = new Context();
System.out.println(context.execute(23, 43));
context.changeStrategy(new MaxStrategy());
System.out.println(context.execute(23, 43));
context.changeStrategy(new ConcatStrategy());
System.out.println(context.execute(23, 43));
}
}
and its output :
$ javac *.java -d build
$ java -cp build ExerciseRunner
66
43
2343
$