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();
}
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();
}
}
I'm afraid I no longer use this forum due to the new point allocation system.
Member
3 Points
11 Posts
Implement fewer members of an Interface
Feb 04, 2015 04:47 AM|dienguis|LINK
Hi, I have an Interface
I have two classes :
DesktopDomainFactory implement IAppDomainFactory and is use as DeskTop
WebDomainFactoryimplement IAppDomainFactory and is use as Web
So I would like DesktopDomainFactory to implement only GetDesktopDomain and WebDomainFactory to implement only GetWebDomain.
So this functions must be removed from my classes
Best Regards
All-Star
37441 Points
9076 Posts
Re: Implement fewer members of an Interface
Feb 04, 2015 07:56 AM|AidyF|LINK
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