📜  如何使用面向对象的原则设计停车场?

📅  最后修改于: 2021-09-10 02:47:05             🧑  作者: Mango

使用面向对象的原则设计停车场。

问:亚马逊、苹果、谷歌和更多采访

解决方案:就我们现在的目的而言,我们将做出以下假设。我们做出这些特定假设是为了在不增加太多的情况下增加问题的复杂性。如果你做出不同的假设,那完全没问题。
1)停车场有多层。每个级别都有多排点。
2)停车场可停摩托车、汽车、公交车。
3)停车场有摩托车点、紧凑点、大点。
4)摩托车可以停在任何地方。
5) 汽车可以停在一个紧凑的地方或一个大的地方。
6) 一辆公共汽车可以停在五个连续的同一排内的大停车位。它不能停在小地方。

在下面的实现中,我们创建了一个抽象类 Vehicle,从它继承了 Car、Bus 和 Motorcycle。为了处理不同的停车位大小,我们只有一个类 ParkingSpot,它有一个指示大小的成员变量。

Java的主要逻辑如下

// Vehicle and its inherited classes.
public enum VehicleSize { Motorcycle, Compact,Large }
  
public abstract class Vehicle
{
      protected ArrayList parkingSpots =
                           new ArrayList();
      protected String licensePlate;
      protected int spotsNeeded;
      protected VehicleSize size;
  
      public int getSpotsNeeded()
      {
          return spotsNeeded;
      }
      public VehicleSize getSize()
      {
          return size;
      }
  
      /* Park vehicle in this spot (among others,
         potentially) */
      public void parkinSpot(ParkingSpot s)
      {
          parkingSpots.add(s);
      }
  
  
      /* Remove vehicle from spot, and notify spot
         that it's gone */
      public void clearSpots() { ... }
  
      /* Checks if the spot is big enough for the
         vehicle (and is available).
         This * compares the SIZE only.It does not
        check if it has enough spots. */
      public abstract boolean canFitinSpot(ParkingSpot spot);
}
  
public class Bus extends Vehicle
{
    public Bus()
    {
        spotsNeeded = 5;
        size = VehicleSize.Large;
    }
  
    /* Checks if the spot is a Large. Doesn't check
     num of spots */
    public boolean canFitinSpot(ParkingSpot spot) 
    {... }
}
  
public class Car extends Vehicle
{
    public Car()
    {
        spotsNeeded = 1;
        size = VehicleSize.Compact;
    }
  
    /* Checks if the spot is a Compact or a Large. */
    public boolean canFitinSpot(ParkingSpot spot) 
    { ... }
}
  
public class Motorcycle extends Vehicle
{
    public Motorcycle()
    {
        spotsNeeded = 1;
        size = VehicleSize.Motorcycle;
    }
    public boolean canFitinSpot(ParkingSpot spot) 
    { ... }
}
  
  
  

ParkingSpot是通过只有一个代表停车位大小的变量来实现的。我们可以通过继承自 ParkingSpot 的 LargeSpot、CompactSpot 和 MotorcycleSpot 类来实现这一点,但这可能有点过头了。除了它们的大小之外,这些斑点可能没有不同的行为。

public class ParkingSpot
{
    private Vehicle vehicle;
    private VehicleSize spotSize;
    private int row;
    private int spotNumber;
    private Level level;
  
    public ParkingSpot(Level lvl, int r, int n,
                         VehicleSize s)
    { ... }
  
    public boolean isAvailable()
    {
        return vehicle == null;
    }
  
    /* Check if the spot is big enough and is available */
    public boolean canFitVehicle(Vehicle vehicle) { ... }
  
    /* Park vehicle in this spot. */
    public boolean park(Vehicle v) {..}
  
    public int getRow()
    {
        return row;
    }
    public int getSpotNumber()
    {
        return spotNumber;
    }
  
    /* Remove vehicle from spot, and notify
      level that a new spot is available */
    public void removeVehicle() { ... }
}

来源 :
www.andiamogo.com/S-OOD.pdf

更多参考:
https://www.quora.com/How-do-I-answer-design-related-questions-like-design-a-parking-lot-in-an-Amazon-interview
http://stackoverflow.com/questions/764933/amazon-interview-question-design-an-oo-parking-lot