Factory Pattern

40 views
Skip to first unread message

Dan Vega

unread,
Mar 15, 2011, 10:47:38 PM3/15/11
to coldfu...@googlegroups.com
I am working on some code and I keep refactoring trying to improve upon the design. I am working on a project of mine (hyrule) that does data validation. A user can set some properties like so.

this.constraints = [
  {property="firstname",blank=false},
  {property="lastname",blank=false},
  {property="email",email=true},
  {property="username",min=6,max=20,context="create"},
  {property="password",min=6,max=20,context="create"}
]

Property is the name of the property we are validating
blank/email/min/max are constraints that we want to validate
context is used to validate that data in certain contexts (so you don't validate all of the data all of the time


so lets say we have a validator class that validates the data the user entered and in that class we have a validate method



public boolean function validate(){

       // lots of stuff happening

       // we read the type based on that struct coming in..say it was blank

       var obj = getConstraintFactory().getConstraint(key);
}


This hands the responsibility off to the factory which looks like this 


My question is what the heck do you do in the instance someone passes an incorrect type to getConstraint() ? Do you just return a null, throw an error? Is it common for a constraint factory to have a list of valid constraints that you can check against before trying to get a component? 

I ask this because in my case I have a ton of properties i am searching against

property
email
blank
context

and not all of them have a constraint nor should they be validated against. My idea was to do something like

if( structKeyExists(getConstraintFactory().getValidConstriants(),"blank"){
   getConstraintFactory().getConstraint(blank)
}

I really hope through all that rambling any of that made sense :( Thanks for listening guys (and girls) 

Nathan Strutz

unread,
Mar 16, 2011, 12:41:35 AM3/16/11
to coldfu...@googlegroups.com
It depends.

If you're making something like a spring autowiring bean factory that pretty much owns all the objects in a system, you are going to throw an error. The developer typed the name of an unknown object, that should be an exceptional situation. For any object being called dynamically, we should always check for hasBean() before getBean().

On the other hand, if you are making a public API and want to allow for fat-fingered results, maybe you should just ignore it.

Given the nature of this factory, and the fact that other developers will be using it indirectly, maybe you should have a default constraint that just returns true (or whatever). However what if I was just misspelling name of my desired constraint, {property="firstname", bank=false }? In development, I hope this would throw an error so I can catch my misspelling easily. In a testing environment, maybe log a problem but not alert. then in production, just ignore it; I don't want a 300mb log file on my web server that tells me I cnat' spel.

nathan strutz
[http://www.dopefly.com/] [http://hi.im/nathanstrutz]


--
You received this message because you are subscribed to the Google Groups "Object-Oriented Programming in ColdFusion" group.
To post to this group, send email to coldfu...@googlegroups.com.
To unsubscribe from this group, send email to coldfusionoo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/coldfusionoo?hl=en.

Dan Vega

unread,
Mar 16, 2011, 9:31:46 AM3/16/11
to coldfu...@googlegroups.com
First off thanks for the reply Nathan

I like the idea of a hasConstraint() method to see if the factory contains such a constraint. My question with this is I have a couple of ways that I can set this list. I can pass in a list of valid constraints (or set them in the constructor) or I can do what I originally was doing. I was reading the constraint directory and loading a list of cfcs and striping of Constraint.cfc. So if there was a BlankConstraint.cfc I would end up with "Blank" as one of my valid types. I am not sure what to do here because it just doesn't seem to me like its the job of the factory to get a list of valid constraints.

As far as spelling is concerned I have no clue what I can do about that. Remember this could be my list of values to examine

property,blank,email,context,min,maxx

How would I ever know what is an actual constraint , other property or something someone mistyped ? 
Reply all
Reply to author
Forward
0 new messages