Object-Oriented Design(OOD)
类型
Singleton Pattern
public class Singleton {
private static Singleton instance = null;
private Singleton(){
// Exists only to defeat instantiation.
}
public static Singleton getInstance(){
if(instance==null) instance = new Singleton();
return instance;
}
}
Factory Pattern
public class CardGame{
public static CardGame createCardGame(GameType type){
if(type == GameType.Poker){
return new PokerGame();
}else if(type == GameType.BlackJack){
return new BlackJackGame();
}
return null;
}
}
Command Pattern
命令模式很好理解,举个例子,司令员下令让士兵去干件事情,从整个事情的角度来考虑,司令员的作用是,发出口令,口令经过传递,传到了士兵耳朵里,士兵去执行。这个过程好在,三者相互解耦,任何一方都不用去依赖其他人,只需要做好自己的事儿就行,司令员要的是结果,不会去关注到底士兵是怎么实现的。
Decorator Pattern
Problem1: Parking Lot
public enum VehicleSize{Motorcycle, Compact, Large}
public abstract class Vehicle{
private List<ParkingSpot> parkingSpots = new ArrayList<>();
private String licensePlate;
private int spotsNeeded;
private VehicleSize size;
public int getSpotsNeeded(){return spotsNeeded;}
public VehicleSize getSize(){return size;}
public void partInSpot(ParkingSpot s){ parkingSpots.add(s)};
public void clearSpots(){}
public abstract boolean canFitInSpot(ParkingSpot spot);
}
public class Bus extends Vehicle {
public Bus(){
spotsNeeded = 5;
size = VehicleSize.Large;
}
public boolean canFitInSpot(ParkingSpot spot){}
}
public class Car extends Vehicle {
public Car(){
spotsNeeded = 1;
size = VehicleSize.Compact;
}
public boolean canFitInSpot(ParkingSpot spot){}
}
public class Motorcycle extends Vehicle {
public Motorcycle(){
spotsNeeded = 1;
size = VehicleSize.Motorcycle;
}
public boolean canFitInSpot(ParkingSpot spot){}
}
public class ParkingLot {
private Level[] levels;
private final int NUM_LEVELS = 5;
public ParkingLot(){}
public boolean parkVehicle(Vehicle vehicle){}
}
public class Level {
private int floor;
private ParkingSpot[] spots;
private int availableSpots = 0;
private static final int SPOTS_PER_ROW = 10;
public Level(int flr, int numberSpots){}
public int availableSpots(){ return availableSpots;}
public boolean parkVehicle(Vehicle vehicle){}
private boolean parkStartingAtSpot(int num, Vehicle v){}
private int findAvailableSpots(Vehicle vehicle){}
public void spotFreed(){availableSpots++;}
}
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};
public boolean canFitVehicle(Vehicle vehicle){}
public boolean part(Vehicle v){}
public int getRow(){ return spotNumber;}
public void removeVehicle(){}
}
Problem2: Card Game
public class Card{
public enum Suit{
CLUBS(1),SPADES(2),HEARTS(3),DIAMONDS(4);
int value;
private Suit(int v){ value = v;}
}
private int card;
private Suit suit;
public Card(int r, Suit s){
card = r;
suit = s;
}
public int value(){return card;}
public Suit suit(){return suit;}
}
public class Black extends Card{
public Black(int r, Suit s){
super(r,s);
}
}
Problem3: Elevator
Problem explain1
Problem explain2
Problem4: File System
Problem5: Hotel Reservation
Summary