This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes which design pattern is suitable for my problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "which design pattern is suitable for my problem" Watch "which design pattern is suitable for my problem" New topic
Author

which design pattern is suitable for my problem

jing hu
Ranch Hand

Joined: Jun 30, 2009
Posts: 63
Now I have a class A.
I need to procee A with different method on the basis of some attributes of A. for example

And I have five method,
A process1(A&)
A process2(A&)
A process3(A&)
A process4(A&)
A process5(A&)

when A attribute key is "a", I need to process A with {process1, process2, process5};
when A attribute key is "b", I need to process A with {process1, process2};
when A attribute key is "e", I need to process A with {process1, process3, process5};
when A attribute key is "f", I need to process A with {process1, process4, process5};

And I have a config file to record these rules.

So I want to know which design pattern is suit my question? I want to write elegant program ^__^

Thanks very much.



Marimuthu Madasamy
Ranch Hand

Joined: Jun 07, 2007
Posts: 72

One solution would be to represent those processes as functions/commands and composing them in a collection and pass it around.



- Marimuthu Madasamy
jing hu
Ranch Hand

Joined: Jun 30, 2009
Posts: 63
Marimuthu Madasamy,Thanks your detail reply.
I have read you code carefully. And I have understood the code.
But I have a question.

the rules which process class is need to process the class A is in a config file. I need to read the rules from a config file.
after loading the rules, In Java I know I can user class.forName("process1") to get a instance to process A.
But My code is based on c++, I can't use class.forName("process1") to create a object just by className in runtime.
the rules which class is need is in the config file.

So I want to know How can i create the wanted class to process A in runtime after loading the config file.

Thanks very much!!!
Marimuthu Madasamy
Ranch Hand

Joined: Jun 07, 2007
Posts: 72

The solution which I posted above would just fit. If you have five classes to process an object of class A, lets say, MyClass1, MyClass2, MyClass3, MyClass4, MyClass5. With reference to my above solution, when you create 'Function's, inside apply() method you need to create an instance of one of those classes and forward to a method on those class.

When you read rules from your properties file, you need to put appropriate 'Function's to a map as in the above solution. Since we have composed appropriate instances of one of the above classes inside 'Function1' instances, the function instances will forward to the method on the required class.

If all your classes share a common interface to process Class A objects, you don't need to wrap it in another interface like 'Function1'; you can just create a map on that interface to define rules.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which design pattern is suitable for my problem
 
Similar Threads
Displaying Two Buttons in one Row from HashMap
I/O binary piping ? Please Help !
Callback references
Showing output from Runtime.getRuntime().exec
java programming