简体   繁体   中英

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:

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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