Preface
The combination design model is used to form a whole by combining multiple parts. For example, when we go to a meal, we order a portion of rice and a portion of fish-flavored shredded pork. These things can be regarded as a part. Through combination, we can form a new product, fish-flavored shredded pork rice bowl. This is the combination design model.
Business scenarios of combination design patterns
Forms are used to collect user data, such as login, registration, or filling in some information. Since these forms are similar, if we do them one by one, they are full of repetitive things and increase the workload, we can use the combination design model.
Small case of combination design pattern
The operation of a restaurant requires some hiring of some personnel, but these hiring personnel have one thing in common. We need to pay a certain salary as a reward for labor. Secondly, they also need to bear certain tasks and responsibilities. They may need to report the restaurant situation to someone, or they may have subordinates, etc. Let's do it.
Declare an employee category. We need to provide three parameters for newly recruited employees, name of the employee, salary of the employee and position of the employee. We can also obtain the employee's name, obtain and modify the employee's salary, obtain the employee's current position and transfer the employee's position.
class Staff { //Employee name Employee salary Employee position constructor(name, salary, position) { = name; = salary; = position; } // Get the employee's name getName() { return ; } //Modify employee salary setSalary(salary) { = salary; } // Obtain employee salary getSalary() { return ; } // Set up employee positions setPosition(position) { = position; } // Obtain employee positions getPosition() { return ; } }
To declare a store category, the store category is to add newly recruited employees to the store staff, and to view the position information of the store staff and to count the current employment costs.
class StoreMembers { constructor() { //Shop staff = []; } // Add employees to the store chief staff addEmployee(employee) { (employee); } // Obtain the total expenses of the store staff getNetSalaries() { let netSalary = 0; netSalary = ((total, currt) => () + ()) return netSalary; } // Get the information list of store staff getPositionList() { return ((res => ({ name: (), position: (), salary: () }))) } }
We add newly recruited employees to the store chief staff and obtain the total expenses of the store for employees and obtain the store chief staff information list.
// Create two employee class instances const xh = new Staff("Little Red", 12000, 'waiter'); const xm = new Staff("Xiao Ming", 10000, 'Cashier'); // Create a store const storeMembers = new StoreMembers(); //Add new staff to the store (xh); (xm); //Get the total salary of the store staff ("Total staff salary:", ()); //Get the total position information of the store staff ("Staff General Position Information:", ());
Combination design pattern makes the interrelated data structural. Whether it is intuitive to see or modify the relationship between the current data, you only need to care about the current lower-level data, which reduces the complexity between the data and improves the maintainability of the code.
This is the article about JavaScript design pattern combination design pattern cases. For more related JavaScript combination pattern content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!