What design pattern to use

S

Smith

Hi

I couldn't find a newsgroup discussing software architecure so now I'm trying
my luck here. Please redirect me if there's a newsgroup about architecture
:)

I've got the following scenario and wondering if there's a design pattern
that suits my needs.

I need to implement business logic with a lot of rules and wondering if
there's a design pattern that suits my needs? I'd like it to be flexible to
changes, additions and deletions.

Example:

A person is registered:

If male
- If older than 30
-- Has children
-- Has no children
--- More than two children
-- Is blue eyed
If female
- If older than 30
-- Has children
-- Has no children
-- Likes pizza
 
G

Gregory A. Beamer

Hi

I couldn't find a newsgroup discussing software architecure so now I'm
trying my luck here. Please redirect me if there's a newsgroup about
architecture
:)

I've got the following scenario and wondering if there's a design
pattern that suits my needs.

I need to implement business logic with a lot of rules and wondering
if there's a design pattern that suits my needs? I'd like it to be
flexible to changes, additions and deletions.

Example:

A person is registered:

If male
- If older than 30
-- Has children
-- Has no children
--- More than two children
-- Is blue eyed
If female
- If older than 30
-- Has children
-- Has no children
-- Likes pizza

It depends on what your goal is:

If you are talking about an object that adds extra attributes
(properties), you can use a decorator pattern. This is more focused on
the state of the object.

If you are talking more about the workflow different users go through,
perhaps a strategy pattern. This is more behavior focused.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

My vacation and childhood cancer awareness site:
http://www.crazycancertour.com

*******************************************
| Think outside the box! |
*******************************************
 
M

Mr. Arnold

Smith said:
Hi

I couldn't find a newsgroup discussing software architecure so now I'm
trying
my luck here. Please redirect me if there's a newsgroup about architecture
:)

I've got the following scenario and wondering if there's a design pattern
that suits my needs.

I need to implement business logic with a lot of rules and wondering if
there's a design pattern that suits my needs? I'd like it to be flexible
to
changes, additions and deletions.

You may want to look into Broken Rules and validation of a Business Object
that is using a Broken Rules validation pattern. You add new validation
rules or remove them out of the Business Object as needed.

http://madskristensen.net/post/Add-validation-to-business-objects.aspx


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4458 (20090925) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
H

Henning Bredel

Smith said:
I need to implement business logic with a lot of rules and wondering if
there's a design pattern that suits my needs? I'd like it to be flexible to
changes, additions and deletions.

Example:

A person is registered:

If male
- If older than 30
-- Has children
-- Has no children
--- More than two children
-- Is blue eyed
If female
- If older than 30
-- Has children
-- Has no children
-- Likes pizza

If you want a pattern, which extract these condition chain from
your code, I suggest to define for each end point of such a chain
a criteria name. For example: male->children->.. ==> isCategoryXY
to categorize each person on which then want to execute an action.

I'd try a functor pattern. Encapsulate your function in an object.
As good example have a look in the commons collection project of
the Java apache foundation (language shouldn't be the problem here).

Here is a good example in using such a pattern:

http://onjava.com/pub/a/onjava/2004/12/22/jakarta-gems-1.html?page=2

Implement for each criteria the Predicate interface, signature like

public boolean evaluate(object o);

which code evaluates if the criteria is met. Next, you implement
an "executable" Closure interface with signature

public void execute(object o);

which code executes a closure on the object. You only have to marry
predicate and closure. Map each predicate to its closure:

HashMap predicateMap = new HashMap();
// predicateMap.put(isCategoryXY, doActionOn);

After that you can iterate over the Map with respect to the persons

,-------------------

foreach (Person person in persons)
{
foreach (Predicate predicate in predicateMap.Keys)
{
if (predicate.evaluate(person))
{
predicateMap[predicate].execute(person);
}
}
}

`--------------------

Consider, where to define the Predicates and the Closures. You could
do it probably generic (if there are not only Persons) and define the
Predicates and Closures as a partial class.

Please understand my posting only as an idea and not as a solution.
I didn't try it, yet. Maybe that's a wrong start (could be discussed
here). I just read the above article today and thought it could be
useful for your problem. Sorry, if I might got your questions wrong.

Best

Henning
 
P

Paul

Factory, Strategy and composite patterns will do exactly that. Just a non
working example below should giveyou an idea. This can be interspersed with
factory patterns to load rules dynamicaly/ initially based on sex.


interface IRegistrationRule
{
bool Register(Person person);
}

class AbstractRegistrationRule
{
bool Register(Person person);
}

class ChildrenRegistrationRule : AbstractRegistrationRule
{
bool Register(Person person)
{
Rule..................
}
}

class NoChildrenRegistrationRule : AbstractRegistrationRule
{
bool Register(Person person)
{
Rule..................
}
}

class FoodLikesRegistrationRule : AbstractRegistrationRule
{
bool Register(Person person)
{
Rule..................
}
}


class FoodDisLikesRegistrationRule : AbstractRegistrationRule
{
bool Register(Person person)
{
Rule..................
}
}

abstract class CompositeRegistrationRule: AbstractRegistrationRule
{
IList<IRegistrationRule> rules = new List<IRegistrationRule>;
bool Register(Person person);
}

class MaleRegistrationRule:CompositeRegistrationRule
{


}


class FeMaleRegistrationRule:CompositeRegistrationRule
{
public FeMaleRegistrationRule()
{
rules.Add (new FoodDisLikesRegistrationRule ());
rules.Add (new ChildrenRegistrationRule ());
}

bool Register(Person)
{

bool register = true;
foreach(IregistrationRule rule in rules)
{
register = rule.register(person)
if (register == false)
{
break;
}
}
return register;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top