using System;
namespace HidingMembersViaExplicitInterfaceImplementation
{
public enum FruitType
{
Apple,
Orange,
Banana
}
public class Factory
{
public static Fruit Create(FruitType type)
{
// the Factory class can modify the properties because it has access to
// the private IFruitSetters interface
var fruit = new Fruit();
((IFruit)fruit).fruitType = type;
switch (type)
{
case FruitType.Apple:
((IFruit)fruit).color = "Red";
break;
case FruitType.Banana:
((IFruit)fruit).color = "Yellow";
break;
case FruitType.Orange:
((IFruit)fruit).color = "Orange";
break;
}
((IFruit)fruit).DoOtherHiddenThings();
return fruit;
}
// define a private interface containing the setters.
private interface IFruit
{
// define the setter in the private interface
string color { set; }
FruitType fruitType { set; }
void DoOtherHiddenThings();
}
// This is the inner class. It has members color and fruitType that should not be modified
// by clients, but should be modified by the factory.
public class Fruit : IFruit
{
private string pColor;
private FruitType pFruitType;
// explicitly implement the setters
string IFruit.color { set { pColor = value; } }
FruitType IFruit.fruitType { set { pFruitType = value; } }
void IFruit.DoOtherHiddenThings()
{
//Does nothing. Just showing how to hide methods too.
}
// create a public getters
public string Color { get { return pColor; } }
public FruitType FruitType { get { return pFruitType; } }
}
}
class Program
{
static void Main(string[] args)
{
Factory.Fruit apple = Factory.Create(FruitType.Apple);
// can only read color and fruitType because only the getter is public
// and IFruit is private to Factory
string fruitColor = apple.Color;
FruitType type = apple.FruitType;
}
}
}
Yet another benefit can be found using this technique. Often, large interface definitions force a class to implement methods that, for our purposes, make no logical sense for our usage. By implementing them explicitly, we effectively hide those methods from the visible public class instance.