-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d661333
commit ce52abb
Showing
20 changed files
with
414 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
Behavioral Design Pattern/Command Design Pattern/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
### IntelliJ IDEA ### | ||
out/ | ||
!**/src/main/**/out/ | ||
!**/src/test/**/out/ | ||
|
||
### Eclipse ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
bin/ | ||
!**/src/main/**/bin/ | ||
!**/src/test/**/bin/ | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store |
8 changes: 8 additions & 0 deletions
8
Behavioral Design Pattern/Command Design Pattern/.idea/.gitignore
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
Behavioral Design Pattern/Command Design Pattern/.idea/misc.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
Behavioral Design Pattern/Command Design Pattern/.idea/modules.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
124 changes: 124 additions & 0 deletions
124
Behavioral Design Pattern/Command Design Pattern/.idea/uiDesigner.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
Behavioral Design Pattern/Command Design Pattern/.idea/vcs.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
Behavioral Design Pattern/Command Design Pattern/Command Design Pattern.iml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
</component> | ||
</module> |
19 changes: 19 additions & 0 deletions
19
Behavioral Design Pattern/Command Design Pattern/ReadMe.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Command Design Pattern | ||
|
||
Command design pattern, bir istek/komutu, bir yöntemi çağıracak ve parametreler gibi gerekli tüm verileri kapsülleyecek bir nesneye dönüştürmek için kullanılır. | ||
Bu, isteklerin ve eylemlerin daha esnek ve soyut bir şekilde temsil edilmesini sağlar. | ||
|
||
## Problem | ||
|
||
- İstemcinin bir işlem yapacağı zaman bu işlemin hem ne yapıldığından hem de nasıl yapıldığından habersiz karşılayabilmesi gerekmektedir. | ||
- Örneğin, istemci yalnızca “Yap” demeli ve eldeki veriler en uygun algoritma ile sıralanmalıdır. | ||
- Aslında istemci “Yap” dediğinde elindeki verilerin sıralanacağını mı yoksa silineceğini mi yoksa başka bir şey mi olacağını bilemez. | ||
|
||
## Çözüm | ||
|
||
- Command design pattern bir istek atıldığı zaman bu isteğin direkt olarak atılmaması gerektiğini belirtir. | ||
|
||
- İstek atan nesne ile istek alan nesne arasında bu isteği ileten ayrı bir nesne ile istek atarsak hem alıcı hemde verici nesneler kendi görevleri ile ilgilenmiş olurlar | ||
ve birbirlerine olan bağımlılığı ortadan kalmış olur. | ||
|
||
- İstemci sadece bir metod üzerinden istek atar bu metod "execute()" olarak adlandırılır. |
18 changes: 18 additions & 0 deletions
18
Behavioral Design Pattern/Command Design Pattern/src/AbstractCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class AbstractCommand implements Command{ | ||
|
||
protected PaymentAccount getCorrectPaymentAccount(User user, Product product){ | ||
String currency = product.getCurrency(); | ||
if(PaymentConstants.USD.equals(currency)){ | ||
return user.getUsdAccount(); | ||
}else if (PaymentConstants.EUR.equals(currency)){ | ||
return user.getEurAccount(); | ||
}else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public void execute(User user, Product product) { | ||
|
||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Behavioral Design Pattern/Command Design Pattern/src/Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
public interface Command { | ||
void execute(User user, Product product); | ||
} |
5 changes: 5 additions & 0 deletions
5
Behavioral Design Pattern/Command Design Pattern/src/EURAccount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class EURAccount extends PaymentAccount { | ||
public EURAccount(double balance){ | ||
super(balance); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Behavioral Design Pattern/Command Design Pattern/src/GeneralInstanceFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import java.util.ArrayList; | ||
|
||
public class GeneralInstanceFactory { | ||
public static Product getTelephoneInstance(){ // telefon nesnesi oluşturup geriye döner. | ||
return new Product("Telefon","USD",1150); | ||
} | ||
|
||
public static User getUserInstance(){ // örnek bir user nesnesi oluşturur ve geriye döner. | ||
USDAccount usdAccount = new USDAccount(2000); | ||
EURAccount eurAccount = new EURAccount(2000); | ||
return new User(usdAccount, eurAccount, new ArrayList<>()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Behavioral Design Pattern/Command Design Pattern/src/Main.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public class Main { | ||
public static void main(String[] args) { | ||
|
||
Product telephone = GeneralInstanceFactory.getTelephoneInstance(); // telefon nesnesi üretip getirdi | ||
User user = GeneralInstanceFactory.getUserInstance(); // user nesnesi oluşturup getirdi | ||
|
||
onButtonClick(new Payment(), user, telephone); | ||
user.showInfo(); | ||
|
||
onButtonClick(new RePayment(), user, telephone); | ||
user.showInfo(); | ||
} | ||
|
||
private static void onButtonClick(Command command, User user, Product product){ | ||
command.execute(user, product); | ||
}} |
18 changes: 18 additions & 0 deletions
18
Behavioral Design Pattern/Command Design Pattern/src/Payment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class Payment extends AbstractCommand{ | ||
@Override | ||
public void execute(User user, Product product) { | ||
PaymentAccount account = getCorrectPaymentAccount(user, product); | ||
double newBalance; | ||
if(account instanceof USDAccount){ // USDAccount nesnesi mi? | ||
newBalance = user.getUsdAccount().balance - product.getPrice(); // aldığı ürünü hesaptaki paradan düşürüyor. | ||
user.getUsdAccount().balance = newBalance; | ||
System.out.println(PaymentConstants.PAYMENT_INFO_FOR_USD); | ||
} | ||
else if(account instanceof EURAccount){ | ||
newBalance = user.getEurAccount().balance - product.getPrice(); | ||
user.getEurAccount().balance = newBalance; | ||
System.out.println(PaymentConstants.PAYMENT_INFO_FOR_EUR); | ||
} | ||
user.getProductList().add(product); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
Behavioral Design Pattern/Command Design Pattern/src/PaymentAccount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
public abstract class PaymentAccount { | ||
protected double balance; | ||
|
||
public PaymentAccount(double balance){ | ||
this.balance = balance; | ||
} | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
Behavioral Design Pattern/Command Design Pattern/src/PaymentConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
public class PaymentConstants { | ||
public static final String USD = "USD"; | ||
public static final String EUR = "EUR"; | ||
|
||
public static final String PAYMENT_INFO_FOR_USD = "Dolar Hesabından Ödeme İşlemi Gerçekleştirildi."; | ||
public static final String PAYMENT_INFO_FOR_EUR = "Euro Hesabından Ödeme İşlemi Gerçekleştirildi."; | ||
|
||
public static final String REPAYMENT_INFO_FOR_USD = "Dolar Hesabına İade İşlemi Gerçekleştirildi."; | ||
public static final String REPAYMENT_INFO_FOR_EUR = "Euro Hesabına İade İşlemi Gerçekleştirildi."; | ||
} |
36 changes: 36 additions & 0 deletions
36
Behavioral Design Pattern/Command Design Pattern/src/Product.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
public class Product { | ||
|
||
private String name; | ||
private String currency; | ||
private double price; | ||
|
||
public Product(String name, String currency, double price) { | ||
this.name = name; | ||
this.currency = currency; | ||
this.price = price; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getCurrency() { | ||
return currency; | ||
} | ||
|
||
public void setCurrency(String currency) { | ||
this.currency = currency; | ||
} | ||
|
||
public double getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(double price) { | ||
this.price = price; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Behavioral Design Pattern/Command Design Pattern/src/RePayment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
public class RePayment extends AbstractCommand{ | ||
|
||
@Override | ||
public void execute(User user, Product product) { | ||
PaymentAccount account = getCorrectPaymentAccount(user, product); | ||
double newBalance; | ||
if(account instanceof USDAccount){ | ||
newBalance = user.getUsdAccount().balance + product.getPrice(); | ||
user.getUsdAccount().balance = newBalance; | ||
System.out.println(PaymentConstants.PAYMENT_INFO_FOR_USD); | ||
} | ||
else if(account instanceof EURAccount){ | ||
newBalance = user.getEurAccount().balance + product.getPrice(); | ||
user.getEurAccount().balance = newBalance; | ||
System.out.println(PaymentConstants.PAYMENT_INFO_FOR_EUR); | ||
} | ||
|
||
user.getProductList().remove(product); | ||
} | ||
|
||
} |
5 changes: 5 additions & 0 deletions
5
Behavioral Design Pattern/Command Design Pattern/src/USDAccount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
public class USDAccount extends PaymentAccount { | ||
public USDAccount(double balance){ | ||
super(balance); | ||
} | ||
} |
Oops, something went wrong.