E
Let's start dealing with your problem from the end.The same point shall not necessarily be number. It could be a date, time or even IP. There are a lot of combinations and complexity in configuration. Anything ready?Comparison operations exist in all programming languages. It always works for the numbers, but with other types of data, there may be problems.If you're lucky and your programming language supports the redefinition of comparison operations, you can introduce your type of data (e.g.,) IPAddressand implement correct operator behaviour for him >♪ <♪ == (all other comparison operators - derivatives from these three).If you are unlucky and your programming language does not support the redefinition of comparison operators, a special interface will be available to assist with the necessary methods in a clear form:interface IPPointInterface
{
public function isEqualTo(IPPointInterface $ip): bool;
public function isGraterThan(IPPointInterface $ip): bool;
public function isLessThan(IPPointInterface $ip): bool;
}
By analogy, an interface can be introduced for any type of data.It is clear that the interval is defined by two points and many points on the number of direct points between them. If we have a time interval, it makes sense that we can check if there's a point at a given interval.You've rightly caught the main thrust of the interval - he can check the point in it or not. That's what you're supposed to do in his interface. In addition, the customer code should be able to obtain the starting and final points:interface IPIntervalInterface {
public function containsPoint(IPPointInterface $ip): bool;
public function getStartPoint(IPPointInterface $ip): IPPoint;
public function getEndPoint(IPPointInterface $ip): IPPoint;
}
From personal experience, I would add that in 99% of cases there are several other methods that should maintain interval interfaces, such as checking intervals.And there's a problem, because intervals are four types-- [a, b]♪ (a, b)♪ [a, b)♪ (a, b]♪This is the details of implementation. You can either implement four classes of intervals or shirk and implement the base class for cut (closed intervals) [a, b]class IPClosedInterval implements IPIntervalInterface {
private $start;
private $end;
public function __construct(IPPointInterface $start, IPPointInterface $end) {
$this->start = $start;
$this->end = $end;
}
public function getStartPoint() {
return $this->start;
}
public function getEndPoint() {
return $this->end;
}
public function containsPoint(IPPointInterface $ip) {
return $ip->isLessThan($this->getEndPoint())
&& $ip->isGreaterThan($this->getStartEndPoint());
}
}
And two border decorators.class IPIntervalLeftOpenDecorator implements IPIntervalInterface {
private $interval;
public function __construct(IPIntervalInterface $interval) {
$this->interval = $interval;
}
public function containsPoint(IPPointInterface $ip) {
return $this->interval->containsPoint($ip)
|| $ip->isEqualTo($this->getStartPoint());
}
public function getStartPoint() {
return $this->interval->getStartPoint();
}
public function getEndPoint() {
return $this->interval->getEndPoint();
}
}
If desired, the designer and the code returning the boundaries can be removed to the abstract decorator.Here's an example of how this can be used:$a = new IPPoint('...');
$b = new IPPoint('...');
$closed_int = new IPIntervalClosed($a, $b); // [a, b]
$left_opened_int = new IPIntervalLeftOpenDecorator($closed_int); // (a, b]
$right_opened_int = new IPIntervalRightOpenDecorator($closed_int); // [a, b)
$opened_int = new IPIntervalLeftOpenDecorator($right_opened_int); // (a, b)
Programming has been in existence for decades, and this task has to be repeated and, accordingly, there must be a general solution that I do not seem to know.It depends very much on your programming language. For example, in C++, you can define the reference type for the point and parameterize the interval class with that type.With regard to PHP, you can also identify a base type for a point. IPPointInterface Except for the types of arguments in the methods, but then you'll have to manually control the matching of the same types in the interval methods. It's not as complicated as it seems, but I still prefer automatic control of types.If there is still a need for automatic control of PHP types, the automatic generation of interval classes for different types of points may be performed, but this topic requires separate discussion.In fact, the question is not exactly how, but the question is, are there solutions?Personally, I only got a class to work at intervals: https://github.com/thephpleague/period ♪ At the same time, I never looked for a solution to work with other types of intervals.