????????Shoes????????????????????type???????仯???????????????????????Shoes??????????????type?????????????????????????????????????????????????????????????Shoes??????????????????????????????????????????
????????????????????????Replace type code with subclasses???????????????????????????????????????Shoes???????????????type????仯??????????????????????????Replace type code with strategy/state???????????????????????????
???????????????????????????????????????????????ê??
????????????????????????????type?????????????Encapsulate field??type????????ê??????????type???滻??
Type Shoes::getType() const
{
return type;
}
double Shoes::getCharge(int quantity) const
{
double result = 0;
switch(getType())
{
case REGULAR:
result += price * quantity;
break;
case NEW_STYLE:
if (quantity > 1)
{
result += (price + (quantity - 1) * price * 0.8);
}
else
{
result += price;
}
break;
case LIMITED_EDITION:
result += price * quantity * 1.1;
break;
}
return result;
}
?????????????getCharge?ж?type?????????滻??????′???????г??????getType()??????Shoes????й????????????????type????????????????????
???????????Shoes???????????????????????????Shoes???????????????????????????ê?????????????β??????Shoes????幹???????????????setup??????????????????
????struct Shoes
????{
????static Shoes* create(Type type?? double price);
????Shoes(Type type?? double price);
????double getCharge(int quantity) const;
????private:
????Type getType() const;
????private:
????Type type;
????double price;
????};
????Shoes* Shoes::create(Type type?? double price)
????{
????return new Shoes(type?? price);
????}
????????????????????????????substitute????????????????????Shoes???????????滻????ù??????????滻????Shoes???????????protected(???????)????в????
????struct Shoes
????{
????static Shoes* create(Type type?? double price);
????double getCharge(int quantity) const;
????protected:
????Shoes(Type type?? double price);
????private??
????Type getType() const;
????private:
????Type type;
????double price;
????};
????// client code
????// Shoes* shoes = new Shoes(REGULAR?? 100.0);
????Shoes* shoes = Shoes::create(REGULAR?? 100.0);
?????????????????????????????????????Shoes?????????????????
????????????ê?????????OK?????ê??getType?????????ζ?type???????????????????????type?????滻????ê??create????????Shoes????幹?????????????????滻????????Shoes?????
???????????????????????Shoes???????????Shoes??????????滻??
?????????Shoes???getType?????????鷽????????????????setup????????RegularShoes?????Shoes??????д??getType????????????????????
????struct RegularShoes : Shoes
????{
????RegularShoes(double price);
????private:
????Type getType() const override;
????};
????RegularShoes::RegularShoes(double price)
????: Shoes(REGULAR?? price)
????{
????}
????Type RegularShoes::getType() const
????{
????return REGULAR;
????}
?????????????????substitute????RegularShoes?滻Shoes????????ж???REGULAR????????
????Shoes* Shoes::create(Type type?? double price)
????{
????if(type == REGULAR) return new RegularShoes(price);
????return new Shoes(type?? price);
????}
???????????????NewStyleShoes??LimitedEditionShoes?????滻???????????С?
NewStyleShoes::NewStyleShoes(double price)
: Shoes(NEW_STYLE?? price)
{
}
Type NewStyleShoes::getType() const
{
return NEW_STYLE;
}
LimitedEditionShoes::LimitedEditionShoes(double price)
: Shoes(LIMITED_EDITION?? price)
{
}
Type LimitedEditionShoes::getType() const
{
return LIMITED_EDITION;
}
Shoes* Shoes::create(Type type?? double price)
{
switch(type)
{
case REGULAR:
return new RegularShoes(price);
case NEW_STYLE:
return new NewStyleShoes(price);
case LIMITED_EDITION:
return new LimitedEditionShoes(price);
}
return nullptr;
}
??????Shoes::create?????У????????????????·?????nullptr???????????????Shoes?????NullObject???????
?????????Shoes?в????????????type????????????????????????????setup?????Shoes??????????type???????????Shoes(double price)??????????????substitute?????????е????Shoes(Type type?? double price)????滻???????????????????乹????????????????????C++11???й??????????????????????????????????type??Shoes(Type type?? double price)????????????Shoes?е?getType???????麯???????????cpp????е????????
????struct Shoes
????{
????static Shoes* create(Type type?? double price);
????Shoes(double price);
????virtual ~Shoes(){}
????double getCharge(int quantity) const;
????private:
????virtual Type getType() const = 0;
????private:
????double price;
????};
????struct RegularShoes : Shoes
????{
????using Shoes::Shoes;
????private:
????Type getType() const override;
????};
?????????????????????????????????滻?????????????????????????????????????????getCharge?е??????????????????????????Replace Condition with Polymorphism(???????????????)????????????????????????????
?????????getCharge??????鷽????????????????setup???????д???getCharge???д?????????????????????copy?????????????????????????????Shoes?е?price???????????protected??
????struct Shoes
????{
????static Shoes* create(Type type?? double price);
????Shoes(double price);
????virtual ~Shoes(){}
????virtual double getCharge(int quantity) const;
????private:
????virtual Type getType() const = 0;
????protected:
????double price;
????};
????struct RegularShoes : Shoes
????{
????using Shoes::Shoes;
????private:
????double getCharge(int quantity) const override;
????Type getType() const override;
????};
????????????????substitute?????????getCharge??????е????????滻????????????Shoes::getCharge?ж??REGULAR????????в????
????double Shoes::getCharge(int quantity) const
????{
????double result = 0;
????switch(getType())
????{
????case NEW_STYLE:
????if (quantity > 1)
????{
????result += (price + (quantity - 1) * price * 0.8);
????}
????else
????{
????result += price;
????}
????break;
????case LIMITED_EDITION:
????result += price * quantity * 1.1;
????break;
????}
????return result;
????}
???????????????Shoes::getCharge??????????????????????????У?????Shoes::getCharge???滻???????Shoes::getCharge?????????????????????麯????????????????getType???????????????????
????struct Shoes
????{
????static Shoes* create(Type type?? double price);
????Shoes(double price);
????virtual ~Shoes(){}
????virtual double getCharge(int quantity) const = 0;
????protected:
????double price;
????};
????Shoes::Shoes(double price)
????: price(price)
????{
????}
????Shoes* Shoes::create(Type type?? double price)
????{
????switch(type)
????{
????case REGULAR:
????return new RegularShoes(price);
????case NEW_STYLE:
????return new NewStyleShoes(price);
????case LIMITED_EDITION:
????return new LimitedEditionShoes(price);
????}
????return nullptr;
????}
????struct RegularShoes : Shoes
????{
????using Shoes::Shoes;
????private:
????double getCharge(int quantity) const override;
????};
????double RegularShoes::getCharge(int quantity) const
????{
????return price * quantity;
????}
????struct NewStyleShoes : Shoes
????{
????using Shoes::Shoes;
????private:
????double getCharge(int quantity) const override;
????};
????double NewStyleShoes::getCharge(int quantity) const
????{
????double result = price;
????if (quantity > 1)
????{
????result += ((quantity - 1) * price * 0.8);
????}
????return result;
????}
????struct LimitedEditionShoes : Shoes
????{
????using Shoes::Shoes;
????private:
????double getCharge(int quantity) const override;
????};
????double LimitedEditionShoes::getCharge(int quantity) const
????{
????return price * quantity * 1.1;
????}
????????????????????????ɡ?????????????????????????????????а??С???????????????????ê?????á????????????????????getCharge?е?switch-case????????????????????????????Shoes::create??????????????switch-case???????????????switch-case??????????????????????????????????????????????????????? ??????????и?????????в???????????????????????????У????????????μ?switch-case????? ???????У????????????仯????????????????????????????????????μ????