Home  /  Questions  /  Question

50   50
Aug 29, 2011

Help to decide appropriate pattern to be implement.

What is the best solution to resolve the following problem?

Problem: I would like to implement generic business logic (method) which can be invoked by any methods irrespective input type.
        For example: 
                Generic method:
                                public <complexType> GenericMethod(<ComplexType>)
                                                ... business logic implementation
                                                ... return <complexType>
                Client Method 1:
                                public <complexType> Client1(<complexType1>)
                                                ... call GenericMethod<complexType1>)
                Client Method 2:
                                public <complexType> Client1(<complexType2>)
                                                ... call GenericMethod<complexType2>)

Question: If you have noticed, In above scenario "Client Method1" and "ClientMethod2" is passing different input type and method is expecting same return type.

Is there any pattern or mechanism which will decide type and based on that it will invoke appropriate method.

Aug 30, 2011
Hi Nirav

I would suggest Strategy pattern where you can use different strategy methods based on the context.

Thanks & Regards
Pubudu Thilakarathna