Skip to content

Commit

Permalink
add command design pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Emin-Yildiz committed Jul 17, 2023
1 parent d661333 commit ce52abb
Show file tree
Hide file tree
Showing 20 changed files with 414 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Behavioral Design Pattern/Command Design Pattern/.gitignore
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

124 changes: 124 additions & 0 deletions 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.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 Behavioral Design Pattern/Command Design Pattern/ReadMe.md
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.
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) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public interface Command {
void execute(User user, Product product);
}
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);
}
}
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 Behavioral Design Pattern/Command Design Pattern/src/Main.java
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 Behavioral Design Pattern/Command Design Pattern/src/Payment.java
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);
}
}
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;
}

}
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 Behavioral Design Pattern/Command Design Pattern/src/Product.java
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;
}
}
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);
}

}
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);
}
}
Loading

0 comments on commit ce52abb

Please sign in to comment.