PatternsCSharpProgramming/Patterns/Flyweight/Program.cs
Dvurechensky ddc05f8703 1.0.3
2025-05-12 03:32:04 +03:00

105 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Author: Nikolay Dvurechensky
* Site: https://www.dvurechensky.pro/
* Gmail: dvurechenskysoft@gmail.com
* Last Updated: 12 мая 2025 03:31:02
* Version: 1.0.7
*/
/* Приспособленец
Благодаря совместному использованию,
поддерживает эффективную работу
с большим количеством объектов.
(для оптимизации работы с памятью)
*/
class Program
{
static void Main()
{
#region Пример 1 - базовое
double longtitude = 22.33;
double latitude = 55.11;
HouseFactory houseFactory = new HouseFactory();
for (int i = 0; i < 10; i++)
{
House panelH = houseFactory.GetHouse("Panel");
if(panelH != null)
panelH.Build(longtitude, latitude);
longtitude += 0.1;
latitude += 0.1;
}
for (int i = 0; i < 10; i++)
{
House officeH = houseFactory.GetHouse("Office");
if (officeH != null)
officeH.Build(longtitude, latitude);
longtitude += 0.1;
latitude += 0.1;
}
Console.ReadKey();
#endregion
}
}
abstract class House
{
/// <summary>
/// Кол-во этажей - внутреннее состояние
/// </summary>
protected int stages;
/// <summary>
/// Внешнее состояние действия
/// </summary>
/// <param name="latitude"></param>
/// <param name="longitude"></param>
public abstract void Build(double latitude, double longitude);
}
class PanelHouse : House
{
public PanelHouse()
{
stages = 5;
}
public override void Build(double latitude, double longitude)
{
Console.WriteLine($"PanelHouse Build stages-{stages} {latitude}, {longitude}");
}
}
class OfficeHouse : House
{
public OfficeHouse()
{
stages = 50;
}
public override void Build(double latitude, double longitude)
{
Console.WriteLine($"OfficeHouse Build stages-{stages} {latitude}, {longitude}");
}
}
class HouseFactory
{
Dictionary<string, House> houses = new Dictionary<string, House>();
public HouseFactory()
{
houses.Add("Panel", new PanelHouse());
houses.Add("Office", new OfficeHouse());
}
public House GetHouse(string key)
{
if (houses.ContainsKey(key))
return houses[key];
else
return null;
}
}