Results 1 to 13 of 13
  1. #1
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default Functions and Classes

    Can something that is basically a function be a class?

    When a user registers on my website, one of the things I need to do is check to see if the Username already exists.

    Should this just be a function (method) in a class, or can I break it out and make it its own class so that it can be re-used by others?


    Debbie

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You can't set up a function outside of a class, if that's your question.

    You could set up a class with the method you wish to use inside of it (you could even make it static if you wanted to). So let's use this as a hypothetical:
    Java Code:
    class Registration {
    	public boolean CanRegister(String username, String password) {
    		if (UsernameChecker.IsUsernameTaken(username)) {
    			// Notify user that name is in use.
    		} else {
    			this.Register(username,password);
    		}
    	}
    }
    
    class UsernameChecker {
    	public static boolean IsUsernameTaken(String username) {
    		// Code to check if a username is in use
    	}
    }

    Of course, this is just a skeleton code. You would also want to check if the username is valid, if the password is valid (and so on). But this should get you started.

    Much luck! :)

  3. #3
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    You can't set up a function outside of a class, if that's your question.

    You could set up a class with the method you wish to use inside of it (you could even make it static if you wanted to). So let's use this as a hypothetical:
    Java Code:
    class Registration {
    	public boolean CanRegister(String username, String password) {
    		if (UsernameChecker.IsUsernameTaken(username)) {
    			// Notify user that name is in use.
    		} else {
    			this.Register(username,password);
    		}
    	}
    }
    
    class UsernameChecker {
    	public static boolean IsUsernameTaken(String username) {
    		// Code to check if a username is in use
    	}
    }

    Of course, this is just a skeleton code. You would also want to check if the username is valid, if the password is valid (and so on). But this should get you started.

    Much luck! :)
    The context of my question is this...

    Let's say I decide that I need a "RegistrationSevice" class. (Not sure if you'd approve of such a class. God knows others have issues with this idea of mine?!)

    And this class needs to do the following things to register a user...

    - checkRequiredFieldsComplete()
    - isValidEmail()
    - isValidPassword()
    - doEmailsMatch()
    - doPasswordsMatch()
    - isEmailUnique()

    Maybe I see value in "isValidEmail()", "isValidPassword()", and "isEmailUnique()" and want to be able to somehow re-use them later.

    Would it be a good design decision to create classes like:

    - EmailValidator

    - PasswordValidator

    - UniquenessValidator


    I think you basically addressed this above, but I wanted to put things into my own words!


    Debbie

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I kind of see what you're getting at now.

    If you want to use something like isValidEmail outside of RegistrationService, then you may want an EmailValidator class.

    However keep in mind that maybe you don't want three separate classes (one for Email, one for Password, and one for Uniqueness). You could have a single Validator class and put three functions in there, like so:
    Java Code:
    class Validator {
        public static boolean CheckEmail(String email) {
             // ...
        }
        public static boolean CheckPassword(String password) {
             // ...
        }
        public static boolean CheckIsUserUnique(String username) {
             // ...
        }
    }

    Then, from your RegistrationService class, you can call Validator.CheckEmail(...); furthermore, it can be accessed from any future classes you want to check. Perhaps LoginService, or UserCpanelService, or what-have-you.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

  6. #6
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    I kind of see what you're getting at now.

    If you want to use something like isValidEmail outside of RegistrationService, then you may want an EmailValidator class.

    However keep in mind that maybe you don't want three separate classes (one for Email, one for Password, and one for Uniqueness). You could have a single Validator class and put three functions in there, like so:
    Java Code:
    class Validator {
        public static boolean CheckEmail(String email) {
             // ...
        }
        public static boolean CheckPassword(String password) {
             // ...
        }
        public static boolean CheckIsUserUnique(String username) {
             // ...
        }
    }

    Then, from your RegistrationService class, you can call Validator.CheckEmail(...); furthermore, it can be accessed from any future classes you want to check. Perhaps LoginService, or UserCpanelService, or what-have-you.
    So, is that good or bad OO design?

    I seem to keep getting ping-ponged between people that tell me to create "God Classes" - from people that only know Procedural Programming - to extreme OOP people who think a class must be very specific in its intent.

    I've had several arguments with people who think all of my User Registration code should be in my User class.

    I think I should have separate User, Registration, and Authentication classes.

    A User is a tangible thing, and Registration and Authentication are processes/services that are intangible things.

    But as per my original post, you could drill down farther and say that Email validation, Password validation, and Checking an Emails uniqueness could live on as classes too.

    This is a topic unto itself, but I *really* need help understanding this better...



    Debbie

  7. #7
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What's that mean??



    Debbie

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    It means that, if you are going to post on multiple forums, you should notify the members of both so that we are not wasting our time answering a question that has already been solved.

    On the actual topic, you should note that a lot of programming is personal preference. You will easily discover which methods work best for you; if you find that a "God class" works best for you (which I highly discourage), then go ahead and use it.

    A general rule for me is that any class should have at least two methods in it. And no class should have so many methods that you're constantly looking for a method within it.

    If you think about an Email Validation, it really shouldn't need its own class. It would have only one method. (isValidEmail() or similar.)

  9. #9
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    It means that, if you are going to post on multiple forums, you should notify the members of both so that we are not wasting our time answering a question that has already been solved.
    I wasn't getting help on the other forum so I figured I'd try over here.

    Okay.


    On the actual topic, you should note that a lot of programming is personal preference. You will easily discover which methods work best for you; if you find that a "God class" works best for you (which I highly discourage), then go ahead and use it.

    A general rule for me is that any class should have at least two methods in it. And no class should have so many methods that you're constantly looking for a method within it.

    If you think about an Email Validation, it really shouldn't need its own class. It would have only one method. (isValidEmail() or similar.)
    Do you think my Registration class looks logical and fits within your suggestions above?

    How do you think is the best way to handle user registration?


    Debbie

  10. #10
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by DoubleDee View Post
    Do you think my Registration class looks logical and fits within your suggestions above?

    How do you think is the best way to handle user registration?
    With the exception of an actual registerUser() function (that does the validation and registration), the RegistrationService class looks complete.

    Personally, I've never done a registration in Java before (done that mostly in PHP). However, I can tell you that this is how I would probably do it:
    • RegistrationService class, which holds a method for checking required fields, as well as checking the database, and finally registering the user.
    • A Validator class that holds a method for checking if a field contains invalid characters (', ", `, etc.), one for checking if email is valid, one for checking if the password/username combination is valid.
    • And the RegistrationService.registerUser() would call firstly, to check if all fields are filled; next, it would check for invalid characters in fields; then, check if each field is valid (e.g. email is in valid a@b.c format); then, it would check if the user is registered already; then finally register the user in the database.

  11. #11
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    With the exception of an actual registerUser() function (that does the validation and registration), the RegistrationService class looks complete.
    Oops! yah, I forgot to type that one in! :)


    Personally, I've never done a registration in Java before (done that mostly in PHP).
    I'm actually do my website in PHP. (I came here because I felt Java programmers are better at OOP.)

    If I post PHP here will I get yelled at?


    However, I can tell you that this is how I would probably do it:
    • RegistrationService class, which holds a method for checking required fields, as well as checking the database, and finally registering the user.
    • A Validator class that holds a method for checking if a field contains invalid characters (', ", `, etc.), one for checking if email is valid, one for checking if the password/username combination is valid.
    • And the RegistrationService.registerUser() would call firstly, to check if all fields are filled; next, it would check for invalid characters in fields; then, check if each field is valid (e.g. email is in valid a@b.c format); then, it would check if the user is registered already; then finally register the user in the database.
    Why did you split up my original Registration class into two classes like you did?

    For instance, I would think maybe you'd put the method that checks the required fields in the Validator class as well.

    Just curious.

    Did you do that to make the RegistrationService class smaller or because you want to re-use the Validator class functionality?

    BTW, why call my Registration class a "service" as in RegistrationService?

    Is just having a class called "Registration" bad?



    Debbie

    P.S. It is getting late on Sunday evening so I probably can't write any code now, but I would like to post my code attempt here later this week if you'll check back?

  12. #12
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Quote Originally Posted by DoubleDee View Post
    I'm actually do my website in PHP. (I came here because I felt Java programmers are better at OOP.)

    If I post PHP here will I get yelled at?
    Probably. ;) There are several PHP forums if that's what you're after (PHPFreak, DevShed PHP section, etc.).

    Quote Originally Posted by DoubleDee View Post
    Why did you split up my original Registration class into two classes like you did?

    For instance, I would think maybe you'd put the method that checks the required fields in the Validator class as well.
    Because the RegistrationService class is handling the registration--that means that it's handling that particular form (the registration form), whereas Validator is just handling generic validations.

    Quote Originally Posted by DoubleDee View Post
    Did you do that to make the RegistrationService class smaller or because you want to re-use the Validator class functionality?
    The latter. If you want to use those in a login service, or another part of the website/Java program, then you want to be able to access them without referring to the RegistrationService class.

    Quote Originally Posted by DoubleDee View Post
    BTW, why call my Registration class a "service" as in RegistrationService?

    Is just having a class called "Registration" bad?
    Not necessarily. You had just mentioned earlier that you used the name RegistrationService, so I carried on that paradigm.
    Quote Originally Posted by DoubleDee View Post
    Let's say I decide that I need a "RegistrationSevice" class.


    Quote Originally Posted by DoubleDee View Post
    P.S. It is getting late on Sunday evening so I probably can't write any code now, but I would like to post my code attempt here later this week if you'll check back?
    For sure; this forum doesn't close. ;)

  13. #13
    DoubleDee is offline Member
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Zack View Post
    Probably. ;) There are several PHP forums if that's what you're after (PHPFreak, DevShed PHP section, etc.).
    I know, but I want a true OOP perspective from either a C++ or Java person.

    (PHP people tend to still code very procedurally...)


    Because the RegistrationService class is handling the registration--that means that it's handling that particular form (the registration form), whereas Validator is just handling generic validations.
    Okay.


    The latter. If you want to use those in a login service, or another part of the website/Java program, then you want to be able to access them without referring to the RegistrationService class.
    Okay


    Not necessarily. You had just mentioned earlier that you used the name RegistrationService, so I carried on that paradigm.
    Oh, okay. I guess that was someone else that insisted on appending "service" to my class name!


    For sure; this forum doesn't close. ;)
    Yah, but people tend to leave and not come back to threads and people.

    I need some Java/OOP gurus to adopt me and help nuture me so I can be a truly studly OOP developer! :o



    Debbie

Similar Threads

  1. Help in implementing XOR functions
    By Manfizy in forum New To Java
    Replies: 3
    Last Post: 04-13-2010, 02:25 AM
  2. JSP Functions
    By sysout in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-07-2009, 04:10 AM
  3. 2 functions
    By baze7 in forum New To Java
    Replies: 3
    Last Post: 08-14-2009, 04:41 AM
  4. Functions in jsp
    By samson in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 03-25-2009, 10:04 PM
  5. problems with functions
    By kri in forum New To Java
    Replies: 6
    Last Post: 02-07-2009, 08:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •