확실히 이것이 최고의 제목이 아닙니다. 수학 문제를 생성하는 시스템을 만들고 있습니다. 개발자는 다음 두 인터페이스를 구현해야합니다.두 클래스 간의 통신을 유지하는 방법은 무엇입니까?
Problem
: 생성 된 문제가 필요한 속성이 포함되어 있습니다.Configuration
:Problem
을 생성하는 범위 매개 변수입니다.
이는 인터페이스입니다 :
public abstract class Problem
{
}
public abstract class Configuration
{
}
그리고 여기가 BinaryProblem 하나의 예이다.
public class BinaryProblem : Problem
{
public decimal X { get; set; }
public decimal Y { get; set; }
public BinaryProblem(decimal x, decimal y)
{
this.X = x; // Number 1
this.Y = y; // Number 2
}
}
public class BinaryProblemConfiguration : Configuration
{
// Range for X
public int XMin { get; set; }
public int XMax { get; set; }
// Range for Y
public int YMin { get; set; }
public int YMax { get; set; }
public BinaryProblemConfiguration() { }
}
문제와 구성 사이의 줄을 볼 수 있습니까? 나는이 두 인터페이스를 구현하는 많은 모듈을 넣어야한다.
그래서 생성 할 방법이 필요합니다.
protected static Random
을 : 거의 모든 구성이 번호 (즉random.Next(X1, Y1);
)를 생성하는 임의의 클래스를 필요로 나는 포함하는 경우 추상 클래스를 만들 수있는 생각했다. 그리고 항상 같은 씨앗을 사용하여 숫자를 만들 필요가 있기 때문에 정적이어야합니다.public abstract TProblem Generate(TConfiguration config); // where : TProblem : Problem, new(), TConfiguration : Configuration
그리고 각 문제 유형이 추상 클래스를 구현합니다.
내 질문은 :이 솔루션을 해결하기 시작하는 좋은 방법입니까, 아니면 다른 솔루션을 사용해야합니까?
편집 : 내가하려고 한 하나의 예는 다음과 같습니다
이 내 추상 클래스, 내 생각은이 클래스를 실체화 할 때, 당신은 일반적인 값을 지정 의미 :
public interface IProblemFactory
{
Problem CreateProblem();
}
public abstract class ProblemBaseFactory<TProblem, TConfiguration> : IProblemFactory
where TProblem : Problem
where TConfiguration : Configuration
{
private const int SEED = 100;
protected TConfiguration _config;
protected static Random _random;
public ProblemBaseFactory(TConfiguration config)
{
_config = config;
if (_random == null) _random = new Random(SEED);
}
public void SetSeed(int newSeed)
{
_random = new Random(newSeed);
}
public Problem CreateProblem()
{
return CreateProblem(_config);
}
public abstract TProblem CreateProblem(TConfiguration config);
}
public class BinaryProblemFactory : ProblemBaseFactory<BinaryProblem, BinaryProblemConfiguration>
{
public override BinaryProblem CreateProblem(BinaryProblemConfiguration config)
{
var x = GenerateValueInRange(_config.Range1);
var y = GenerateValueInRange(_config.Range2);
return new BinaryProblem(x, y, Operators.Addition);
}
private decimal GenerateValueInRange(Range<int> range)
{
return _random.Next(range.MinValue, range.MaxValue);
}
}
Sorr, 나는 'Problem'과 'Configuration'에서 파생 된 클래스가 어떻게 사용되는지 이해하지 못합니다. 사용 예를 추가 할 수 있습니까? – MiMo
X, X1 및 Y1의 범위와 Y, X2, Y2의 범위가 이유는 무엇입니까? 예! XMin과 XMax 등을 사용하십시오. –
새로운 정보를 추가했습니다 :) –