开发者

.net design pattern question

I am trying to understand design pattern problems. I am trying to modify the code like this in winforms and trying to see if any design pattern suits my requirement. Please suggest which is the best design pattern in this scenario. This is very basic code containing 2 tab pages which might have different controls can be added dynamically and read out different files on click of particular tab. To elaborate more... I have written this code to learn and understand design pattern. This is just a scenario where user click on a particular tab which will show dynamic controls generated.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }


    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {


        if (tabControl1.SelectedTab.Name.Equals("tabPage1"))
        {
            GeneratedynamicControlsForTab1();
        }
        else if (tabControl1.SelectedTab.Name.Equals("tabPage2"))
        {
            GeneratedynamicControlsForTab2();
        }
    }

    private void GeneratedynamicControlsForTab1()
    {
        Label label1 = new Label();
        label1.Text = "Label1";
        tabPage1.Controls.Add(label1);

                }

    private void GeneratedynamicControlsForTab2()
    {
        tabPage1.Controls.Clear();
        Label label2 = new Label();
        label2.Text = "Label2";
        tabPage2.Controls.Add(label2);

                }



}

Please let me know if this below implementation is correct for above scenario Please let me know if this implementation is correct to modify above code to state pattern.

public partial class Form1 : Form
{
    void GenerateControl(iState state)
    {
        switch (state.value)
        { 
            case 1:
                GenerateControlforTab1();
                break;

            case 2:
                GenerateControlfo开发者_开发技巧rTab2();
                break;

        }
    }
    void GenerateControlforTab1()
    {

    }

    void GenerateControlforTab2()
    {

    }


private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {

     IState state = new State()

        if (tabControl1.SelectedTab.Name.Equals("tabPage1"))
        {
            state.value =1      ;      }
        else if (tabControl1.SelectedTab.Name.Equals("tabPage2"))
        {
            state.value =2;
        }
        this.GenerateControls();
    }
}

interface iState    {
    void GenerateControls();
}


MVC design pattern.


You have tried to implement the state pattern but there are some mistakes.One of the uses of State pattern is to remove conditional if statements from your code by creating state interface and implementing different states from it.But You've used switch statement instead of ifs.And in tabControl1_SelectedIndexChanged function you use ifs again. You've created the interface iState and directly instantiate it.But I think you should have to create some concrete implementation on the iState interface where the state codes should go to.You can take a look on "O'REILLY Head first design Patterns" chapter-10 for guidance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜