【问题标题】:How to you create a decorator pattern in typescript?如何在打字稿中创建装饰器模式?
【发布时间】:2016-11-04 18:18:49
【问题描述】:

我正在尝试从一组类中创建对象,而不必定义每个类...基本上我正在尝试创建装饰器模式。在打字稿中,由于编译限制,这似乎几乎是不可能的。

我尝试过使用代理。没有骰子。

这是我想要完成的用法(缺少一些代码来允许我正在尝试做的事情 - 这是我想要解决的问题)。

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";

【问题讨论】:

    标签: javascript typescript proxy decorator


    【解决方案1】:

    使用java example here 作为基础,我对其进行了一些更改以适合您的代码:

    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)

    【讨论】:

    • 对。我错过了,在 java 示例中,它们具有私有方法而不是公共方法,这就是为什么它不是问题的原因。但是这个问题存在于 java 中,可能还有其他 OO 语言。基本上,您正在寻找带有 mixins 的装饰器模式。这在打字稿/javascript中是不可能的,无论有没有代理,但我怀疑这可能有点矫枉过正,至少在大多数情况下是这样。你的场景是什么?你的代码只是你的问题的一个例子还是你的真实模型?
    • 这只是一个简化的例子。真实场景还有更多......基本上是一个带有一系列“扩展”的基础对象,可以以任意数量的组合进行混合和匹配。
    • 怎么会有 0(现在是 1)赞成票?这是一个很好的例子和答案!
    猜你喜欢
    • 2021-05-22
    • 2015-06-28
    • 1970-01-01
    • 2018-06-21
    • 2019-07-29
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    相关资源
    最近更新 更多