1

单例模式 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...


linspiration
161 声望53 粉丝

引用和评论

0 条评论