如何确保你的代码遵循 SOLID 原则?
How to be sure that your code follows the SOLID principle?
我对面向对象的设计有疑问。假设我必须为 phone 调用实施一个通用的 bill evaluator。在输入中,我可以使用通话记录(以秒为单位的号码和持续时间),我想获得每次通话的价格。
我想的是使用通用的 Call class 和抽象的 BillEvaluator ,对于每个调用,设置"bill strategy" IStragety,基于抽象函数。
每个 subclass(OrangeBill、VodafoneBill、..)都必须根据某些标准(持续时间、数量、...)实施账单策略。最后,实际价格由实现 IStragety 的 class 评估。
这是一个好的解决方案吗?如果我想尝试一个新的 PhoneOperator,那么我必须创建一个新的 BillHandler subclass,或者如果我可以创建一个新的 IStrategy 实现。但是如果 Vodafone 决定添加新的账单策略(例如,超过 1 小时的通话是免费的),那么我将不得不修改 VodafoneBill class...所以我的解决方案不遵守 SOLID 原则!
这是代码(为了简单起见,避免了所有 get/set 和检查):
import java.io.*;
import java.util.*;
// A generic call, with a number, duration in seconds
// and a price (to evaluate)
class Call{
public String number;
public int duration;
public double price;
public Call(String number, int duration){
this.number = number;
this.duration = duration;
}
public String toString(){
return number + " (" + duration + ")-->" + price;
}
}
// Strategy to determine the price of a call
interface IStrategy{
void setCost(Call call);
}
// Each seconds of the call is charged of 3 cents
class secondsPrice implements IStrategy{
public void setCost(Call call){
call.price = 0.03 * call.duration;
}
}
// each minutes of the conversation is charged 55 cents
// round the minutes value Es. 3:02 => 4 minutes.
class minutesPrice implements IStrategy{
public void setCost(Call call){
int duration = call.duration;
int minutes = duration / 60;
if(duration % 60 > 0){
minutes = minutes + 1;
}
call.price = 0.55 * minutes;
}
}
// each minutes of conversation is charged 1 cents.
class lowPrice implements IStrategy{
public void setCost(Call call){
call.price = 0.01 * (call.duration / 60);
}
}
// Generic class that set the price for each
// call in the log
abstract class BillHandler{
public void evaluateBill(List<Call> log){
for(Call aCall : log){
IStrategy s = billStrategy(aCall);
s.setCost(aCall);
}
}
abstract IStrategy billStrategy(Call call);
}
// Concrete implementation of the Orange Billing strategy.
class OrangeBill extends BillHandler{
IStrategy billStrategy(Call call){
if(call.duration <= 180){
return new secondsPrice();
}
else{
return new minutesPrice();
}
}
}
class VodafoneBill extends BillHandler{
IStrategy billStrategy(Call call){
if(call.number.equals("122")){
return new lowPrice();
}
else if(call.duration < 100){
return new secondsPrice();
}
else{
return new minutesPrice();
}
}
}
class myCode
{
public static void main (String[] args) throws java.lang.Exception
{
myCode c = new myCode();
List<Call> log = new ArrayList<>();
log.add(new Call("122", 180));
log.add(new Call("114", 179));
log.add(new Call("122", 200));
log.add(new Call("411", 54));
System.out.println(log);
BillHandler bill = new OrangeBill();
bill.evaluateBill(log);
System.out.println("OrangeBill:");
System.out.println(log);
bill = new VodafoneBill();
bill.evaluateBill(log);
System.out.println("VodafoneBill:");
System.out.println(log);
}
}
编辑
可能使用责任链模式实施
import java.io.*;
import java.util.*;
// A generic call, with a number, duration in seconds
// and a price (to evaluate)
class Call{
public String number;
public int duration;
public double price;
public Call(String number, int duration){
this.number = number;
this.duration = duration;
}
public String toString(){
return number + " (" + duration + ")-->" + price;
}
}
// Interface implemented by different Provider
interface BillHandler{
void priceCall(Call call);
}
//Orange provider
class OrangeBill implements BillHandler{
private callHandler secondsH = new orangeSecondsHandler();
private callHandler minutesH = new orangeMinutesHandler();
private callHandler commondH = new commonFareHandler();
public void priceCall(Call call){
secondsH.processCall(call);
}
OrangeBill(){
secondsH.setSuccessor(minutesH);
minutesH.setSuccessor(commondH);
}
}
// Vodafone provider
class VodafoneBill implements BillHandler{
private callHandler secondsH = new vodafoneSecondsHandler();
private callHandler minutesH = new vodafoneMinutesHandler();
private callHandler lowCallH = new vodafoneLowCallHandler();
private callHandler commondH = new commonFareHandler();
public void priceCall(Call call){
secondsH.processCall(call);
}
VodafoneBill(){
lowCallH.setSuccessor(secondsH);
secondsH.setSuccessor(minutesH);
minutesH.setSuccessor(commondH);
}
}
// Generic call handler
abstract class callHandler{
public callHandler next;
public void setSuccessor(callHandler next){
this.next = next;
}
abstract public boolean isChargable(Call call);
abstract public void priceCall(Call call);
public void processCall(Call call){
if(isChargable(call)){
priceCall(call);
}
else{
next.processCall(call);
}
}
}
// Concrete implementations of different call handler based
// on its provider policy
// Each seconds of the call is charged of 3 cents
class orangeSecondsHandler extends callHandler{
public boolean isChargable(Call call){
return call.duration <= 180;
}
public void priceCall(Call call){
call.price = 0.03 * call.duration;
}
}
// each minutes of the conversation is charged 55 cents
// round the minutes value Es. 3:02 => 4 minutes.
class orangeMinutesHandler extends callHandler{
public boolean isChargable(Call call){
return call.duration <= 180;
}
public void priceCall(Call call){
int duration = call.duration;
int minutes = duration / 60;
if(duration % 60 > 0){
minutes = minutes + 1;
}
call.price = 0.55 * minutes;
}
}
// Each seconds of the call is charged of 5 cents
class vodafoneSecondsHandler extends callHandler{
public boolean isChargable(Call call){
return call.duration <= 100;
}
public void priceCall(Call call){
call.price = 0.05 * call.duration;
}
}
// each minutes of the conversation is charged 30 cents
// round the minutes value Es. 3:02 => 4 minutes.
class vodafoneMinutesHandler extends callHandler{
public boolean isChargable(Call call){
return call.duration <= 250;
}
public void priceCall(Call call){
int duration = call.duration;
int minutes = duration / 60;
if(duration % 60 > 0){
minutes = minutes + 1;
}
call.price = 0.30 * minutes;
}
}
// Call to selected number are charged by 0.02 cents
// for every minute, without round!
class vodafoneLowCallHandler extends callHandler{
public boolean isChargable(Call call){
return call.number.equals("122");
}
public void priceCall(Call call){
int duration = call.duration;
int minutes = duration / 60;
call.price = 0.02 * minutes;
}
}
class commonFareHandler extends callHandler{
public boolean isChargable(Call call){
return true;
}
public void priceCall(Call call){
call.price = 1 * call.duration;
}
}
class myCode
{
public static void main (String[] args) throws java.lang.Exception
{
myCode c = new myCode();
List<Call> log = new ArrayList<>();
log.add(new Call("122", 180));
log.add(new Call("114", 179));
log.add(new Call("122", 200));
log.add(new Call("411", 54));
System.out.println(log);
// Evaluate the bill with Orange
BillHandler bill = new OrangeBill();
for(Call call : log)
bill.priceCall(call);
System.out.println("OrangeBill:");
System.out.println(log);
// Evaluate the bill with Vodafone
bill = new VodafoneBill();
for(Call call : log)
bill.priceCall(call);
System.out.println("VodafoneBill:");
System.out.println(log);
}
}
我可以提出以下建议:
IStrategy
对我来说似乎是多余的。一种策略适用于多个运营商的可能性似乎很低。因此,如果存在策略到运算符的 1:1 映射,我会删除额外的接口。
- 您可以使用责任链模式来实现具体的
BillHandler
,因为这将使您摆脱 Call
上的多个 if-else 条件。查看 this 示例。
我对面向对象的设计有疑问。假设我必须为 phone 调用实施一个通用的 bill evaluator。在输入中,我可以使用通话记录(以秒为单位的号码和持续时间),我想获得每次通话的价格。
我想的是使用通用的 Call class 和抽象的 BillEvaluator ,对于每个调用,设置"bill strategy" IStragety,基于抽象函数。 每个 subclass(OrangeBill、VodafoneBill、..)都必须根据某些标准(持续时间、数量、...)实施账单策略。最后,实际价格由实现 IStragety 的 class 评估。
这是一个好的解决方案吗?如果我想尝试一个新的 PhoneOperator,那么我必须创建一个新的 BillHandler subclass,或者如果我可以创建一个新的 IStrategy 实现。但是如果 Vodafone 决定添加新的账单策略(例如,超过 1 小时的通话是免费的),那么我将不得不修改 VodafoneBill class...所以我的解决方案不遵守 SOLID 原则!
这是代码(为了简单起见,避免了所有 get/set 和检查):
import java.io.*;
import java.util.*;
// A generic call, with a number, duration in seconds
// and a price (to evaluate)
class Call{
public String number;
public int duration;
public double price;
public Call(String number, int duration){
this.number = number;
this.duration = duration;
}
public String toString(){
return number + " (" + duration + ")-->" + price;
}
}
// Strategy to determine the price of a call
interface IStrategy{
void setCost(Call call);
}
// Each seconds of the call is charged of 3 cents
class secondsPrice implements IStrategy{
public void setCost(Call call){
call.price = 0.03 * call.duration;
}
}
// each minutes of the conversation is charged 55 cents
// round the minutes value Es. 3:02 => 4 minutes.
class minutesPrice implements IStrategy{
public void setCost(Call call){
int duration = call.duration;
int minutes = duration / 60;
if(duration % 60 > 0){
minutes = minutes + 1;
}
call.price = 0.55 * minutes;
}
}
// each minutes of conversation is charged 1 cents.
class lowPrice implements IStrategy{
public void setCost(Call call){
call.price = 0.01 * (call.duration / 60);
}
}
// Generic class that set the price for each
// call in the log
abstract class BillHandler{
public void evaluateBill(List<Call> log){
for(Call aCall : log){
IStrategy s = billStrategy(aCall);
s.setCost(aCall);
}
}
abstract IStrategy billStrategy(Call call);
}
// Concrete implementation of the Orange Billing strategy.
class OrangeBill extends BillHandler{
IStrategy billStrategy(Call call){
if(call.duration <= 180){
return new secondsPrice();
}
else{
return new minutesPrice();
}
}
}
class VodafoneBill extends BillHandler{
IStrategy billStrategy(Call call){
if(call.number.equals("122")){
return new lowPrice();
}
else if(call.duration < 100){
return new secondsPrice();
}
else{
return new minutesPrice();
}
}
}
class myCode
{
public static void main (String[] args) throws java.lang.Exception
{
myCode c = new myCode();
List<Call> log = new ArrayList<>();
log.add(new Call("122", 180));
log.add(new Call("114", 179));
log.add(new Call("122", 200));
log.add(new Call("411", 54));
System.out.println(log);
BillHandler bill = new OrangeBill();
bill.evaluateBill(log);
System.out.println("OrangeBill:");
System.out.println(log);
bill = new VodafoneBill();
bill.evaluateBill(log);
System.out.println("VodafoneBill:");
System.out.println(log);
}
}
编辑
可能使用责任链模式实施
import java.io.*;
import java.util.*;
// A generic call, with a number, duration in seconds
// and a price (to evaluate)
class Call{
public String number;
public int duration;
public double price;
public Call(String number, int duration){
this.number = number;
this.duration = duration;
}
public String toString(){
return number + " (" + duration + ")-->" + price;
}
}
// Interface implemented by different Provider
interface BillHandler{
void priceCall(Call call);
}
//Orange provider
class OrangeBill implements BillHandler{
private callHandler secondsH = new orangeSecondsHandler();
private callHandler minutesH = new orangeMinutesHandler();
private callHandler commondH = new commonFareHandler();
public void priceCall(Call call){
secondsH.processCall(call);
}
OrangeBill(){
secondsH.setSuccessor(minutesH);
minutesH.setSuccessor(commondH);
}
}
// Vodafone provider
class VodafoneBill implements BillHandler{
private callHandler secondsH = new vodafoneSecondsHandler();
private callHandler minutesH = new vodafoneMinutesHandler();
private callHandler lowCallH = new vodafoneLowCallHandler();
private callHandler commondH = new commonFareHandler();
public void priceCall(Call call){
secondsH.processCall(call);
}
VodafoneBill(){
lowCallH.setSuccessor(secondsH);
secondsH.setSuccessor(minutesH);
minutesH.setSuccessor(commondH);
}
}
// Generic call handler
abstract class callHandler{
public callHandler next;
public void setSuccessor(callHandler next){
this.next = next;
}
abstract public boolean isChargable(Call call);
abstract public void priceCall(Call call);
public void processCall(Call call){
if(isChargable(call)){
priceCall(call);
}
else{
next.processCall(call);
}
}
}
// Concrete implementations of different call handler based
// on its provider policy
// Each seconds of the call is charged of 3 cents
class orangeSecondsHandler extends callHandler{
public boolean isChargable(Call call){
return call.duration <= 180;
}
public void priceCall(Call call){
call.price = 0.03 * call.duration;
}
}
// each minutes of the conversation is charged 55 cents
// round the minutes value Es. 3:02 => 4 minutes.
class orangeMinutesHandler extends callHandler{
public boolean isChargable(Call call){
return call.duration <= 180;
}
public void priceCall(Call call){
int duration = call.duration;
int minutes = duration / 60;
if(duration % 60 > 0){
minutes = minutes + 1;
}
call.price = 0.55 * minutes;
}
}
// Each seconds of the call is charged of 5 cents
class vodafoneSecondsHandler extends callHandler{
public boolean isChargable(Call call){
return call.duration <= 100;
}
public void priceCall(Call call){
call.price = 0.05 * call.duration;
}
}
// each minutes of the conversation is charged 30 cents
// round the minutes value Es. 3:02 => 4 minutes.
class vodafoneMinutesHandler extends callHandler{
public boolean isChargable(Call call){
return call.duration <= 250;
}
public void priceCall(Call call){
int duration = call.duration;
int minutes = duration / 60;
if(duration % 60 > 0){
minutes = minutes + 1;
}
call.price = 0.30 * minutes;
}
}
// Call to selected number are charged by 0.02 cents
// for every minute, without round!
class vodafoneLowCallHandler extends callHandler{
public boolean isChargable(Call call){
return call.number.equals("122");
}
public void priceCall(Call call){
int duration = call.duration;
int minutes = duration / 60;
call.price = 0.02 * minutes;
}
}
class commonFareHandler extends callHandler{
public boolean isChargable(Call call){
return true;
}
public void priceCall(Call call){
call.price = 1 * call.duration;
}
}
class myCode
{
public static void main (String[] args) throws java.lang.Exception
{
myCode c = new myCode();
List<Call> log = new ArrayList<>();
log.add(new Call("122", 180));
log.add(new Call("114", 179));
log.add(new Call("122", 200));
log.add(new Call("411", 54));
System.out.println(log);
// Evaluate the bill with Orange
BillHandler bill = new OrangeBill();
for(Call call : log)
bill.priceCall(call);
System.out.println("OrangeBill:");
System.out.println(log);
// Evaluate the bill with Vodafone
bill = new VodafoneBill();
for(Call call : log)
bill.priceCall(call);
System.out.println("VodafoneBill:");
System.out.println(log);
}
}
我可以提出以下建议:
IStrategy
对我来说似乎是多余的。一种策略适用于多个运营商的可能性似乎很低。因此,如果存在策略到运算符的 1:1 映射,我会删除额外的接口。- 您可以使用责任链模式来实现具体的
BillHandler
,因为这将使您摆脱Call
上的多个 if-else 条件。查看 this 示例。