Design pattern is typically description of the common problems and approaches to solve those problems. It is most important piece of knowledge to acquire if you are developing applications. Most of the times, we do tend to ignore design. The latest gangs of developers just want to finish off writing code as soon as possible. The existence of classic development IDEs like VS2005, just adds to this. People just write down the application without having a proper look at the application/product vision. Design patterns are typically divided in groups like: creational, structural, behavioral etc. I do not want to stress on these groups. Instead, I will be concentrating on few patterns.
Singleton Pattern: This is the widely popular design pattern. In most of the interviews, interviews will definitely ask you to write down the class using singleton pattern. This pattern makes sure that only one instance of the class is present at any point of time. This is very simple to implement. BUT do make sure that your class is thread-safe. You may want to use mutex or lock in case of C# or synchronized keyword to the method declaration in case of Java.
Factory Method and Factory Type: Factory Method allows a class to defer instantiation to its subclasses. If you are using .NET Framework, then File.Open could be example of the Factory method. Factory type is also called as abstract factory. It allows creation of families of related classes without specifying their concrete classes. Database Factory in .NET 2.0 would be the best example for this. I have used both the patterns liberally whenever I wanted to detach the object creation mechanism from the caller. There are so many classes in .NET which have implemented these patterns themselves.
Façade Pattern: It actually defines high-level interface making subsystem easier to use. Imagine a scenario that one of your team member responsible for business logic layer, has left. Later you realize that to execute one functionality, you have to call 4 different functions and instantiate 3 different classes. Your UI guys will not like this obviously. You may think of using façade pattern here and simplifying things for UI developers. Trust me they will pray for you. ;)
Adapter Pattern: This pattern is typically used to remove the incompatibility issues between different classes. It typically converts one interface into the interface that is expected by the other class. I like one example which I have read from one of the books on design patterns. Consider that you have been to use new Framework B for communication with Framework C. You already have Framework A for talking to Framework B. And now your CEO told you to use Framework B as well because it is developed by his nephew. This is the situation where you can use adapter pattern. How? I will left it to the readers. ;)
Flyweight pattern: This pattern mainly focuses on reusability. Instead of having large objects, you can use a set of common objects called as Flyweights. You can take the common functionalities in put in these flyweight objects. It is mainly used for efficient sharing.
Apart from these there are other popular patterns like: observer, mediator, chain of responsibility, proxy pattern etc.
It is good to know these patterns. So, next time when you design something or write code, you know what patterns to use.
Cheers,
Amol
No comments:
Post a Comment