Skip to content

Instantly share code, notes, and snippets.

@rotelstift
Created January 8, 2023 10:12
Show Gist options
  • Save rotelstift/77771cf7a0a480c91fb257c7b4453ffb to your computer and use it in GitHub Desktop.
Save rotelstift/77771cf7a0a480c91fb257c7b4453ffb to your computer and use it in GitHub Desktop.
良いコード/悪いコードで学ぶ設計入門を読んで書いたコード
import java.util.HashMap;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;
class Main {
public static void main(String[] args) {
final Customer customerA = new Customer("ID-0001");
final Customer customerB = new Customer("ID-0002");
final Customer customerC = new Customer("ID-0003");
System.out.println(customerA.name() + " : " + customerA.stageRank());
System.out.println(customerB.name() + " : " + customerB.stageRank());
System.out.println(customerC.name() + " : " + customerC.stageRank());
}
}
/**
* Customerクラスを定義
* こうすることでより見通しが良くなる気がする。
*/
class Customer {
private final String id;
private final String name;
private final PurchaseHistory history;
private final GoldCustomerPolicy goldCustomerPolicy = new GoldCustomerPolicy();
private final SilverCustomerPolicy silverCustomerPolicy = new SilverCustomerPolicy();
Customer(final String id) {
this.id = id;
final CustomerDate customerDate = new CustomerDate();
final CustomerDetail customerDetail = customerDate.getCustomerDetail(id);
this.name = customerDetail.name();
this.history = customerDetail.history();
}
String stageRank() {
if (goldCustomerPolicy.complyWithAll(history)) return "Gold";
if (silverCustomerPolicy.complyWithAll(history)) return "Silver";
return "Normal";
}
String name() {
return this.name;
}
}
class CustomerDate {
final Map<String, CustomerDetail> customers = new HashMap<>();
CustomerDate() {
customers.put("ID-0001", new CustomerDetail("アリス", new PurchaseHistory(2000000, 24, 0.0001f)));
customers.put("ID-0002", new CustomerDetail("ボブ", new PurchaseHistory(20000, 12, 0.0f)));
customers.put("ID-0003", new CustomerDetail("チャーリー", new PurchaseHistory(300000, 15, 0.003f)));
}
CustomerDetail getCustomerDetail(final String id) {
return customers.get(id);
}
}
class CustomerDetail {
private final String name;
private final PurchaseHistory history;
CustomerDetail(final String name, final PurchaseHistory history) {
this.name = name;
this.history = history;
}
String name() {
return this.name;
}
PurchaseHistory history() {
return this.history;
}
}
class PurchaseHistory {
final int totalAmount;
final int purchaseFrequencyPerMonth;
final float returnRate;
PurchaseHistory(final int totalAmount, final int purchaseFrequencyPerMonth, final float returnRate){
this.totalAmount = totalAmount;
this.purchaseFrequencyPerMonth = purchaseFrequencyPerMonth;
this.returnRate = returnRate;
}
}
/**
* ECサイトにおいて優良顧客かどうかを判定するロジックを
* if switch はなるべく使わないように、Interfaceを用いて実装する。
*/
interface ExcellentCustomerRule {
boolean ok(final PurchaseHistory history);
}
class GoldCustomerPurchaseAmountRule implements ExcellentCustomerRule {
public boolean ok(final PurchaseHistory history) {
return 100000 <= history.totalAmount;
}
}
class PurchaseFrequencyRule implements ExcellentCustomerRule {
public boolean ok(final PurchaseHistory history) {
return 10 <= history.purchaseFrequencyPerMonth;
}
}
class ReturnRateRule implements ExcellentCustomerRule {
public boolean ok(final PurchaseHistory history) {
return history.returnRate <= 0.001;
}
}
class ExcellentCustomerPolicy {
private final Set<ExcellentCustomerRule> rules;
ExcellentCustomerPolicy() {
rules = new HashSet();
}
void add(final ExcellentCustomerRule rule) {
rules.add(rule);
}
boolean complyWithAll(final PurchaseHistory history) {
for (ExcellentCustomerRule each : rules) {
if (!each.ok(history)) return false;
}
return true;
}
}
class GoldCustomerPolicy {
private final ExcellentCustomerPolicy policy;
GoldCustomerPolicy() {
policy = new ExcellentCustomerPolicy();
policy.add(new GoldCustomerPurchaseAmountRule());
policy.add(new PurchaseFrequencyRule());
policy.add(new ReturnRateRule());
}
boolean complyWithAll(final PurchaseHistory history) {
return policy.complyWithAll(history);
}
}
class SilverCustomerPolicy {
private final ExcellentCustomerPolicy policy;
SilverCustomerPolicy() {
policy = new ExcellentCustomerPolicy();
policy.add(new PurchaseFrequencyRule());
policy.add(new ReturnRateRule());
}
boolean complyWithAll(final PurchaseHistory history) {
return policy.complyWithAll(history);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment