重构代码以使基类只包含一个公共成员

时间:2017-08-02 03:43:04

标签: c# oop base-class

public class AStreamManager
{
    public IVehicle Vehicle { get; set; }

    public string AIp { get; set; }
    public int APort { get; set; }

    public delegate NetworkStream ConnectToAStream(string host, int port);

    public AStreamManager(IVehicle vehicle, string IP, int Port)
    {
        Vehicle = vehicle;

        AIp = IP;
        APort = Port;
    }

    public NetworkStream ConnectToVehicleAStream(ConnectToAStream conn)
    {
        return conn(AIp, APort); ;
    }
}

public class BStreamManager
{
    public IVehicle Vehicle { get; set; }

    public string BIp { get; set; }
    public int BPort { get; set; }

    public delegate NetworkStream ConnectToBStream(string host, int port);

    public BStreamManager(IVehicle vehicle, string IP, int Port)
    {
        Vehicle = vehicle;

        BIp = IP;
        BPort = Port;
    }

    public NetworkStream ConnectToVehicleBStream(ConnectToBStream conn)
    {
        return conn(BIp, BPort); ;
    }

}

我有两个班级AStreamManagerBStreamManager。如您所见,有一个类型为Vehicle的成员变量IVehicle,这些变量在这些类中很常见。 它是否有意义,我能否以“IVehicle Vehicle”移动到基类的方式进行重构?

1 个答案:

答案 0 :(得分:2)

这个问题对我来说似乎很假设。看着你的课程,我根本无法找到他们存在的理由。您有AIpBIp但这些显然是IP地址,因此拥有两个不同的属性是没有意义的。我只是拥有一个StreamManager课程。