Skip to content

Commit 80611c4

Browse files
committed
Strategy Design Pattern - Calculator application
1 parent fefedd4 commit 80611c4

6 files changed

Lines changed: 129 additions & 13 deletions

File tree

patternBonus/src/com/premaseem/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.premaseem.client;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
import java.util.Scanner;
10+
11+
import com.premaseem.strategy.AirthematicOperationStrategy;
12+
import com.premaseem.strategy.impl.AdditionStrategy;
13+
import com.premaseem.strategy.impl.MultiplicationStragegy;
14+
import com.premaseem.strategy.impl.SubstractionStrategy;
15+
16+
public class ClientMain {
17+
18+
public static void main (String[] args) {
19+
System.out.println("This is Calculator application designed using Strategy Pattern");
20+
System.out.println("Client has several Strategy which can be consumed at run time ");
21+
System.out.println("Please input 2 number for Strategic mathematical operation... ");
22+
Scanner scan = new Scanner(System.in);
23+
System.out.println("Please enter first number ");
24+
Integer num1 = scan.nextInt();
25+
System.out.println("Please enter second number");
26+
Integer num2 = scan.nextInt();
27+
System.out.println("ADD");
28+
System.out.println("SUB");
29+
System.out.println("MUL");
30+
31+
// Initially Strategy is null, it would be assigned at run time
32+
AirthematicOperationStrategy strategy = null;
33+
System.out.println("Please enter OPERATION CODE, on the run time strategy would be picked based on your choice of operation");
34+
String operationCode = scan.next();
35+
36+
// Family of different algorithm is getting initialised here
37+
if (operationCode.contains("ADD")) {
38+
strategy = new AdditionStrategy();
39+
}
40+
41+
if (operationCode.contains("SUB")) {
42+
strategy = new SubstractionStrategy();
43+
}
44+
45+
if (operationCode.contains("MUL")) {
46+
strategy = new MultiplicationStragegy();
47+
}
48+
49+
50+
System.out.printf("You picked strategy as %s", operationCode);
51+
System.out.println();
52+
if (strategy != null) {
53+
// Operation remains same but implementation algo changes dynamically
54+
System.out.printf("Final result is %d", strategy.performOperation(num1, num2));
55+
} else {
56+
System.out.println("Invalid operation picked :-( ");
57+
}
58+
System.out.println();
59+
System.out.println(" Thanks by Prem Aseem ");
60+
61+
}
62+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.premaseem.strategy;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
public interface AirthematicOperationStrategy {
10+
11+
Integer performOperation (Integer num1, Integer num2);
12+
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.premaseem.strategy.impl;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
import com.premaseem.strategy.AirthematicOperationStrategy;
10+
11+
public class AdditionStrategy implements AirthematicOperationStrategy {
12+
13+
@Override
14+
public Integer performOperation(Integer num1, Integer num2) {
15+
return num1 + num2;
16+
}
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.premaseem.strategy.impl;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
import com.premaseem.strategy.AirthematicOperationStrategy;
10+
11+
public class MultiplicationStragegy implements AirthematicOperationStrategy {
12+
13+
@Override
14+
public Integer performOperation(Integer num1, Integer num2) {
15+
return num1 * num2;
16+
}
17+
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.premaseem.strategy.impl;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
import com.premaseem.strategy.AirthematicOperationStrategy;
10+
11+
public class SubstractionStrategy implements AirthematicOperationStrategy {
12+
13+
@Override
14+
public Integer performOperation(Integer num1, Integer num2) {
15+
return num1 - num2;
16+
}
17+
18+
}

0 commit comments

Comments
 (0)