Skip to content

Instantly share code, notes, and snippets.

@PureKrome
Created July 17, 2013 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PureKrome/6025579 to your computer and use it in GitHub Desktop.
Save PureKrome/6025579 to your computer and use it in GitHub Desktop.
Problem with Inheritance.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
var animalFactory = new AnimalFactory();
// Search for some fishes.
var fishService = animalFactory.GetAnimalService("fish");
var fishes = fishService.SearchForAnimals(new FishSearchOptions {ScalesType = "shiny green"});
// Search for meows.
var catService = animalFactory.GetAnimalService("cat");
var kittyCats = catService.SearchForAnimals(new MamalSearchOptions {HairColour = "golden"});
}
}
#region Main Animal and Searching Interfaces
/// <summary>
/// I am an Animal! Here me roar! (fish can roar?)
/// </summary>
public interface IAnimal
{
string Name { get; set; }
int Age { get; set; }
}
public class BaseAnimal : IAnimal
{
public string Name { get; set; }
public int Age { get; set; }
}
/// <summary>
/// How we can search for animals.
/// </summary>
public interface IAnimalSearchOptions
{
string Name { get; set; }
}
/// <summary>
/// Search results. We might return the fist 5 results, out of 150.
/// </summary>
public class SearchResult<T> where T: IAnimal
{
IList<IAnimal> Animals { get; set; }
int TotalCount { get; set; }
}
/// <summary>
/// The main search interface for finding animals.
/// </summary>
public interface IAnimalService<T, in TU>
where T: IAnimal
where TU: IAnimalSearchOptions
{
SearchResult<T> SearchForAnimals(TU searchOptions);
}
#endregion
#region Mamals
public class MamalItem : BaseAnimal
{
// -- Mamal specific properties --
public string HairColour { get; set; }
}
public class MamalSearchOptions : IAnimalSearchOptions
{
public string Name { get; set; }
/// <summary>
/// Filter results by hair colour.
/// </summary>
public string HairColour { get; set; }
}
public class MamalService : IAnimalService<MamalItem, MamalSearchOptions>
{
public SearchResult<MamalItem> SearchForAnimals(MamalSearchOptions searchOptions)
{
throw new NotImplementedException();
}
}
#endregion
#region Fishes
public class Fish : BaseAnimal
{
// Fish specific properties
public string ScalesType { get; set; }
}
public class FishSearchOptions : IAnimalSearchOptions
{
public string Name { get; set; }
// Filter by scale type.
public string ScalesType { get; set; }
}
public class FishService : IAnimalService<Fish, FishSearchOptions>
{
public SearchResult<Fish> SearchForAnimals(FishSearchOptions searchOptions)
{
throw new NotImplementedException();
}
}
#endregion
public class AnimalFactory
{
private static IDictionary<string, IAnimalService<IAnimal, IAnimalSearchOptions>> _animals;
public AnimalFactory()
{
_animals = new Dictionary<string, IAnimalService<IAnimal, IAnimalSearchOptions>>
{
{"Cat", new MamalService() },
{"Dog", new MamalService() },
{"GoldFish", new FishService() }
};
}
public IAnimalService<IAnimal, IAnimalSearchOptions> GetAnimalService(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name");
}
return _animals[name];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment