PatternStrategy: Initial commit

This commit is contained in:
2022-05-08 00:10:38 +02:00
parent 87e8cd1e09
commit f21dfa2e27
8 changed files with 70 additions and 0 deletions

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>PatternStrategy</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -0,0 +1,6 @@
public class DoAdd implements Strategy{
@Override
public int doOperation(int num1, int num2) {
return num1 + num2;
}
}

View File

@@ -0,0 +1,6 @@
public class DoMultiply implements Strategy{
@Override
public int doOperation(int num1, int num2) {
return num1 * num2;
}
}

View File

@@ -0,0 +1,6 @@
public class DoSubtract implements Strategy{
@Override
public int doOperation(int num1, int num2) {
return num1 - num2;
}
}

View File

@@ -0,0 +1,13 @@
public class PatternStrategy {
public static void main(String[] args) {
SelectToDo toDo1 = new SelectToDo(new DoAdd());
System.out.println("3 + 2 = " + toDo1.executeStrategy(3, 2));
SelectToDo toDo2 = new SelectToDo(new DoSubtract());
System.out.println("3 - 2 = " + toDo2.executeStrategy(3, 2));
SelectToDo toDo3 = new SelectToDo(new DoMultiply());
System.out.println("3 * 2 = " + toDo3.executeStrategy(3, 2));
}
}

View File

@@ -0,0 +1,12 @@
public class SelectToDo {
private Strategy strategy;
public SelectToDo(Strategy strategy){
this.strategy = strategy;
}
public int executeStrategy(int num1, int num2){
return strategy.doOperation(num1, num2);
}
}

View File

@@ -0,0 +1,4 @@
public interface Strategy {
public int doOperation(int num1, int num2);
}