2012-09-12 18 views
0

比方說你有很多class Section S的每一個都包含class Item小號你會怎麼設計這個多重選擇系統

我想創造的功能,允許多個事物的選擇清單。我想要有能力一次選擇多件事。如果選擇了Item,則其父母Section也將被聚焦/選擇。

而且,爲了更進一步,如果某些無關的類需要修改所選部分/項目的屬性將會怎樣。你如何設計這種功能而不違反封裝的OOP原則。你會創建一個SelectionHandler或什麼?

不尋找實際的實現,大多數只是你會使用的抽象設計/結構。

+0

爲科和項目特定的屬性,這可能更好地工作,你給一個真實的使用情況下,如果正是你正在嘗試做的。 – PeeHaa

回答

1

因此可以選擇Section和Item。創建一個接口,他們都將實施:

interface ISelectable 
{ 
    void Select(); 
    void DeSelect(); 
} 

現在,貨號:

class Item implements ISelectable 
{ 
    private ISelectable _section; // The object that contains this item 
    private bool _selected; 


    public Item() 
    { 
     _selected = false; 
    } 

    public void Select() 
    { 
     _selected = true; 
     _section.Select(); 
    } 

    public void DeSelect() 
    { 
     _selected = false; 
     _section.DeSelect(); 
    } 

    public void setSection(ISelectable section) 
    { 
     _section = section; 
    } 
} 

所以,當我們[德]選擇一個項目,它的父(段)也[德]中選擇。該科:

class Section implements ISelectable 
{ 
    private bool _selected; 
    public ArrayList Items; 

    public Section() 
    { 
     _selected = false; 
     Items = new ArrayList(); 
    } 

    public void addItem(Item i) 
    { 
     i.setSection(this); 
     Items.add(i); 
    } 

    public void Select() 
    { 
     _selected = true; 
    } 

    public void DeSelect() 
    { 
     _selected = false; 
    } 
} 

到目前爲止,我們已經建立了一個系統,當選擇它的一個孩子,將選擇父。

可以定義使用「規則/模式」

private PropertyType _propertyName; 

    public void setPropertyName(PropertyType propertyName) 
    { 
     _propertyName = propertyName; 
    } 

    public PropertyType getPropertyName() 
    { 
     return _propertyName; 
    }