博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[System Design] 系统设计 (3) -- OOD
阅读量:6273 次
发布时间:2019-06-22

本文共 2701 字,大约阅读时间需要 9 分钟。

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

转载地址:http://ywmpa.baihongyu.com/

你可能感兴趣的文章
拥抱PostgreSQL,红帽再表态:SSPL的MongoDB坚决不用
查看>>
让架构更简单,QCon上海2016热点前瞻
查看>>
如何测试ASP.NET Core Web API
查看>>
SQL Server新一轮更新
查看>>
想像亚马逊或 Netflix 一样酷?抱走敏捷转型五大秘籍
查看>>
《Git in Practice》作者访谈:关于Git的八个问题
查看>>
Visual Studio 2019正式版发布,专注于人工智能和生产力
查看>>
写给Java程序员的Java虚拟机学习指南
查看>>
苏宁11.11:如何基于异步化打造会员任务平台?
查看>>
滴滴开源支撑业务代码重构工具Rdebug
查看>>
Table Editor 使用方法
查看>>
支持多种小程序!阿里云ARMS推出小程序监控
查看>>
《The Age of Surge》作者访谈
查看>>
《A Seat at the Table》作者访谈录
查看>>
F# 4.5提供Spans、Match!等特性
查看>>
微信小程序模块化开发实践
查看>>
Jenkins将致力于提升稳定性、易用性和云原生兼容性
查看>>
Facebook开源工具包LASER,支持93种语言
查看>>
禁止eclipse校验JavaScript
查看>>
从微服务迁移到工作流的经验之谈
查看>>