내가 지금하고있는 생각은 공통 개체에 대해 특정 계산을 수행 한 다음 해당 결과에 따라 새로운 분석 개체 집합을 만드는 여러 수준의 "계층"시스템 분석 개체를 사용하는 것입니다. 새로 생성 된 분석 객체는 자신의 차례를 실행하고 선택적으로 더 많은 분석 객체를 생성하는 등의 작업을 수행합니다. 요점은 하위 분석 객체가 생성 된 객체 다음에 항상 실행된다는 점입니다. 상대적으로 중요합니다. 전체 장치가 단일 스레드에 의해 호출 될 것이므로 현재 스레드 안전과 관련이 없습니다. 어떤 기본 조건이 충족되는 한, 나는 이것이 불안정한 디자인이되는 것을 보지 못한다. 그러나 나는 그것에 대해 아직도 조금 열중하고있다.분석 위젯을 사용한 계층화 된 디자인 -이 코드의 냄새가 있습니까?
이 코드는 심각한 코드 냄새입니까? 아니면이 방법으로 구현해야합니까? 더 좋은 방법이 있습니까?
namespace WidgetTier
{
public class Widget
{
private string _name;
public string Name
{
get { return _name; }
}
private TierManager _tm;
private static readonly Random random = new Random();
static Widget()
{
}
public Widget(string name, TierManager tm)
{
_name = name;
_tm = tm;
}
public void DoMyThing()
{
if (random.Next(1000) > 1)
{
_tm.Add();
}
}
}
//NOT thread-safe!
public class TierManager
{
private Dictionary<int, List<Widget>> _tiers;
private int _tierCount = 0;
private int _currentTier = -1;
private int _childCount = 0;
public TierManager()
{
_tiers = new Dictionary<int, List<Widget>>();
}
public void Add()
{
if (_currentTier + 1 >= _tierCount)
{
_tierCount++;
_tiers.Add(_currentTier + 1, new List<Widget>());
}
_tiers[_currentTier + 1].Add(new Widget(string.Format("({0})", _childCount), this));
_childCount++;
}
//Dangerous?
public void Sweep()
{
_currentTier = 0;
while (_currentTier < _tierCount) //_tierCount will start at 1 but keep increasing because child objects will keep adding more tiers.
{
foreach (Widget w in _tiers[_currentTier])
{
w.DoMyThing();
}
_currentTier++;
}
}
public void PrintAll()
{
for (int t = 0; t < _tierCount; t++)
{
Console.Write("Tier #{0}: ", t);
foreach (Widget w in _tiers[t])
{
Console.Write(w.Name + " ");
}
Console.WriteLine();
}
}
}
class Program
{
static void Main(string[] args)
{
TierManager tm = new TierManager();
for (int c = 0; c < 10; c++)
{
tm.Add(); //create base widgets;
}
tm.Sweep();
tm.PrintAll();
Console.ReadLine();
}
}
}
'의 반복되는 것으로 변경하지 않습니다 _tiers'; 'Widget.DoMyThing()'을 호출 할 때마다 * wid * teir에 위젯이 추가됩니다. – Randolpho