【发布时间】:2018-01-07 12:22:15
【问题描述】:
如果我有一个类,其中的所有函数都需要先通过检查,以下哪个更优选
1.
class check{
public check(Status){
if(CheckFunc(Status)){
// Passed the Check function and return a class instance
return new UpdateClass();
}
}
class UpdateClass{
public void Func1(){
// Do the stuff
}
// Other functions...
}
或
2.
class UpdateClass{
boolean passedCheck = false;
public UpdateClass(Status){
if(checkFunc(Status)){
this.passedCheck = true;
}
}
public void Func1(){
if(this.passedCheck){
// Do the stuff
...
}
}
// Other functions...
}
另外,使用类/函数来决定要实例化哪个类是一种好方法吗?
例如:
public Animal decideWhichClass(Status){
if(Status == StatusA){
return new Dog();
}
else if (Status == StatusB){
return new Cat();
}
}
Animal a = decideWhichClass(CurrentStatus);
【问题讨论】:
标签: design-patterns