Код: Выделить всё
public class House
{
public House(IEnumerable windows)
{
Windows = windows;
}
public IEnumerable Windows { get; private set; }
}
public class Window
{
public Window(string brand, int height, int width)
{
Brand = brand;
Height = height;
Width = width;
}
public string Brand { get; private set; }
public int Height { get; private set; }
public int Width { get; private set; }
}
Мы могли бы расширить наш класс окна, чтобы представить возможность того, что окно будет устойчиво к солнечному свету.
Код: Выделить всё
public class WindowMaybeSolarResistant : Window
{
public WindowMaybeSolarResistant(string brand, int height, int width, bool isSolarResistant) : base(brand, height, width)
{
IsSolarResistant = isSolarResistant;
}
public bool IsSolarResistant { get; private set; }
}
Вместо этого мы могли бы просто добавить в окно еще один конструктор.
Код: Выделить всё
public class Window : IWindow
{
public Window(string brand, int height, int width)
{
Brand = brand;
Height = height;
Width = width;
IsSolarResistant = false; //What if this is really true for the window though? Could someone use the wrong constructor?
}
public Window(string brand, int height, int width, bool isSolarResistant)
{
Brand = brand;
Height = height;
Width = width;
IsSolarResistant = isSolarResistant;
}
public string Brand { get; private set; }
public int Height { get; private set; }
public int Width { get; private set; }
public bool IsSolarResistant { get; private set; }
}
Код: Выделить всё
public interface IWindow
{
public string Brand { get; }
public int Height { get; }
public int Width { get; }
}
public class House where TWindow : IWindow
{
public House(IEnumerable windows)
{
Windows = windows;
}
public IEnumerable Windows { get; private set; }
}
Код: Выделить всё
public class HouseCouldHaveSolarWindows : House
{
public HouseCouldHaveSolarWindows(IEnumerable windows) : base(windows)
{
}
public new IEnumerable Windows { get; private set; }
}
Я пытаюсь найти способ следовать принципу открытости/закрытости с моделями, чтобы мои модели были открыты для расширения (новые свойства) и закрыты для модификации.
Подробнее здесь: https://stackoverflow.com/questions/788 ... dification