N
N
Nikolai Ryapolov2017-02-07 14:04:00
C++ / C#
Nikolai Ryapolov, 2017-02-07 14:04:00

How to properly refactor the following C# code so that it looks beautiful?

Good day.
Tell me how to refactor the following methods correctly and is it possible to simplify them for further use?
We have the following "abstract base" class:

public class BaseCar
{
    public int Position { get; set; } // номер стояночного места
    public int Box {get; set;} //номер гаража
}

from it we inherit 2 or more other classes
//Самосвалы
public class Tipper : BaseCar
{
    public int Capacity { get; set; }
}
// Автобусы
public class Autobus : BaseCar
{
    public int MaxPeople {get;set;}
}

These classes become part of the garage class.
public class Garage 
{
    public List<Autobus> autobus_list {get; set;};
    public List<Tipper> tipper_list {get; set;};
  
    public Garage()
    {
        autobus_list = getAutobusList();
        tipper_list = getTippetList();
    }
}

The logic class is responsible for working with the Garage class
public class Logic
{
    public Garage garage = new Garage();
    ....
    // Теперь сама суть вопроса
    // Требуется вернуть отсортированный по стояночному месту список 
    // или список определенной техники в каком либо гараже

    // В данный момент я делаю это такими вот методами
    public List<Autobus> getListAutobusInBox(int box)
    {
         return garage.autobus_list.FindAll(x => x.Box == box);
    }
    public List<Tipper> getListTipperInBox(int box)
    {
         return garage.tipper_list.FindAll(x => x.Box == box);
    }

    public List<Autobus> SortListAutobus(List<Autobus> list)
    {
         list.Sort(delegate (Autobus x, Autobus y)
                {
                    if (object.Equals(x, y)) return 0;
                    return x.Position.CompareTo(y.Position);
                });
                return list;
    }

    public List<Tipper> SortListTipper(List<Tipper> list)
    {
        list.Sort(delegate (Tipper x, Tipper y)
        {
            if (object.Equals(x, y)) 
                return 0;
            return x.Position.CompareTo(y.Position);
        });
        return list;
    }
}

I understand in my gut that the SortListAutobus and SortListTipper methods are absolutely identical at the level of the BaseCar base class, as well as the getListTipperInBox and getListAutobusInBox methods. Tell me how to bring them together at the base class level. It is likely that this is done at the level of interfaces, but I cannot make friends with them in any way. If it's all about interfaces, you can use this example to show how it will work.
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman, 2017-02-07
@NelegalSS

What interfaces? Generic Types and Methods - metanit.com/sharp/tutorial/3.12.php

using System.IO;
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Console.WriteLine("Hello, World!");
    }

    public class BaseCar
    {
        public int Position { get; set; } // номер стояночного места
        public int Box { get; set; } //номер гаража
    }

    public class Tipper : BaseCar
    {
        public int Capacity { get; set; }
    }
    // Автобусы
    public class Autobus : BaseCar
    {
        public int MaxPeople { get; set; }
    }

    public class Garage
    {
        public List<Autobus> autobus_list { get; set; }
        public List<Tipper> tipper_list { get; set; }

        public Garage()
        {
            autobus_list = new List<Autobus>();
            tipper_list = new List<Tipper>();
        }
    }

    public class Logic
    {
        public Garage garage = new Garage();

        public List<Autobus> getListAutobusInBox(int box)
        {
            return garage.autobus_list.FindAll(x => x.Box == box);
        }

        public List<Tipper> getListTipperInBox(int box)
        {
            return garage.tipper_list.FindAll(x => x.Box == box);
        }

        public List<T> SortList<T>(List<T> list) where T : BaseCar
        {
            return list.Sort((x, y) => object.Equals(x, y) ? 0 : x.Position.CompareTo(y.Position)).ToList();
        }

    }
}

P
Peter, 2017-02-07
@petermzg

You yourself write that BaseCar is abstract.
Then
you can add a method to the base class to determine the type of car.
Which will have to be implemented in each child class

public class Autobus : BaseCar
{
    public override CarType GetCarType()
    {
        return CarType.Autobus; 
    }
    public int MaxPeople {get;set;}
}

Of course, you can determine by class car.Type == typeof(Autobus), but it will be slower.
In the class Garage , you can store everything in one list List< BaseCar >
Now, according to a single list, you can sort by place and exclude by type.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question