Using Design patterns is one of the core element in making any application design robust and scalable.
I will give in this article few examples which are quite generic and useful for most of web development, These are my favorites and this one is quite challenging skill for any architect/developer to bring best design pattern in the application development. But few patterns are not very complex which can be discussed and may be few references I can mention to understand more about it.
One of my favorite pattern is strategy pattern. As shown below just there many features which can be selected based on the Execute strategy inputs. There are many other patterns are very useful in apps like singleton or facade. or factory.. and others. I need to start with some thing. One interesting example when I am learning this design pattern is one of Ecommerce product has a folder. If we move any code in that folder ex show recommendations function to that folder feature will be enabled in that ecommerce site .
As it is open source product I could go though implementation and finally saw the following code implementation but done exceptionally well.
There are many interesting facts about design pattens. In one product the return object / structure from Facade layer is BOject and company name is Business Objects. After couple of days of working with that code i realize that it is best way to implement Facade. So we can go though lot of these implementations one by one.
A strategy pattern is use to perform an operation (or set of operations) in a particular manner. In the classic example, a factory might create different types of Animals: Dog, Cat, Tiger, while a strategy pattern would perform particular actions, for example, Move; using Run, Walk, or Lope strategies.
In fact the two can be used together. For example, you may have a factory that creates your business objects. It may use different strategies based on the persistence medium. If your data is stored locally in XML it would use one strategy. If the data were remote in a different database, it would use another.
same way many operational patterns depends on deletgate
Several design patterns use delegation. The State (338), Strategy (349), and Visitor (366) patterns depend on it. In the State pattern, an object delegates requests to a State object that represents its current state. In the Strategy pattern, an object delegates a specific request to an object that represents a strategy for carrying out the request. An object will only have one state, but it can have many strategies for different requests. The purpose of both patterns
is to change the behavior of an object by changing the objects to which it delegates requests. In Visitor, the operation that gets performed on each element of an object structure is always delegated to the Visitor object.
So some times design can use multiple patterns for depends on use case.
class MealBuilder:
def __init__(self):
self.builder = Builder()
def prepare_veg_meal(self):
self.builder.build_part1("Salad")
self.builder.build_part2("Vegetable Curry")
self.builder.build_part3("Rice")
return self.builder.get_product()
def prepare_non_veg_meal(self):
self.builder.build_part1("Chicken Soup")
self.builder.build_part2("Grilled Chicken")
self.builder.build_part3("Chicken Tikka")
return self.builder.get_product()
Pizza pizza = new Pizza.Builder(12)
.cheese(true)
.pepperoni(true)
.bacon(true)
.build();
L Let us look in to more complex use cases and some of important aspects we need to consider while using these creational patterns.
No comments:
Post a Comment