


Introduction to the builder pattern of JavaScript design patterns_javascript skills
Builder Mode Instructions
1. Separate the construction of a complex object from its representation so that the same creation process can have different representations. This is called the builder pattern.
2. Description in object-oriented language, main role:
1>. The Builder interface class defines a unified and operable behavior of the builder [worker], which represents a complex structural object;
2>. ConcreteBuilder is used to create [implement] various forms of instance objects of Builder to represent different representations of Builder;
3>. The director is used to guide the execution process and form of the Builder instance, to be separated from the performance of the Builder instance, and to guide the Builder instance to create and generate product results according to a certain rule sequence;
4>. The result created by ResultObject will generate a result object; this is the result created by the specific creator according to the guidance of Director;
3. The builder model is actually a commander, a builder, and a client who uses the commander to call specific builders to work and obtain results from the specific builders;
4. Builder Mode, Simulation Scenario: [It’s good to see an example illustrating the description of Builder Mode]
Suppose a family wants to build a house, but the owner or other members of the family do not know how to build a house, so they have to hire a few workers. The team building the house also needs a foreman to mortgage the house. Build a house according to the owner’s ideas, and the foreman will design it according to the owner’s requirements and ask the workers how to do it;
The foreman said that the first step is to put up the overall frame of the house, the second step is to build the bedroom, the third step is to decorate the kitchen, the fourth step is to complete the living room construction and decoration, the fifth step...
The foreman does not do anything, but the specific builder must follow the foreman’s requirements, building the first step, the second step, until the entire house is completed;
The creator must have all the skills to create this house, that is, building the skeleton, decorating the bedroom, etc..., that is, what the builder does, or the ability he has, must be greater than or equal to what the commander requires. something, or ability;
That is, the commander is an organizer, and the builder provides skills;
5. In a weak language like JavaScript, there is no such thing as an interface, so just ignore the interface definition layer, directly create a specific builder, and then build a guidance class to call back the builder;
Example source code
1. Worker Builder X:
function workerBuilder() {
This.workOne = function() {
//Build the skeleton of the house
}
This.workTwo=function() {
//Build a bedroom
}
This.workThree=function() {
//Build a kitchen
}
This.workFour=function() {
//Build a living room
}
//....
This.getResult = function() {
//Complete the house
var house = new House();
//house.HouseFrame ...
Return house;
}
}
workBuilder is a specific builder class, workOne and Two are things to be done, building skeletons, etc.;
Of course, you can build several more workBuilders to represent workers. The method of executing each job is different; but the work content is the same;
2. Commander class
function Director() {
This.construct = function(builder) {
builder.workOne();
builder.workTwo();
builder.workThree();
builder.workFour();
//...
//The order of the above content can be set, and the work items can also be set
}
}
The guidance method under the commander class has a callback reference to the builder, which includes several or all of the builder's work content; the commander organizes and arranges what the builder workers have to do;
3. Product House
function House() {
This.HouseFrame = '';
This.Room = '';
This.Kitchen = '';
This.LivingRoom = '';
//...
}
4. How to use
var builder = new workBuilder();
var director = new Director();
director.construct(builder);
var house = builder.getResult();
The fourth step is equivalent to the customer: the house owner. The house owner asks the Director to build the house, but the foreman does not do anything, so he instructs the builders to build the house. Finally, the house owner obtains the construction from the workers. Nice house;
Other instructions
The builder mode is more suitable for situations where the content [abstraction] is complex and the actual scene performance is different, such as inconsistent work content or sequence; such as everyone's daily life process, and things like the above Examples of similar scenarios; through the instructor layer, you can reduce the number of situations where there are many similar workplaces, but the order of work rules is inconsistent; you can greatly reduce the construction abstraction of actual objects;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



In the Java framework, the difference between design patterns and architectural patterns is that design patterns define abstract solutions to common problems in software design, focusing on the interaction between classes and objects, such as factory patterns. Architectural patterns define the relationship between system structures and modules, focusing on the organization and interaction of system components, such as layered architecture.

The decorator pattern is a structural design pattern that allows dynamic addition of object functionality without modifying the original class. It is implemented through the collaboration of abstract components, concrete components, abstract decorators and concrete decorators, and can flexibly expand class functions to meet changing needs. In this example, milk and mocha decorators are added to Espresso for a total price of $2.29, demonstrating the power of the decorator pattern in dynamically modifying the behavior of objects.

1. Factory pattern: Separate object creation and business logic, and create objects of specified types through factory classes. 2. Observer pattern: allows subject objects to notify observer objects of their state changes, achieving loose coupling and observer pattern.

Design patterns solve code maintenance challenges by providing reusable and extensible solutions: Observer Pattern: Allows objects to subscribe to events and receive notifications when they occur. Factory Pattern: Provides a centralized way to create objects without relying on concrete classes. Singleton pattern: ensures that a class has only one instance, which is used to create globally accessible objects.

The Adapter pattern is a structural design pattern that allows incompatible objects to work together. It converts one interface into another so that the objects can interact smoothly. The object adapter implements the adapter pattern by creating an adapter object containing the adapted object and implementing the target interface. In a practical case, through the adapter mode, the client (such as MediaPlayer) can play advanced format media (such as VLC), although it itself only supports ordinary media formats (such as MP3).

TDD is used to write high-quality PHP code. The steps include: writing test cases, describing the expected functionality and making them fail. Write code so that only the test cases pass without excessive optimization or detailed design. After the test cases pass, optimize and refactor the code to improve readability, maintainability, and scalability.

The Guice framework applies a number of design patterns, including: Singleton pattern: ensuring that a class has only one instance through the @Singleton annotation. Factory method pattern: Create a factory method through the @Provides annotation and obtain the object instance during dependency injection. Strategy mode: Encapsulate the algorithm into different strategy classes and specify the specific strategy through the @Named annotation.

The advantages of using design patterns in Java frameworks include: enhanced code readability, maintainability, and scalability. Disadvantages include complexity, performance overhead, and steep learning curve due to overuse. Practical case: Proxy mode is used to lazy load objects. Use design patterns wisely to take advantage of their advantages and minimize their disadvantages.
