简体   繁体   English

如何在打字稿中创建装饰器图案?

[英]How to you create a decorator pattern in typescript?

I'm trying to create objects from a set of classes without having to define each set... essentially I'm trying to create the decorator pattern. 我正在尝试从一组类中创建对象,而不必定义每个组...本质上,我正在尝试创建装饰器模式。 In typescript this seems to be near impossible due to compiling restrictions. 在打字稿中,由于编译限制,这似乎几乎是不可能的。

I've tried using Proxies. 我已经尝试使用代理。 No dice. 没有骰子。

Here's the usage I'm trying to accomplish (some code is missing to allow what I'm trying to do - which is what I'm trying to solve). 这是我要完成的用法(缺少一些代码来允许我要执行的操作-这是我要解决的问题)。

class Person {

    public name:string;
    public age:number;

    public identify(){
        console.log(`${this.name} age ${this.age}`);
    }


}

class Child {

    public Mother:Person;
    public Father:Person;

    public logParents(){
        console.log("Parents:");
        this.Mother.identify();
        this.Father.identify();
    }

}

class Student {

    public school:string;

    public logSchool(){
        console.log(this.school);
    }

}

let Dad = new Person();
Dad.name = "Brad";
Dad.age = 32;

let Mom = new Person();
Mom = new Student(Mom);
Mom.name = "Janet";
Mom.age = 34;
Mom.school = "College of Night School Moms";

let Johnny = new Person();
Johnny = new Child(Johnny);
Johnny = new Student(Johnny);
Johnny.name = "Johnny";
Johnny.age = 12;
Johnny.Mother = Mom;
Johnny,Father = Dad;
Johnny.school = "School for kids who can't read good";

Using the java example here as the base, I changed it a bit to fit your code: 此处java示例为基础,我对其进行了一些更改以适合您的代码:

interface Person {
    name: string;
    age: number;
    identify(): void;
}

class SimplePerson implements Person {
    public name: string;
    public age: number;

    public identify(){
        console.log(`${this.name} age ${this.age}`);
    }
}

abstract class PersonDecorator implements Person {
    protected decoratedPerson: Person;

    public constructor(person: Person) {
        this.decoratedPerson = person;
    }

    public get name() {
        return this.decoratedPerson.name;
    }

    public get age() {
        return this.decoratedPerson.age;
    }

    identify(): void {
        return this.decoratedPerson.identify();
    }
}

class Child extends PersonDecorator {
    public Mother: Person;
    public Father: Person;

    public logParents(){
        console.log("Parents:");
        this.Mother.identify();
        this.Father.identify();
    }
}

class Student extends PersonDecorator {
    public school:string;

    public logSchool(){
        console.log(this.school);
    }
}

let Dad = new SimplePerson();
Dad.name = "Brad";
Dad.age = 32;

let Mom = new SimplePerson();
Mom = new Student(Mom);
Mom.name = "Janet";
Mom.age = 34;
(Mom as Student).school = "College of Night School Moms";

let Johnny = new SimplePerson();
Johnny = new Child(Johnny);
Johnny = new Student(Johnny);
Johnny.name = "Johnny";
Johnny.age = 12;
(Johnny as Child).Mother = Mom;
(Johnny as Child).Father = Dad;
(Johnny as Student).school = "School for kids who can't read good";

( code in playground ) 操场上的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM