Which Design Pattern Is Suitable

Now I have a class A. 
I need to procee A with different method on the basis of some attributes of A. for example:

Code: 

class A 

string key; 
string value; 

 
class A
{
string key;
string value;
}
 

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 suits my question?

Solution:

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

Code:

//interface representing the command/function. Accepts one argument of type T and returns an object of type R 
interface Function1<T, R> { 
  R apply(T arg0); 

class A { 
  final private String key; 
  final private String value; 

  public A(String key, String value) { 
    this.key = key; 
    this.value = value; 
  } 

  public String getKey() { 
    return key; 
  } 

  public String getValue() { 
    return value; 
  } 

/*...Ignored boilerplate codes ...*/ 

public static void main(String[] args) { 
    //Wrap and create composition over methods process1, process2 ... process5 

    Function1<A, A> process1 = new Function1<A, A>() { 
      @Override public A apply(A a) { 
        System.out.println("am executing process1"); 
        // Call some process1(a) and return an object of A 
        return null; 
      } 
    }; 
    Function1<A, A> process2 = new Function1<A, A>() { 
      @Override public A apply(A a) { 
        System.out.println("am executing process2"); 
        // Call some process2(a) and return an object of A 
        return null; 
      } 
    }; 
    Function1<A, A> process3 = new Function1<A, A>() { 
      @Override public A apply(A a) { 
        System.out.println("am executing process3"); 
        // Call some process3(a) and return an object of A 
        return null; 
      } 
    }; 
    Function1<A, A> process4 = new Function1<A, A>() { 
      @Override public A apply(A a) { 
        System.out.println("am executing process4"); 
        // Call some process4(a) and return an object of A 
        return null; 
      } 
    }; 
    Function1<A, A> process5 = new Function1<A, A>() { 
      @Override public A apply(A a) { 
        System.out.println("am executing process5"); 
        // Call some process5(a) and return an object of A 
        return null; 
      } 
    }; 

    Map<String, List<Function1<A,A>>> mapping = new HashMap<String, List<Function1<A,A>>>(); 
    mapping.put("a", Arrays.asList(process1, process2, process5)); 
    mapping.put("b", Arrays.asList(process1, process2)); 
    mapping.put("e", Arrays.asList(process1, process3, process5)); 
    mapping.put("f", Arrays.asList(process1, process4, process5)); 

    A a = new A("a", "value"); 
    executeProcesses(mapping.get(a.getKey()), a); 

    /* Output: 
    am executing process1 
    am executing process2 
    am executing process5 
    */ 
  } 

  public static void executeProcesses(List<Function1<A, A>> processes, A a) { 
    for(Function1<A, A> process: processes) { 
      process.apply(a); //If you want, you can collect the result 
    } 
  } 

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.