Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

Now you need to develop a system in which there are different roles, the most common being administrator and employee roles, and then you need to distribute different permissions according to different roles

Get the requirements immediately after the code:

class Person {
    constructor(name, permission) {
	this.name = name;
	this.permission = permission; }}/**
 * 管理员
 */
class Admin extends Person {
    constructor(name, permission) {
        super(name, permission); }}/** * employee */
class Staff extends Person {
    constructor(name, permission) {
        super(name, permission); }}Copy the code

There should be other differences between administrators and employees, and the code above does not highlight the details for ease of understanding

Once you’ve created the base class, you can then instantiate the subclasses directly based on the role using the new keyword, and you’ll be happy with your functionality

const admin = new Admin('Administrator'['user'.'salary'.'vacation']);
const zs = new Staff('Joe'['vacation']);
const ls = new Staff('bill'['vacation']);
Copy the code

Looking at the code above, there are some questions:

  • The code is highly coupled and depends on the implementation of concrete classes

When I want to create a specific class, I need to pay attention to the specific class. For example, when I create a role with administrator rights, I need to directly create the new Admin, and the coupling with the class is too strong

  • The code is redundant

When I need to create an employee role, the parameter passed to the Staff stays the same and becomes name, resulting in a lot of duplicate code

  • It is not conducive to expansion and maintenance

As the business iterates, both new permissions and changing permissions for different roles can become a headache

Simple factory mode can solve the above problems well:

/** * type: role type - Administrator and employee * name: role name */
const Factory = (type, name) = > {
    switch (type) {
        case "admin": // Create an administrator
            return new Admin(name, ["user"."salary"."vacation"]);
        case "staff": // Create an employee
            return new Staff(name, ["vacation"]);
        default: // Robust processing
            throw new Error("Alter character creation is not currently supported"); }};console.log(Factory("admin"."Administrator"));
console.log(Factory("staff"."Zhang"));
console.log(Factory(""."Zhang"));
Copy the code

Through a process of function will create the object encapsulation, alone outside the factory pattern function called when creating objects don’t need to pay attention to how the characters of specific implementation, only responsible for transfer corresponding parameter when you call Like our own went to vegetable market to buy food cooking myself, we need to track the dish washing, chopping vegetables, cooking, plate, such as operation, And then you get a dish. When we go to a restaurant and order something and the chef cooks it, we don’t care how the chef cooks it, we end up with a dish on our table. The simple factory model is like going to a restaurant and ordering, right

Let’s look at UML for the simple factory pattern: