Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which design pattern is suitable for my problem

 
jing hu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Java Monad Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One solution would be to represent those processes as functions/commands and composing them in a collection and pass it around.

 
jing hu
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
Java Monad Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic