Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:44:12+00:00 2026-06-10T14:44:12+00:00

I have the following class: class UserValidator{ // no constructor , default is fine.

  • 0

I have the following class:

class UserValidator{

     // no constructor , default is fine. 

     void Validate(Request request, Response response){
        // some validation logic on request
        // if fails, add the message to response.
     }

}

Similarly I can do the same as follows:

class UserValidator{

     private Request request;
     private Response response;

     UserValidator(Request _request, Response _response)
     {
         request = _request;
         response = _response;
     }

     void Validate(){
        // some validation logic for request
        // if fails, add the message to response.
     }

}

For Testing which one is more preferable? Can i say that the second UserValidator class has a state and thus easier to test, whereas first one doesnt have a state and harder to test? Matter of the fact the first one can be a static class as well, which can not be passed to other methods, but if called statically it doest work.

Which one is better in terms of testability? which one contains state? which is more ideal?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:44:13+00:00Added an answer on June 10, 2026 at 2:44 pm

    Only the second one contains state.

    Personally, I would use the first one statically, like

    class UserValidator{
    
         private UserValidator() { } // no constructor
    
         public static void Validate(Request request, Response response){
            // some validation logic on request
            // if fails, add the message to response.
         }
    
    }
    

    However, there is an advantage to storing the result – you can run it again and again without needing to actually run it.

    class UserValidator {
        public static enum ValidatorState { NOT_RUN, PASSED, FAILED }
        private ValidatorState state = NOT_RUN;
        private final Request request;
        private final Response response;
        public UserValidator(Request request, Response response) {
            this.request = request;
            this.response = response;
        }
        public ValidatorState validate() {
            if(state != NOT_RUN) return state;
            if(blah blah)
                state = PASSED;
            else
                state = FAILED;
            return state;
        }
    }
    

    To have a list of various validators, you will need some kind of interface to perform that.

    public interface Validator {
        boolean validate(Request request, Response response);
    }
    

    Then you would create a static method library with all your validation. Note that these are made up examples, but contain some real-world-ish kind of logic.

    public class ValidationLibrary {
        private ValidationLibrary() { }
        public boolean validateUsername(Request request, Response response) {
            String name = request.getProperty("username");
            if(name.length() < 3) return false;
            if(name.length() > 12) return false;
            if(name.equals(name.reverse()) return false; // who knows?
            return true;
        }
        public boolean validateSecurity(Request request, Response response) {
            // what page is the user trying to reach?
            SecurityRealm realm = SecurityRealm.realmForPage(request.getProperty("page"));
            String username = request.getProperty("name");
            return realm.allows(username);
        }
    }
    

    Later if you need multiple validators, you can simply do this

    List<Validator> validators = new ArrayList<Validator>();
    validators.add(new Validator(){ 
        public boolean validate(Request req, Response res) { ValidationLibrary.validateUsername(req,res); } 
    });
    validators.add(new Validator(){ 
        public boolean validate(Request req, Response res) { ValidationLibrary.validateSecurity(req,res); } 
    });
    

    Then when you get a request, you simply do this to validate it:

    for(Validator v : validators) if(!v.validate(request,response)) return false;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following class public class ButtonChange { private int _buttonState; public void SetButtonState(int
I have following class, I need to get type in constructor, how can I
I have the following class/entity: public class Product : ISaleable { public void ProcessSale()
I have following class that returns number of current Request per Second of IIS.
I have following code: class A { public: A(); private: void slot(); }; The
I have following class in android, where I am defining some functions to calculate
I have following situation class base { public: virtual void Accepted(SOCKET s) //// Event
I have following class. In this, Iris is another class with some attributes. public
I have following class with a constructor : public class TestAdapter { protected static
I have following class (as seen through reflector) public class W : IDisposable {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.