2016-05-17 20 views
0

我想创建一个宏项目。这个宏项目将是可编辑的。哪个模式适用于具有依赖于其他属性的两个属性

每个宏项目都有一个类型和一个eventValue。

有三类项目(pressKey,releaseKey,delayInMs)

对于pressKeyEvent,我想的是,用户只能选择一个作为keyObject的eventValue的releaseKeyEvent。

对于delayEvent,我希望用户只能选择一个整数作为eventValue。

现在我有这个

export enum MacroEventEnum { 
    pressKey, 
    releaseKey, 
    delayInMs 
} 

export class MacroItem { 
    // This represent the type of the macro event 
    public macroEvent: MacroEventEnum; 
    // This represent the value associate with the macro event 
    public macroEventValue: any; 

    constructor(macroEvent: MacroEventEnum, macroEventValue: any) { 
     this.macroEvent = macroEvent; 
     this.macroEventValue = macroEventValue; 
    } 
} 

的问题是,当用户改变macroEvent的类型是pressKey,它仍然可以使用的时间内macroEventValue。

在这种情况下应该使用什么样的模式知道用户可以随时更改macroEvent。

感谢您的建议:)

+0

“知道用户可以随时更改itemEvent”是什么意思?另外,这功课呢? –

+0

更精确吗? @AlexHall – stephanec

+0

我不知道这是什么语言,所以我会假装它是Java。你打算让用户写'MacroItem item = new MacroItem(pressKey,enterKey); item.macroEvent = delayInMs;'?这是一个非常糟糕的主意。你应该让这个类不可变。 –

回答

0

你需要的类层次:MacroItemKeyMacroItemDelayMacroItem,其中KeyMacroItemDelayMacroItem无论从MacroItem继承。

MacroItem具有macroEvent属性,KeyMacroItem具有keyObject属性附加地,并DelayMacroItem具有delayValue属性附加。您可以在MacroItem中使用虚拟方法,该方法在KeyMacroItemDelayMacroItem中被覆盖,以根据各自的要求进行操作。

相关问题