2016-05-17 57 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中被覆蓋,以根據各自的要求進行操作。

相關問題