[System Design] 系统设计 (3) -- OOD

单例模式 Singleton

使用场景

  1. 回收站

  2. 计数器

  3. 鼠标

特点 Features

  1. Singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.

  2. One object only have one instance.

  3. The abstract factory,builder,and prototype patterns can use Singletons in their implementation.

实现 Implementation

An implementation of the singleton pattern must:

ensure that only one instance of the singleton class ever exists; and
provide global access to that instance.

class Solution {
    private static Solution instance = null;
    public static Solution getInstance() {
        if (instance == null) {
            instance = new Solution();
        }
        return instance;
    }
    private Solution() {}
};

工厂模式 Factory Pattern

IOC injection -- Google

停车场设计 Parking Lot

Entities 哪些实体?

  • Vehicle,slot,lot.

    • Vehicle: car,bus,motorcycle.

      • ID (Primary Key),ParkingSize,moveIn(),moveOut()

    • Slot:

      • slotID (Primary Key),slotSize(),available(),fitInSpot()

    • Lot:

      • lotID (Primary Key),List<Slot> slotList,availableNum(),Map<Vehicle,Slot> v2slot,Map<Slot,Vehicle> slot2v,findAvailableSlots(),LookUp()

Manager 管理员

  • Parking Ticket

    • parkingTicket

      • int ID,parkingSlot pSlot,Vehicle myVehicle,Time enterTime,Time leaveTime

  • Parking Manager 负责调度(单例)

    • parkingManager

      • List<parkingTicket> ticketList,generateTicket(),withdrawTicket(),LookUp()

Blackjack AI 21点游戏

Entities

GameManager

  • GameRoom room

  • GameState state

  • RunGame()

  • Reset()

  • DecideWinner()

GameRoom

  • Dealer dealer

  • List<Player> playList

  • join(Player p)

  • leave(Player p)

  • openGameRoom()

Dealer

  • int ID

  • String name

  • Double money

  • List<BlackJackCard> HandCard

  • CardBox dealerCardBox

  • wantCard()

  • checkBigger17()

  • deal()

  • Strategy sDealer

Player

  • int ID

  • String name

  • Double money

  • List<BlackJackCard> HandCard

  • wantCard()

  • showHand()

  • calculateHandscore()

  • makeDecision()

  • Strategy sPlayer

Gamer (Player + Dealer)

  • int ID

  • String name

  • Double money

  • List<BlackJackCard> HandCard

  • wantCard()

Deck

CardBox

  • List<BlackJackCard> cardList

  • AppendDecks()

  • Shuffle()

  • PopCard()

Pack 装了4副牌or so

  • List<BlackJackCard> cardList

card

  • int faceValue

  • enum suit

  • Boolean isAvailable

BlackJackCard

  • int realValue,int maxValue(),int minValue()

Elevator 电梯设计

Entities

  • Elevator

    • int ID

    • Enum status

    • int size

    • int maxWeight

    • int NowFloor

    • moveUp()

    • moveDown()

    • stop()

    • getNowFloor()

    • getNowStatus()

  • ElevatorUser

    • int userID

    • callUp(Elevator)

    • callDown(Elevator)

    • checkElevatorStatus(Elevator)

    • int targetFloor

    • openDoor(Elevator)

    • closeDoor(Elevator)

    • emergency(Elevator)

  • Manager

    • List<Elevator> elevatorList

    • List<Message> messageList

    • Strategy strategy

  • Message

    • Enum status(drop/pick)

    • Elevator elevator

    • List<Integer> targetFloors

    • Time enterTime

    • Operations...

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...