Skip to content

Commit b5b57b6

Browse files
committed
Added Strategy
1 parent df7ef6a commit b5b57b6

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package BehavioralPatterns.StrategyDP;
2+
3+
public class Main {
4+
public static void main(String[] args){
5+
PaymentService paymentService1 = new PaymentService(100, new PaymentByPayPal());
6+
PaymentService paymentService2 = new PaymentService(50, new PaymentByCard());
7+
paymentService1.processPayment();
8+
paymentService2.processPayment();
9+
}
10+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package BehavioralPatterns.StrategyDP;
2+
3+
public class PaymentByCard implements PaymentMethod{
4+
@Override
5+
public void pay(int amount) {
6+
System.out.println("Payed " + amount + " using Credit Card");
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package BehavioralPatterns.StrategyDP;
2+
3+
public class PaymentByPayPal implements PaymentMethod{
4+
@Override
5+
public void pay(int amount) {
6+
System.out.println("Payed " + amount + " using PayPal");
7+
}
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package BehavioralPatterns.StrategyDP;
2+
3+
public interface PaymentMethod {
4+
void pay(int amount);
5+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package BehavioralPatterns.StrategyDP;
2+
3+
public class PaymentService {
4+
private final int price;
5+
private final PaymentMethod method;
6+
7+
public PaymentService(int price, PaymentMethod method) {
8+
this.price = price;
9+
this.method = method;
10+
}
11+
12+
public void processPayment() {
13+
method.pay(price);
14+
}
15+
}

0 commit comments

Comments
 (0)