locked
Implement fewer members of an Interface RRS feed

  • Question

  • User-748049670 posted

    Hi, I have an Interface

     public interface IAppDomainFactory
        {
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            DesktopApp GetDesktopDomain();
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            WebApp GetWebDomain();
        }
    
    

    I  have  two  classes :

    DesktopDomainFactory implement IAppDomainFactory and is use as DeskTop

      public class DesktopDomainFactory<T> : IAppDomainFactory where T : DesktopApp, new()
        {
            public T _domain { get; set; }
    
            public DesktopDomainFactory()
            {
                _domain = new T();
            }
    
            public DesktopApp GetDesktopDomain()
            {
                return DeskTopService.GetDomain();
            }
    
            public WebApp GetWebDomain()
            {
                throw new NotImplementedException();
            }
        }

    WebDomainFactoryimplement IAppDomainFactory and is use as Web

     public class WebDomainFactory<T> : IAppDomainFactory where T : WebApp, new()
        {
            public T _domain { get; set; }
    
            public WebDomainFactory()
            {
                _domain = new T();
            }
    
            public DesktopApp GetDesktopDomain()
            {
                throw new NotImplementedException();
            }
    
            public WebApp GetWebDomain()
            {
                return WebAppService.GetDomain();
            }
        }

    So I would like DesktopDomainFactory to implement only GetDesktopDomain  and WebDomainFactory to implement only GetWebDomain.

    So this functions must be removed from my classes

     public DesktopApp GetDesktopDomain()
     {
          throw new NotImplementedException();
     }
    
     public WebApp GetWebDomain()
     {
          throw new NotImplementedException();
     }
    
    

    Best Regards

    Wednesday, February 4, 2015 4:47 AM

Answers

  • User-760709272 posted

    You can't selectively implement methods on an interface, an interface is a contract, a guarantee that those methods are available to the calling code.  If you want to make your code more generic you might need to do some additional abstraction

    public interface IApp
    {
        string SayHello();
    }
    
    public class WebApp : IApp
    {
        public string SayHello()
        {
            return "Hello from web app";
        }
    
        public void WebAppOnlyMethod()
        {
        }
    }
    
    public class DesktopApp : IApp
    {
        public string SayHello()
        {
            return "Hello from desktop app";
        }
    
        public void DesktopAppOnlyMethod()
        {
        }
    }
    
    public interface IAppDomainFactory<T> where T : IApp
    {
        T GetDomain();
    }
    
    public interface IDomainService<T>
    {
        T GetDomain();
    }
    
    public class DesktopService<T> : IDomainService<T> where T : class, IApp
    {
        public T GetDomain()
        {
            return new DesktopApp() as T;
        }
    }
    
    public class WebService<T> : IDomainService<T> where T : class, IApp
    {
        public T GetDomain()
        {
            return new WebApp() as T;
        }
    }
    
    public class AppDomainFactory<T> : IAppDomainFactory<T> where T : class, IApp, new()
    {
        private T _domain { get; set; }
        private IDomainService<T> _service { get; set; }
    
        public AppDomainFactory()
        {
            _domain = new T();
    
            // you could use DI to inject the correct service into the constructor
            // rather than using "if" statements
    
            if (typeof(T) == typeof(DesktopApp))
            {
                _service = new DesktopService<T>();
            }
            else if (typeof(T) == typeof(WebApp))
            {
                _service = new WebService<T>();
            }
        }
    
        public T GetDomain()
        {
            _domain = _service.GetDomain();
            return _domain;
        }
    }
    
    public class MyClient
    {
        public MyClient()
        {
            AppDomainFactory<DesktopApp> fd = new AppDomainFactory<DesktopApp>();
            IApp app = fd.GetDomain();
            System.Diagnostics.Debug.WriteLine(app.SayHello());
    
            if (app is DesktopApp)
            {
                DesktopApp da = app as DesktopApp;
                da.DesktopAppOnlyMethod();
            }
    
            AppDomainFactory<WebApp> fw = new AppDomainFactory<WebApp>();
            app = fw.GetDomain();
            System.Diagnostics.Debug.WriteLine(app.SayHello());
    
            if (app is WebApp)
            {
                WebApp da = app as WebApp;
                da.WebAppOnlyMethod();
            }
    
            // or
            DesktopApp desktopApp  = fd.GetDomain();
            System.Diagnostics.Debug.WriteLine(desktopApp.SayHello());
            desktopApp.DesktopAppOnlyMethod();
    
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 4, 2015 7:56 AM