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 7713883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:59:02+00:00 2026-06-01T01:59:02+00:00

I’m working on a project which I want to build up OO. Now I

  • 0

I’m working on a project which I want to build up OO. Now I came with a function that checks or a value is valid.

private function valid(value:*, acceptedValues:Array):Boolean {
   for(var i:uint = 0; i < acceptedValues.length; i++) {
        if (value == acceptedValues[i]) {
            return true;
        }
    }
    return false;
}

As you can see, the function is very general and will be accessed across different classes.
Now my question is; where do I store it in a OO correct way?

Thanks in advance!

  • 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-01T01:59:03+00:00Added an answer on June 1, 2026 at 1:59 am

    I’ll add some more input to the confusion and say this:

    You won’t want a single method to validate your values. Today, just passing an array of valid values might be enough. But tomorrow, you’ll have something like an e-mail address to validate, and then you’ll need a method that validates against a RegEx. Maybe next week, you’ll need to validate against a set of values that derives from the context the value was taken from, and so on…

    Using inheritance in this context, as one comment suggested, is not a good idea – you’ll tightly couple your validations to the rest of the code, and sooner or later you’ll find yourself changing a lot of things when only a simple validation call should have changed. Same goes for a utility class: You’ll find yourself using that class reference lots of times, and if you ever choose to change your validation method, you’ll have to accommodate for lots of changes in lots of places.

    So, in good OO fashion, you best use an interface, let’s call it Validator and let all of your validating classes implement it:

    public interface Validator {
        function validate ( value : * ) : Boolean;
    } 
    

    By the way, that’s also the ultimate reason not to use a static class: There are no static interfaces in ActionScript.

    Now for some classes. Let’s start with your own validation method, based on an array of values:

    public class ArrayValidatorImpl implements Validator {
        private _validValues : Array;
    
        public function validate ( value : * ) : Boolean {
            return value in _validValues;
        } 
    
        public function ArrayValidatorImpl (validValues:Array ) {
            _validValues = validValues;
        }
    }
    

    …and the e-mail one:

    public class EmailValidatorImpl implements Validator {
        public function validate ( value : * ) : Boolean {
            var reg:RegExp = /(^[a-z0-9_\+-]+(\.[a-z0-9_\+-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*\.(ac|ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|asia|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cat|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|info|int|io|iq|ir|is|it|je|jm|jo|jobs|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mobi|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tel|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|travel|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|xn|ye|yt|yu|za|zm|zw{2,4})$)/;
            return reg.exec( value.toString() );
        } 
    }
    

    Any time you need validation now, you can simply pass an instance of the interface to the class that needs it, for example:

    public class MyValidatingClass {
        private var _validator:Validator;
    
        public function myGreatMethod ( myValue : * ) : void {
            if( _validator.validate( myValue ) ) doStuffWith( myValue );
        }
    
        // ...
    
        public function MyValidatingClass( validator:Validator ) {
            _validator = validator;
        }
    }  
    

    If your requirements change, you can simply pass a different implementation, with out ever having to touch the code for MyValidatingClass again. Clean, simple, loosely coupled – and ready to be reused in the next program you write. And the one after that. And so on…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is

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.