
Freelance Programmer Father to 3 beautiful children @kellie_aurora Contact: Delvoid.dev@gmail.com https://xitter.cf/t.co/DIejukjA0c
England
Joined November 2010
- Tweets1.7K
- Following576
- Followers278
- Likes9.4K
The Builder Pattern is a creational design pattern that provides a flexible solution for constructing complex objects, especially when their construction needs to follow specific steps or configurations. It separates the construction of a complex object from its representation, enabling the same construction process to create various representations of the object.
Imagine constructing a complex object like a computer, where a variety of configurations (e.g., processor type, RAM size, storage size) are possible. Not all configurations are mandatory, and they can be selected in various orders, creating a challenge in object construction.
Before using the Builder Pattern:
After using the Builder Pattern:
Key Points
- Controlled Construction: The Builder Pattern provides controlled step-by-step construction of a complex object.
- Clear Representation: The client code remains clean and clear, unaffected by the complexity of the object construction.
- Flexible Configuration: Objects can be configured with various setups while keeping the construction process shielded from the client.
- Enhanced Scalability: The builder can be easily extended to incorporate new configurations or attributes without affecting the client code.
When to use:
The Builder Pattern is especially useful in the following scenarios:
- Complex Object Construction: When an object needs to be constructed under various configurations or through multiple steps.
- Separation of Concerns: When you want to separate the complex construction logic from the actual representation of the object.
- Immutable Object Creation: When creating an immutable object with various configurations without exposing the construction logic.
- Parameter Overload: To avoid constructors with a large number of parameters (some of them might be optional, which can further complicate the logic).
- Constructing Composite Structures: When building composite (tree-like) structures, ensure that each node is constructed correctly.
- Enhanced Readability: To enhance the readability of object creation when there are numerous parameters, especially when many of them have the same type.
Examples:
- Meal Preparation in Fast Food Systems: A meal might have various items (like a burger, drink, dessert) and each item might have several options.
- SQL Query Builder: A query could have multiple configurations (e.g., SELECT, WHERE, ORDER BY clauses) that can be added in various orders or combinations.
- Document Converters: Documents can be converted into various formats (PDF, Word, Text, etc.) using different converters.
- API Request Builder: For constructing API requests in SDKs, where each request might have numerous optional configurations (headers, query parameters, body, etc.).
Advantages:
- Improved Readability: The step-by-step method of chaining improves the readability of object creation.
- Increased Flexibility: Allows for constructing objects with a large number of possible configurations.
- Separation of Construction and Representation: The client code does not need to deal with the object’s construction logic.
Disadvantages:
- Complexity: This can introduce more classes and increase complexity, which might be unnecessary for simpler objects.
- Builder Synchronisation: The builder and the product classes might need to be modified or kept in sync, which can be cumbersome.
Conclusion:
The Builder Pattern serves as a robust solution for constructing complex objects, shielding the intricate construction logic from client code and offering a clear and straightforward interface for object creation. It is pivotal to recognise the scenarios where the Builder Pattern shines, particularly in cases involving numerous configurations, step-by-step construction, or when aiming to enhance code readability and maintainability. While the pattern offers numerous advantages, it is crucial to apply it judiciously, considering the potential overhead of implementing additional classes and managing synchronization between the Builder and the Product. Balancing the need for structured object creation with code simplicity and performance considerations will ensure the effective application of the Builder Pattern in software development.
#learningNotes
2
171
The Factory Pattern is a versatile design pattern that simplifies object creation, by providing a method that acts as a “factory” for creating objects. It abstracts the instantiation process, allowing the type of object designed to be determined at runtime.
Image creating a user authentication system where users have different roles like Admin, Members or Guest. Each role might have a distinct authentication mechanism and permissions.
Before using the Factory Pattern:
After using the Factory Pattern:
Key points
- Decoupling: Client code is decoupled from the object creation code, promoting modularity.
- Flexibility: New types/functions can be added seamlessly without altering existing code.
- Abstraction: The client code does not need to know about the concrete types or creation logic.
We can see that the Factory Pattern abstracts away the complexity and provides a cleaner, more maintainable and scalable solution. However, as with everything, it is crucial to apply it only where the benefits outweigh the introduced abstraction layer.
#programmingNotes
Continued playing around with streams and created a terminal animation using readable streams. Behold the swinging monkey, try it out yourself by running curl delvoid.live in your terminal
2
7
200
Played around with streams again this afternoon. Created a command line script that takes a file as input and compresses it using some of the different algorithms available in the zlib module
Never really had to use streams yet, so this has been fun to dive into
5
366
Created an encryption themed animation. Love the way it turned out
Check out the codepen
codepen.io/Delvoid/pen/GRPjR…
Had a blast learning and visualizing maze-generation algorithms and solving them. Pretty danm satisfying to watch









