How to replace inheritance with interfaces in a factory template?

I dynamically add and replace controls on the winform panel at runtime

Despite the fact that all the work I was told to implement interfaces, and not inherit from baseUserControl.

I’m all for this, but I don’t see how I can achieve the same result using interfaces

How do I encode my Factory?

How can this be improved and use interfaces instead?

//Simplified noddy example

//Client code
var controlA = ControlFactory
    .Create("UserControlA") as UserControlA;

panel1.Controls.Add(ControlA);

//Factory
public class ControlFactory
{
    public static BaseUserControl Create(string name)
    {
        switch (name)
        {
            case "UserControlA":
                var userControlA = new UserControlA();

                return userControlA;

            case "UserControlB":
                var userControlB = new UserControlB();
                return userControlB;
        }
        return null;
    }
}
   //BaseUserControl
   public partial class BaseUserControl : UserControl
    {
        public BaseUserControl()
        {
            InitializeComponent();
        }

        public virtual void DoSomething()
        {

        }
    }

    public partial class UserControlA : BaseUserControl
    {
        public UserControlA()
        {
            InitializeComponent();
        }

        public override void DoSomething()
        {
            //Do something here
        }
    }

public partial class UserControlB : BaseUserControl
{
    public UserControlB()
    {
        InitializeComponent();
    }

    public override void DoSomething()
    {
        //Do something here
    }
}
+5
source share
1 answer

Here's how you can do it:

using System;
using System.Windows.Forms;
using System.ComponentModel;

//Interface
public interface IControl : IComponent
{
    void DoSomething();
}

//Factory
public class ControlFactory
{
    public static IControl Create(string name)
    {
        switch (name)
        {
            case "UserControlA":
                var userControlA = new UserControlA();

                return userControlA;

            case "UserControlB":
                var userControlB = new UserControlB();
                return userControlB;
        }
        return null;
    }
}

//BaseUserControl
public partial class BaseUserControl : UserControl, IControl
{
    public BaseUserControl()
    {
        InitializeComponent();
    }

    public virtual void DoSomething()
    {

    }
}

public partial class UserControlA : BaseUserControl, IControl
{
    public UserControlA()
    {
        InitializeComponent();
    }

    public override void DoSomething()
    {
        //Do something here
    }
}

public partial class UserControlB : BaseUserControl, IControl
{
    public UserControlB()
    {
        InitializeComponent();
    }

    public override void DoSomething()
    {
        //Do something here
    }
}

You can save BaseUserControlif you have any functions common to UserControlAand UserControlB; otherwise, eliminate it and make the last two obtained directly from UserControl.

, IControl. , UserControl. .

//Interface
public interface IControl : IComponent
{
    void DoSomething();

    // To be inherited from UserControl.
    Size Size { get; set; }
    bool Focus();
    event EventHandler FontChanged;
}

Windows Forms - , Control.ControlCollection.Add - Control , . ; , . , :

//Interface
public interface IControl : IComponent
{
    void DoSomething();

    Control AsWindowsForms();
}

//BaseUserControl
public partial class BaseUserControl : UserControl, IControl
{
    public BaseUserControl()
    {
        InitializeComponent();
    }

    public virtual void DoSomething()
    {

    }

    public Control AsWindowsForms()
    {
        return this as Control;
    }
}

:

var controlA = ControlFactory.Create("UserControlA").AsWindowsForms();
var controlB = ControlFactory.Create("UserControlB").AsWindowsForms();
panel1.Controls.Add(controlA);
panel1.Controls.Add(controlB);
+7

All Articles