作者:丝家发艺 | 来源:互联网 | 2023-09-02 16:09
Wefollowthefourstepsinthedesignprinciple:FUNCTIONTherearethreetypesofparkingspaces:motor
We follow the four steps in the design principle:
FUNCTION
There are three types of parking spaces: motorbike, car and handicapped car.
Motorbikes and cars can only be parked in their designated parking spaces, while
handicapped cars can be parked either in their own parks or those for regular
cars.
Any car can only park to a space for its type available in the parking lot.
Available parking spaces of each type are maintained in the parking lot
object.
Each parking space has a permission system: either paying to park or free
park.
OBJECTS
ParkingLot is a class
ParkingSpace is a
class
ParkingLot has a finite number
of ParkingSpace objects
No need to have subclasses for each type
of ParkingSpace because they share the same operations as the
generic ParkingSpace. The type is identified in terms of a member
variable in ParkingSpace objects.
Class Vehicle and its subclasses implement each type of
vehicles.
An interface Permission represents a generic payment
system. Permission is implemented
as PaidPermission (pay to park)
and FreePermission (free park).
An non-static inner class (Java) ParkingMeter in
each ParkingSpace to record start and end time of a parking
session.
RESPONSIBILIES
ParkingLot maintains arrays of vacant parking spaces, each for
one type of spaces. No need to record occupied information separately in
each ParkingSpace instance.
Define the class Vehicle to provide the
method park for common parking operation, each subclass
implements the vehicle interface to park into a parking space of its type.
ParkingLot has no operation besides accessor and mutator, it‘s
used as a storage to maintain available parking spaces of each
type. allocateFreeSpace() andreclaimFreeSpace() are
responsible for allocating and reclaiming parking spaces. Readings
of ParkingMeters in
each ParkingSpace and Permission in ParkingLot are
applied in these two methods to get parking fees. They are called by
Vehicle::park() and Vehicle::unpark() methods.
public interface Permission{
float getFee(Calendar start, Calendar end);
}
class ParkingLot{
private Listgt; freeMotorSpace;
private Listgt; freeHandicappedSpace;
private Listgt; freeCompactSpace;
public ParkingLot();
public ParkingSpace allocateFreeSpace(ParkingSpaceType pspaceType)
{
//get a ParkingSpace from the corresponding free list
pspace.setStart();
return pspace;
}
public float reclaimFreeSpace(ParkingSpace pspace){
pspace.setEnd();
//return free space to the list
return pspace.getFee(perm);
}
private Permission perm;
}
class ParkingSpace
{
enum ParkingSpaceType
{
MOTORBIKE, CAR, HANDICAPPED;
}
private int id;
private ParkingSpaceType pspaceType;
private ParkingMeter meter;
public ParkingSpace(int id, ParkingSpaceType pspaceType)
{
super();
this.id = id;
this.pspaceType = pspaceType;
}
public int getId()
{
return id;
}
public ParkingSpaceType getpspaceType()
{
return pspaceType;
}
private class ParkingMeter{
public GregorianCalendar start;
public GregorianCalendar end;
}
public void setStart()
{
meter.start = new GregorianCalendar();
}
public void setEnd()
{
meter.end = new GregorianCalendar();
}
public float getFee(Permision perm)
{
return perm.getFee(meter.start, meter.end);
}
}
public abstract class Vehicle{
public boolean park(ParkingLot pLot);
public boolean unpark(ParkingLot pLot){
if(pspace != null){
pLot.reclaimFreeSpace(pspace);
return true;
}
return false;
};
private ParkingSpace pspace;
}
public class Motorbike extends Vehicle{
public boolean park(ParkingLot pLot){
if((pspace=pLot.allocateFreeSpace(ParkingSpaceType.MOTORBIKE))==null){
return false;
}
return true;
}
}
public class Cars extends Vehicle{
public boolean park(ParkingLot pLot){
if((pspace=pLot.allocateFreeSpace(ParkingSpaceType.CAR))==null){
return false;
}
return true;
}
}
public class HandicappedCars extends Vehicle{
public boolean park(ParkingLot pLot){
if((pspace=pLot.allocateFreeSpace(ParkingSpaceType.HANDICAPPED))==null && (pspace=pLot.getfreeSpace(ParkingSpaceType.CAR))==null){
return false;
}
return true;
}
}
parking lot,布布扣,bubuko.com