Vajda
Vajda

Reputation: 1794

How should I implement interface composition?

I have this situation:

public interface IHasValue<T>
    {
        T Value { get; set; }
    }

public interface IClickable
    {
        void SubscribeOnClick(EventHandler click);
    }

public interface ILoginView : IView
    {
        IHasValue<string> Username { get; }
        IHasValue<string> Password { get; }
        IClickable Login { get; }
        IClickable Cancel { get; }
    }

public partial class LoginVIew : Form, ILoginView
    {
        public LoginVIew()
        {
            InitializeComponent();
        }

        #region ILoginView Members

        public IHasValue<string> Username
        {
            get { ?? }
        }

        public IHasValue<string> Password
        {
            get { ?? }
        }

        public IClickable Login
        {
            get { ?? }
        }

        public IClickable Cancel
        {
            get { ?? }
        }

        #endregion
    }

How should I implement this? I have no idea.

Upvotes: 0

Views: 2342

Answers (1)

David Neale
David Neale

Reputation: 17028

Well you'll need to implement IHasValue<T> and IClickable in order to be able to create an implementation of ILoginView (because you need to be able to create instances of object implementing those interfaces in the ILoginView members.

 public class StringValue : IHasValue<string>
 {
     public string Value { get; set; }
 }

 public interface LoginClickable : IClickable
 {
     public void SubscribeOnClick(EventHandler click)
     {
          // do something to login
     }
 } 

 public interface CancelClickable : IClickable
 {
     public void SubscribeOnClick(EventHandler click)
     {
          // do something to cancel
     }
 } 

...

    public IHasValue<string> Username
    {
        get { return new StringValue { Value = "Username" }; }
    }

    public IHasValue<string> Password
    {
         get { return new StringValue { Value = "Password" }; }
    }

    public IClickable Login
    {
        get { return new LoginClickable(); }
    }

    public IClickable Cancel
    {
        get { return new CancelClickable(); }
    }

Upvotes: 3

Related Questions