7 Aralık 2014 Pazar

How to choose and apply a Design Pattern

You can choose from many design patterns, so how do you identify which one is appropriate for your problem? To know which design pattern to use and how to apply the solution template to your specific problem, it’s important to understand these guidelines.
  • You can’t apply patterns without knowing about them. The first important step is to expand your knowledge and study patterns and principles both in the abstract and concrete form. You can implement a pattern in many ways. The more you see different implementations of patterns, the more you will understand the intent of the pattern and how a single pattern can have varying implementations.
  • Do you need to introduce the complexity of a design pattern? It’s common for developers to try to use a pattern to solve every problem when they are studying patterns. You always need to weigh the upfront time needed to implement a pattern for the benefit that it’s going to give. Remember the KISS principle: Keep It Simple, Stupid.
  • Generalize your problem; identify the issues you’re facing in a more abstract manner. Look at how the intent of each pattern and principle is written, and see if your problem fits with the problem that a particular pattern or principle is trying to solve. Remember that design patterns are high-level solutions; try to abstract your problem, and don’t focus too hard on the details of your specific issue.
  • Look at patterns of a similar nature and patterns in the same group. Just because you have used a pattern before doesn’t mean it will always be the correct pattern choice when solving a problem.
  • Encapsulate what varies. Look at what will likely change with your application. If you know that a special offer discount algorithm will change over time, look for a pattern that will help you change it without impacting the rest of your application.
  • After you have chosen a design pattern, ensure that you use the language of your pattern along with the language of the domain when naming the participants in a solution. For example, if you are using the strategy pattern to provide a solution for costing various shipping couriers, name them accordingly, such as FedExShippingCostStrategy. By using the pattern’s common vocabulary along with the language of your domain, you will immediately make your code more readable and understandable to other developers with patterns knowledge.
When it comes to design patterns, there is no substitute for studying. The more you know about each of the design patterns, the better equipped you will be at applying them. Scan the intent of each pattern to refresh your memory when you have a problem and are looking for a solution.

A great learning exercise is to try to identify patterns in the .NET Framework. For example, the ASP.NET Cache uses the Singleton pattern; creating a new Guid uses the Factory pattern; the .NET 2 XML classes use the Factory pattern whereas version 1.0 did not.


Hiç yorum yok: