设计模式
学无止境jl
学习,运动,动手动脑!
展开
-
设计模式之状态模式State Pattern详解Java版
一、定义The State Pattern allows an object to alter its behaviorwhen its internal state changes. The object will appear tochange its class.状态模式允许一个对象当它的内部状态改变时,改变它的行为。对象能够改变它的类。类图:二、实例实现口香糖自动贩卖机的状态变化:翻译:No Quarter(默认状态),insert quarter(投入硬币),Has Quar原创 2020-07-29 22:44:36 · 191 阅读 · 0 评论 -
设计模式之组合模式Composite Pattern详解Java版
一、定义The Composite Pattern allows you tocompose objects into tree structures torepresent part-whole hierarchies. Compositelets clients treat individual objects andcompositions of objects uniformly.组合模式允许你组合对象到树结构中来代表部分和整体的层次结构。组合让客户端统一对待单个对象和组合对象。二原创 2020-07-28 19:47:29 · 200 阅读 · 0 评论 -
设计模式之迭代器模式Iterator Pattern详解Java版
一、定义The Iterator Pattern provides a way toaccess the elements of an aggregate objectsequentially without exposing its underlyingrepresentation.迭代器模式在不暴露底层实现的情况下,提供了一种访问聚合对象的各个元素的方法。二、实例有两个班级,一班使用了ArrayList来管理学生信息,二班使用了数组Array来管理学生信息,现有学生信息调查小组来对两个班原创 2020-07-28 19:06:43 · 215 阅读 · 0 评论 -
设计模式之模板方法模式Template Method Pattern-Java版
一、定义The Template Method Pattern defines the skeletonof an algorithm in a method, deferring some steps tosubclasses. Template Method lets subclasses redefinecertain steps of an algorithm without changing thealgorithm’s structure.模板方法模式定义了一种算法的框架,将部分步骤原创 2020-07-27 18:32:35 · 82 阅读 · 0 评论 -
设计模式之门面模式+迪米特法则(最少知识法则)Facade Pattern-Java版
一、定义The Facade Pattern provides a unified interface to aset of interfaces in a subsytem. Facade defines a higher-level interface that makes the subsystem easier to use.门面模式为子系统中一系列的接口提供了一个统一接口。门面模式定义了更高级的接口,使得子系统更加容易使用。门面模式也是一个容易理解的模式,就是把一系列的操作都整合到一个原创 2020-07-26 22:20:44 · 226 阅读 · 0 评论 -
设计模式之适配器模式Adapter Pattern详解-Java版
一、定义The Adapter Pattern converts the interface of a classinto another interface the clients expect. Adapter letsclasses work together that couldn’t otherwise because ofincompatible interfaces.适配器模式转换一个类的接口为客户端所需要的另一个接口。适配器让多个类协同工作,否则会因为接口不兼容而不能工作。适原创 2020-07-26 21:19:25 · 183 阅读 · 0 评论 -
设计模式之命令模式Command Pattern-Java版
一、定义The Command Pattern encapsulates a request as anobject, thereby letting you parameterize other objectswith different requests, queue or log requests, and supportundoable operations.命令模式把请求封装为一个对象,因此,你可以使用不同的请求、队列或日志请求参数化其他对象,并支持可撤消的操作。从定义中,可以得知原创 2020-07-25 18:51:05 · 89 阅读 · 0 评论 -
设计模式:一文搞定单例模式(防止反射、反序列化、clone破坏单例)Singleton Pattern-Java版
一、定义单例模式,顾名思义,就是一个类从始至终只产生一个对象。现实生活中的例子有很多,比如在太阳系考虑问题,那么太阳和地球都可称为单例,再比如工具类,有时候没有把所有方法用static修饰(这不是个好办法),就应该把它做成单例,因为它没有不变的状态。二、五种单例模式:饿汉式、懒汉式、双重检查锁、静态内部类、枚举(一) 饿汉式,加载类时马上创建对象/** * 饿汉式 * 优点:线程安全、效率高 * 缺点:不能做到“即用即创建”,有可能浪费内存资源 */public class Singlet原创 2020-07-23 13:41:33 · 526 阅读 · 0 评论 -
设计模式之简单工厂-工厂方法-抽象工厂Factory Pattern-Java版
一、简单工厂简单工厂不是一个设计模式,但是可以使客户端从依赖具体实现中解耦出来。例子:披萨店有多种不同的披萨,下列代码表示了生产披萨的过程:public class PizzaStore{ Pizza orderPizza(String type){ Pizza pizza=null; if("cheese".equals(type)){ pizza=new CheesePizza(); } else if("pepperoni".equals(type)) { pizza=原创 2020-07-22 12:18:44 · 123 阅读 · 0 评论 -
设计模式之装饰模式Decorator Pattern-Java版
一、定义《Head First Design Patterns》:The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.即:装饰模式将额外的责任动态地附加到一个对象上。装饰器为扩展功能提供了一种灵活的子类化可选方法。简单原创 2020-07-20 21:58:11 · 168 阅读 · 0 评论 -
设计模式之观察者模式Oberver Pattern-Java版
一、观察者模式《Head First GOF》:The Observer Pattern defines a one-to-manydependency between objects so that when oneobject changes state, all of its dependents arenotified and updated automatically.即:观察者模式在对象之间定义了一对多的依赖,当一个对象状态改变了,它的全部依赖都会自动被通知和更新。简单说,就是一原创 2020-07-19 22:37:43 · 150 阅读 · 0 评论 -
设计模式之策略模式Strategy Pattern-Java版
一、策略模式定义《Head First GOF》:The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes theminterchangeable. Strategy lets the algorithm vary independently from clients that use it.即:策略模式定义了算法族,把算法族封装起来,使得它们之间可以切换。策略让算法独立于使用策略模式原创 2020-07-19 20:46:57 · 126 阅读 · 0 评论