Giter VIP home page Giter VIP logo

design-pattern-examples's Introduction

设计模式应用实践

项目设计说明

通过模拟手机的生产制造过程,练习基于多种设计模式生产过程中耦合/扩展等问题。
将全部设计模式在系统中一起实现过于复杂。因此,可以基于具体设计模式单独设计实现。

建造者模式

手机由,CPU,内存,电池,屏幕4个必须部件,以及摄像头,GPS,指纹识别,NFC,4个可选部件组成。
基于不同部件可以组装出不同类型手机。例如,P1为适合最简功能的老年机,仅需要基本部件即可;P2适合拍照爱好者;P3为旗舰版,等等。
手机为抽象的,手机类型为具体的。在增加新手机类型时,无需创建新组装线。

工厂模式/单例模式

部件由部件生产线提供,手机由组装线组装。
在增加新零部件时,仅需增加新生产线及生成方法,而无需修改原实现。

适配器模式

手机默认使用Type-C USB接口,支持读取以及快速充电。但仍需要支持Micro USB接口,其支持读取以及普通充电。而type-C与micro接口是不兼容的。 因此,提供将Micro USB转为Type-C的适配器。实现当用户使用Micro USB读取数据时,按适配的方式读取;但当充电时,按适配普通充电。

思路:设计Type-C的基本行为及实现;设计Micro的基本行为及实现;设计一个适配器(包装器),实现将Micro的行为适配为标准type-c行为。仅需设计适配方法模拟即可。

观察者模式/订阅发布模式

当生产线生产出问题部件时,发送消息通知所有关注部件质量的人,例如质管员,工人等。

思路:设计一个抽象的观察者,包含当收到消息时的行为;任何具体观察者决定收到消息后的行为实现。
设计消息源,维护一个监听此消息源的观察者集合,提供将观察者加入/移除集合的方法,提供通知所有此消息观察者消息产生的方法。
设计一个消息源工厂,封装各种消息源。即,当需要发送任何消息或订阅任何消息源监听时,通过工厂实现。
当部件生产出现问题(调用一个方法模拟即可),通知部件质量消息源错误信息, 消息源通知注册在此消息源上的全部观察者事件的发生,各观察者产生不同的消息消费行为,例如质管员记录,工人核查等(均为模拟的方法)。

需绘制各模式的类图,并详细说明设计过程及设计方法。给出选择使用的设计模式的理由及选用设计模式的优缺点。

design-pattern-examples's People

Contributors

bwhyman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.