How to identify appropriate Design Patterns to resolve code smells
I am involved with an application where there are a lot of coding issues (i.e code smells), such as, many if else conditions, hard coded data passing from one layer to another, etc..
I want to improve the code quality for the application. Can any one please tell me how I can identify that a particular Design Pattern is appropriate to resolve (or refactor) certain pieces of code?
Please let me know if the question is not clear.
Harshad Riswadkar, Sep 14, 2010
There are multiple ways in which u can handle the situation.
1. Refactor your code:
Review code and see where there is repetetive code. Create small methods and pass paramaters for small differences in repetive code.
2. Once you are finished and convinced that the code is refactored into private methods, u wud find that there needs to be refactoring across multiple classes. E.g similar code in multiple classes with small differences relvant to that specific class.
For this, create a separate class and move methods there. You would realize that either your classes containing common code need to inherit from this new class or need to have a refernce for it. Well, doing this is using design patterns.
3.Now in case if else statments consit of creating mutiple objects:
A a = new A();
B b = new B();
such a case calls for creating common class and inherting A and B from same. use factory to create object at runtime.
There are just few small samples, this may help you.
Nunu Kohli, Sep 14, 2010
Thanks alot Jacob I am already going with Factory pattern but need to know how to implement builder pattern on parallel of factory pattern.
Jul 30, 2010