Thursday, July 10, 2025

Design Uplift: VII - Using Design patterns Part 1

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.


In my project talash.azurewebsites.net User action processing implemented using strategy.  Any user action from different web clients will post data to service bus. till that point it is common code.  When processing different user actions like "Like", "delete" "comment" etc. There are many common tasks like logging , calculate rank and other things which are common to assets ,  so they are part of base strategy class and the only code functionality where  other than updating db will be  passed to the stagy derived class and executed which is very specific to User action. 

Even though it looks like just switch statement on the user action but when code organized in to above class diagram it is much cleaner and modular. User action processing will be dynamic addition just by adding the processing logic required for new action.

Let us look at Builder pattern which is creational pattern and mostly used as well.

 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.


Above explanation given just to differentiate builder pattern with strategy pattern. some use cases we may implement both together as well. So now we can focus on Builder pattern which is very common creational patterns used. 

Here is classic example of builder pattern

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()

The Builder Pattern is a creational design pattern used to construct complex objects step by step. It will be complex to just doing every thing in constructor to construct an object. By following builder pattern it is possible to depend on few methods of the builder to do construct object. The flow and methods used to build object will differ. 

Above example is simple one. But complex objects can be build 

Pizza pizza = new Pizza.Builder(12)
                       .cheese(true)
                       .pepperoni(true)
                       .bacon(true)
                       .build();
Builder's setter methods return the Builder object they are able to be chained. Pizza object construction done using builder class.

There are many practical examples we come across for this pattern.

NET StringBuilder class is a great example of builder pattern. It is mostly used to create a string in a series of steps. The final result you get on doing ToString() is always a string but the creation of that string varies according to what functions in the StringBuilder class were used. To sum up, the basic idea is to build complex objects and hide the implementation details of how it is being built.
Sql Query Builder.  API like fluent also uses above pattern to build the fluent API and pass different values to construct .

I like the following summrization of creational patterns 

 


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

Design Uplift VIII - Trends and Analytics

Design Objectives: 1. User should be able to see trending content as landing page content. Which also should consider user preferences. 2. U...