dimanche 7 janvier 2018

Which programming pattern is preferred

If I have a class which all the functions in it need to pass a check first, which of the following is more preferred

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...
}

OR

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...
}

Also, is it a good way to use a class/function to decide which class to be instantiated?

for example:

    public Animal decideWhichClass(Status){
        if(Status == StatusA){
            return new Dog();
        }
        else if (Status == StatusB){
        return new Cat();
        }
    }

    Animal a = decideWhichClass(CurrentStatus);

Aucun commentaire:

Enregistrer un commentaire