Class 使用 C# 以 SOLID 原则进行设计
Class design with SOLID Principle using C#
每部电梯都有一组状态。
• 维护:电梯不响应外部信号(仅响应自身信号)。
• 站立:电梯固定在一个楼层上。如果它接到电话。电梯在那一层,门开着。如果它在另一层楼,它会朝那个方向移动。
• 向上:电梯向上移动。每次到达一层时,它都会检查是否需要停下来。如果是这样,它会停下来并打开门。它等待一定的时间并关上门(除非有人穿过它们。然后它从请求列表中删除楼层并检查是否有另一个请求。如果有,电梯再次开始移动。如果没有,它进入国家立场。
• 向下:像向上但方向相反
注意:有些电梯不是从 bottom/first_floor 开始,尤其是。在天空刮板的情况下。 min_floor & max_floor 是 Elevator 的两个附加属性。
我的演示设计:
public abstract class Elevator
{
protected bool[] floorReady;
protected int CurrentFloor = 1;
protected int topfloor;
protected ElevatorStatus Status = ElevatorStatus.STOPPED; //ElevatorStatus is enum
protected virtual void Stop(int floor){}
protected virtual void Descend(int floor) {}
protected virtual void Ascend(int floor) {}
protected virtual void StayPut () {}
protected virtual void FloorPress (int floor){}
}
interface ILogger
{
void RegisterLog(string Message)
}
public FileLogger : ILogger
{
void RegisterLog(string Message)
{
//Custom Code
}
}
public class MyElevator : Elevator
{
// All method overrride for base
}
//Client class
class program
{
public static void main()
{
//DI for calling Looging
}
}
有人可以帮助我设计满足所有 SOLID 原则的 类..
提前致谢...我想使用 SOLID 原理设计一个电梯模拟器。
通过使用它,您可以将移动电梯的业务规则移动到每个特定状态。因此,不是使用枚举,而是让每个状态决定下一步要做什么(比如下一个动作使用哪个状态)。
它还允许您在不修改电梯的情况下添加新功能(通过添加更多状态)class(从 SOLID 的角度来看这很重要)。
每部电梯都有一组状态。
• 维护:电梯不响应外部信号(仅响应自身信号)。
• 站立:电梯固定在一个楼层上。如果它接到电话。电梯在那一层,门开着。如果它在另一层楼,它会朝那个方向移动。
• 向上:电梯向上移动。每次到达一层时,它都会检查是否需要停下来。如果是这样,它会停下来并打开门。它等待一定的时间并关上门(除非有人穿过它们。然后它从请求列表中删除楼层并检查是否有另一个请求。如果有,电梯再次开始移动。如果没有,它进入国家立场。
• 向下:像向上但方向相反
注意:有些电梯不是从 bottom/first_floor 开始,尤其是。在天空刮板的情况下。 min_floor & max_floor 是 Elevator 的两个附加属性。
我的演示设计:
public abstract class Elevator
{
protected bool[] floorReady;
protected int CurrentFloor = 1;
protected int topfloor;
protected ElevatorStatus Status = ElevatorStatus.STOPPED; //ElevatorStatus is enum
protected virtual void Stop(int floor){}
protected virtual void Descend(int floor) {}
protected virtual void Ascend(int floor) {}
protected virtual void StayPut () {}
protected virtual void FloorPress (int floor){}
}
interface ILogger
{
void RegisterLog(string Message)
}
public FileLogger : ILogger
{
void RegisterLog(string Message)
{
//Custom Code
}
}
public class MyElevator : Elevator
{
// All method overrride for base
}
//Client class
class program
{
public static void main()
{
//DI for calling Looging
}
}
有人可以帮助我设计满足所有 SOLID 原则的 类..
提前致谢...我想使用 SOLID 原理设计一个电梯模拟器。
通过使用它,您可以将移动电梯的业务规则移动到每个特定状态。因此,不是使用枚举,而是让每个状态决定下一步要做什么(比如下一个动作使用哪个状态)。
它还允许您在不修改电梯的情况下添加新功能(通过添加更多状态)class(从 SOLID 的角度来看这很重要)。