Classe di base o interfaccia astratta? Nessuno dei due sembra giusto
-
03-07-2019 - |
Domanda
Dato il seguente codice:
using System.Collections.Generic;
static class Program {
static void Main() {
bar Bar = new bar();
baz Baz = new baz();
System.Console.WriteLine(
"We have {0} bars, rejoice!", bar.Cache.Count);
}
}
public abstract class foo {
public static List<foo> Cache = new List<foo>();
}
public class bar : foo {
public bar() { Cache.Add(this); }
}
public class baz : foo {
public baz() { Cache.Add(this); }
}
Ottieni l'output (un po 'atteso) " Abbiamo 2 barre, rallegrati! " ;.
Questo è favoloso, ora abbiamo il doppio dei posti in cui avere la nostra birra (apparentemente), ma quello che voglio davvero è che ogni classe abbia la propria cache. Il motivo per cui non voglio solo implementare questa cache nella sottoclasse è perché ho anche alcuni metodi nella mia classe astratta che devono essere in grado di operare sulla cache (vale a dire, per scorrere su tutti loro). C'è un modo per fare questo? Ho esaminato l'utilizzo di un'interfaccia per foo
, ma l'interfaccia non consente di definire membri statici come parte dell'interfaccia.
Soluzione
Ogni classe derivata di foo dovrebbe definire come / dove ottenere una cache, così ognuno può (potenzialmente) avere la propria cache. I metodi in foo possono fare riferimento a GetCache () senza che l'implementazione sia nota.
public abstract class foo
{
public abstract ICache GetCache();
public void DoSomethingToCache()
{
ICache cache = this.GetCache();
cache.DoSomething();
}
}
public class bar : foo
{
public static ICache BarCache = new FooCache();
public override ICache GetCache()
{
return bar.BarCache;
}
}
public class FooCache : ICache { }
Altri suggerimenti
Utilizza una classe base generica parametrizzata con la sottoclasse:
using System.Collections;
using System.Collections.Generic;
static class Program
{
static void Main()
{
bar Bar = new bar();
baz Baz = new baz();
System.Console.WriteLine(
"We have {0} bars, rejoice!", Bar.GetCache().Count);
}
}
public abstract class foo<T>
{
private static List<foo<T> > Cache = new List<foo<T> >();
public IList GetCache()
{
return Cache;
}
}
public class bar : foo<bar>
{
public bar() { GetCache().Add(this); }
}
public class baz : foo<baz>
{
public baz() { GetCache().Add(this); }
}
public abstract class foo {
public abstract List<foo> Cache { get; }
protected static Dictionary<Type, List<foo>> InnerCache = new Dictionary<Type, List<foo>>();
}
public class bar : foo {
public override List<foo> Cache {
get { return foo.InnerCache[typeof(bar)]; }
}
public bar() { Cache.Add(this); }
}
public class baz : foo {
public override List<foo> Cache {
get { return foo.InnerCache[typeof(baz)]; }
}
public baz() { Cache.Add(this); }
}
Ecco la tua risposta:
using System;
using System.Collections.Generic;
static class Program
{
static void Main()
{
var bar = new Bar();
var baz = new Baz();
System.Console.WriteLine(
"We have {0} bars, rejoice!", Bar.Cache.Count);
bar.PrintList();
baz.PrintList();
}
}
public abstract class Foo<T>
{
public static List<T> Cache = new List<T>();
public void PrintList()
{
foreach(var item in Cache)
{
Console.WriteLine(item);
}
}
}
public class Bar : Foo<Bar>
{
public Bar() { Cache.Add(this); }
}
public class Baz : Foo<Baz>
{
public Baz() { Cache.Add(this); }
}
prova questo:
using System.Collections.Generic;
using System;
static class Program
{
static void Main()
{
Bar bar = new Bar();
Baz baz = new Baz();
System.Console.WriteLine(
"We have {0} bars, rejoice!", bar.Cache.Count);
System.Console.ReadKey();
}
}
public abstract class Foo
{
public Foo()
{
Cache = new List<string>();
}
public List<String> Cache { get; set; }
}
public class Bar : Foo
{
public Bar()
{
Cache.Add("Bar");
}
}
public class Baz : Foo
{
public Baz() { Cache.Add("Baz"); }
}
Mi dispiace di aver dovuto cambiare la custodia .. Mi stava facendo esplodere la testa