GithubHelp home page GithubHelp logo

designfirst's Introduction

源码设计模式解析和实战笔记

本代码库是通过ImageLoader工具函数,熟悉几种常用的设计模式

1. 设计模式六大原则

1.1 单一职责原则(优化代码的第一步)

定义:对于一个类而言,应该仅有一个引起它变化的原因。两个全完不一样的功能不应该放在一个类中,需要根据具体的业务,功能对类进行相应的拆分。比如demo中的DishCacheDoubleCacheMemoryCache等只做图片缓存,ImageLoader类只做图片加载,相互互不干扰。

1.2 开闭原则

定义: 软件中的对象(类、模块、函数等)对扩展是开放的,但对修改是封闭的。比如demo中ImageLoader可以做到不可修改,但是通过继承ImageCache,可以扩展程序。

1.3 里氏替换原则

1.4 依赖倒置原则

高层次的模块不依赖低层次的模块的实现细节的目的,依赖模块被颠倒另外。依赖倒置原则有如下几个关键点:

  • 高层模块不应该依赖低层模块,两者都应该依赖其抽象。比如demo中的ImageLoader为高层次的模块,DishCacheDoubleCacheMemoryCache等为低层次模块,两者不相互依赖,但其共同依赖其抽象。
  • 抽象不应该依赖细节。ImageCache只实现了putget方法
  • 细节应该依赖抽象。 DishCacheDoubleCacheMemoryCache依赖实现了抽象ImageCache

1.5 接口隔离原则

类间的依赖关系应该建立在最小的接口上。

1.6 迪米特原则

1.7 总结

在应用开发过程中,最难的不是完成应用的开发工作,而是在后续的升级、维护过程中让应用系统能够拥抱变化。拥抱变化也就意味着在满足需求且不破坏系统稳定性的前提下保持高可扩展性、高内聚、低耦合。在经历了各个版本的变更以后依赖保持清醒、灵活、稳定的系统架构。当然。这是一个比较理想的情况,但是我们必须朝着这个方向去努力,那么遵循面向对象六大原则就是我们走向灵活软件之路所迈出的第一步。

designfirst's People

Contributors

18360538648 avatar

Watchers

James Cloos avatar  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.