第1部第9章私たちの生活の時間

42170 ワード

<現在実装が必要なリスト>
  • ドル5+10 CHF=10ドル(為替レートが2:1の場合)
  • $5 * 2 = $10
  • amount私有化
  • ドル副作用
  • 金銭四捨五入
  • equals()
  • hashCode()
  • Equal null
  • Equal object
  • ~~ 5CHF * 2 = 10CHF~~
  • ドル/Franc重複除外
  • ドル
  • 共通equals
  • 公共時間
  • FrancとDollarの比較
  • 通話?
  • 不要なサブクラスを除去するために,通貨概念を導入する.
  • money classにcurrency()メソッドを宣言します.
  • abstract class Money {
        protected int amount;
        abstract Money times(int multiplier);
        abstract String currency();
        
        public boolean equals(Object object) {
            Money money = (Money) object;
            return amount == money.amount && getClass().equals(money.getClass());
        }
    
        static Money dollar(int amount) {
            return new Dollar(amount);
        }
    
        static Money franc(int amount) {
            return new Franc(amount);
        }
    }
    
  • は、2つのサブクラスにおいてcurrency()を実装する.
  • 
    // Franc
    public class Franc extends Money{
    
        public Franc(int amount) {
            this.amount = amount;
        }
    
        Money times(int multiplier) {
            return new Franc( amount * multiplier);
        }
    
        @Override
        String currency() {
            return "CHF";
        }
    
        public boolean equals(Object object) {
            Money money = (Money) object;
            return amount == money.amount;
        }
    }
    
    // Dollar
    public class Dollar extends Money {
        public Dollar(int amount) {
            this.amount = amount;
        }
    
        Money times(int multiplier) {
            return new Dollar( amount * multiplier);
        }
    
        @Override
        String currency() {
            return "USD";
        }
    
        public boolean equals(Object object) {
            Money money = (Money) object;
            return amount == money.amount;
        }
    }
    
    2つのクラスを含む同じ実装を望んでいます.
    ->インスタンス変数に通貨を格納し、メッサーがそれだけを返すようにします.
    
    // Money
    abstract class Money {
        protected int amount;
        abstract Money times(int multiplier);
        protected String currency;
    
        String currency() {
            return currency;
        }
    
        public boolean equals(Object object) {
            Money money = (Money) object;
            return amount == money.amount && getClass().equals(money.getClass());
        }
    
        static Money dollar(int amount) {
            return new Dollar(amount);
        }
    
        static Money franc(int amount) {
            return new Franc(amount);
        }
    }
    
    // Franc
    public class Franc extends Money{
        private String currency;
        public Franc(int amount) {
            this.amount = amount;
        }
    
        Money times(int multiplier) {
            return new Franc( amount * multiplier);
        }
    
        Franc(int amount) {
            this.amount = amount;
            currency = "CHF";
        }
        @Override
        String currency() {
            return currency;
        }
    
        public boolean equals(Object object) {
            Money money = (Money) object;
            return amount == money.amount;
        }
    }
    
    // Dollar
    public class Dollar extends Money {
        private String currency;
        public Dollar(int amount) {
            this.amount = amount;
        }
    
        Money times(int multiplier) {
            return new Dollar( amount * multiplier);
        }
    
        Dollar(int amount) {
            this.amount = amount;
            currency = "USD";
        }
        @Override
        String currency() {
            return currency;
        }
    
        public boolean equals(Object object) {
            Money money = (Money) object;
            return amount == money.amount;
        }
    }
    
    これで間違いだ.先に置いておいて、まずtimes()を整理します.
    
    // Money
    abstract class Money {
        protected int amount;
        abstract Money times(int multiplier);
        protected String currency;
    
        String currency() {
            return currency;
        }
    
        public boolean equals(Object object) {
            Money money = (Money) object;
            return amount == money.amount && getClass().equals(money.getClass());
        }
    
        static Money dollar(int amount) {
            return new Dollar(amount, "USD");
        }
    
        static Money franc(int amount) {
            return new Franc(amount, "CHF");
        }
    }
    
    // Franc
    public class Franc extends Money{
        private String currency;
        public Franc(int amount) {
            this.amount = amount;
        }
    
        Money times(int multiplier) {
            return Money.franc(amount * multiplier);
        }
    
        Franc(int amount, String currency) {
            this.amount = amount;
            this.currency = currency;
        }
        @Override
        String currency() {
            return currency;
        }
    
        public boolean equals(Object object) {
            Money money = (Money) object;
            return amount == money.amount;
        }
    }
    
    // Dollar
    public class Dollar extends Money {
        private String currency;
        public Dollar(int amount) {
            this.amount = amount;
        }
    
        Money times(int multiplier) {
            return Money.dollar( amount * multiplier);
        }
    
        Dollar(int amount, String currency) {
            this.amount = amount;
            this.currency = currency;
        }
        @Override
        String currency() {
            return currency;
        }
    
        public boolean equals(Object object) {
            Money money = (Money) object;
            return amount == money.amount;
        }
    }
    現在、2つの成長は同じであるため、実装をより高いレベルに向上させることができる.
    // Money
    abstract class Money {
        protected int amount;
        abstract Money times(int multiplier);
        protected String currency;
    
        String currency() {
            return currency;
        }
    
        public boolean equals(Object object) {
            Money money = (Money) object;
            return amount == money.amount && getClass().equals(money.getClass());
        }
    
        Money(int amount, String currency) {
            this.amount = amount;
            this.currency = currency;
        }
    
        static Money dollar(int amount) {
            return new Dollar(amount, "USD");
        }
    
        static Money franc(int amount) {
            return new Franc(amount, "CHF");
        }
    }
    
    // Franc
    public class Franc extends Money{
        private String currency;
    
        Money times(int multiplier) {
            return Money.franc(amount * multiplier);
        }
        Franc(int amount, String currency) {
            super(amount, currency);
        }
        @Override
        String currency() {
            return currency;
        }
    
        public boolean equals(Object object) {
            Money money = (Money) object;
            return amount == money.amount;
        }
    }
    
    // Dollar
    public class Dollar extends Money {
        private String currency;
    
        Money times(int multiplier) {
            return Money.dollar( amount * multiplier);
        }
    
        Dollar(int amount, String currency) {
            super(amount, currency);
        }
        @Override
        String currency() {
            return currency;
        }
    
        public boolean equals(Object object) {
            Money money = (Money) object;
            return amount == money.amount;
        }
    }
    <現在実装が必要なリスト>
  • ドル5+10 CHF=10ドル(為替レートが2:1の場合)
  • $5 * 2 = $10
  • amount私有化
  • ドル副作用
  • 金銭四捨五入
  • equals()
  • hashCode()
  • Equal null
  • Equal object
  • ~~ 5CHF * 2 = 10CHF~~
  • ドル/Franc重複除外
  • ドル
  • 共通equals
  • 公共時間
  • FrancとDollarの比較
  • 通話?
  • 削除
  • testFrancマルチレプリケーション
  • 私たちは今まで.
  • の大きな設計アイデアを処理する際、困難のため、より小さな仕事をしました.
  • の他の部分を発信者に移行し、2つの生成者をマッチングさせる.
  • 回()の再包装を一時停止しました.
  • は、同じ生成者をより高いレベルに移行する.