设计模式概述以及23种设计模式的介绍

设计模式概述以及23种设计模式介绍
设计模式概述(Design pattern):
设计模式:代表了最佳的实践,通常被有经验的⾯向对象的软件开发⼈员所采⽤。设计模式是软件开发⼈员在软件开发过程中⾯临的⼀般问题的解决⽅案。这些解决⽅案是众多软件开发⼈员经过相当长的⼀段时间的试验和错误总结出来的。
设计模式是⼀套被反复使⽤的、多数⼈知晓的、经过分类编⽬的、代码设计经验的总结。使⽤设计模式是为了重⽤代码、让代码更容易被他⼈理解、保证代码可靠性。 毫⽆疑问,设计模式于⼰于他⼈于系统都是多赢的,设计模式使代码编制真正⼯程化,设计模式是软件⼯程的基⽯,如同⼤厦的⼀块块砖⽯⼀样。项⽬中合理地运⽤设计模式可以完美地解决很多问题,每种模式在现实中都有相应的原理来与之对应,每种模式都描述了⼀个在我们周围不断重复发⽣的问题,以及该问题的核⼼解决⽅案,这也是设计模式能被⼴泛应⽤的原因。
GOF介绍:在 1994 年,由 Erich Gamma、Richard Helm、Ralph Johnson 和 John Vlissides 四⼈合著出版了⼀本名为 Design Patterns - Elements of Reusable Object-Oriented Software(中⽂译名:设计模式 - 可复⽤的⾯向对象软件元素) 的书,该书⾸次提到了软件开发中设计模式的概念。
四位作者合称 GOF(四⼈帮,全拼 Gang of Four)。他们所提出的设计模式主要是基于以下的⾯向对象设计原则。
山河之恋
对接⼝编程⽽不是对实现编程。
优先使⽤对象组合⽽不是继承。
总的来说设计模式分为3⼤类:
竹胶合模板· 创建型模式(Creational Patterns):(都是⽤来帮助我们创建对象的)这些设计模式提供了⼀种在创建对象的同时隐藏创建逻辑的⽅式,⽽不是使⽤ new 运算符直接实例化对象。这使得程序在判断针对某个给定实例需要创建哪些对象时更加灵活。主要包括:
1. 单例模式(Singleton Pattern):保证⼀个类仅有⼀个对象,并提供⼀个访问它的全局访问点。(Ensure a class only has one instance,and provide
a globe point of access to it.)
详细单例模式介绍可点此链接:
⽤Python实现单例模式:
2. ⼯⼚模式(Factory Pattern):定义⼀个⽤于创建对象的接⼝,让⼦类决定将哪⼀个类实例化,FactoryMethod使⼀个类的实例化延迟到其⼦类。
(Define an interface for creating anobject, but let subclasses decide which class to instantiate. Factory methodlets a class defer instantiation to subclasses.)
详细⼯⼚模式介绍可点此链接:
Python实现⼯⼚模式:
3. 抽象⼯⼚模式(Abstract Factory Pattern):提供⼀个创建⼀系列相关或相互依赖对象的接⼝,⽽⽆需指定它们的具体类。(Provide an
interface for creating families of related or dependent object without specifying their concrete classes.)
详细抽象⼯⼚模式介绍可点此链接:
4. 建造者模式(Builder Pattern):将⼀个复杂对象的构建与它的表⽰分离,使得同样的构建过程可以创建不同的表⽰。(Separate the
construction of a complex object from its representation so that the same construction process can create different representations.)
四川省西昌市详细建造者模式介绍可点此链接:
5. 原型模式(Prototype Pattern):⽤原型实例指定创建对象的种类,并且通过拷贝这个原型来创建新的对象。(Specify the kinds of objects to
create using a prototypical instance, and create new objects by copying the prototype.)
详细原型模式介绍可点此链接:
卫生带
·结构型模式(Structural Patterns):这些设计模式关注类和对象的组合。继承的概念被⽤来组合接⼝和定义组合对象获得新功能的⽅式。主要包括:
1. 适配器模式(Adapter Pattern):将⼀个类的接⼝转换成客户希望的另⼀个接⼝。Adapter模式使得原本由于接⼝不兼容⽽不能⼀起⼯作的那些类
可以⼀起⼯作。(Convert the interface of a class into another interface clients except. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.)
详细适配器模式介绍可点此链接:
2. 桥接模式(Bridge Pattern):将抽象部分与它的实现部分分离,使他们都可以独⽴地变化。(Decouple an abstraction from its implementation
so that the two can vary independently.)
详细桥接模式介绍可点此链接:
3. 装饰模式(Decorator Pattern):动态地给⼀个对象添加⼀些额外的职责。就扩展功能⽽⾔,Decorator模式⽐⽣成⼦类的⽅式更为灵活。
(Attach additional responsibilities to an object dynamically. Decorators provides a flexible alternative to subclasses for extending functionality.)详细装饰模式介绍可点此链接:
4. 组合模式(Composite Pattern):将对象组合成树形结构以表⽰“部分-整体”的层次结构。Composite 使得客户对单个对象和复合对象的使⽤具有
⼀致性。(Compose object into tree structures torepresent part-whole hierarchy. Composite lets clients treat individual objectsand compositions of objects uniformly.)
详细组合模式介绍可点此链接:
5. 外观模式(Facade Pattern):为⼦系统中的⼀组接⼝提供⼀个⼀致的接⼝。Façade模式定义了⼀个⾼层接⼝,这个接⼝使得这⼀⼦系统更加容
易使⽤。(Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.)
详细外观模式介绍可点此链接:
6. 享元模式(Flyweight Pattern):运⽤共享技术有效地⽀持⼤量细粒度的对象。(Use sharing to support large numbers offine-grained objects
efficiently.)
详细享元模式介绍可点此链接:
7. 代理模式(Proxy Pattern):为其他对象提供⼀个代理以控制对这个对象的访问。(Provide a surrogate or placeholder foranther object to control
access to it.)
详细代理模式介绍可点此链接:
· ⾏为型模式(Behavioral Patterns):这些设计模式特别关注对象之间的通信。主要包括:
1. 模版⽅法模式(Template Pattern):定义⼀个操作中的算法的⾻架,⽽将⼀些步骤延迟到⼦类。TemplateMethod 使得⼦类可以不改变⼀个算法
的结构即可重定义该算法的某些特定步骤。(Define the skeleton of an algorithm inan operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing thealgorithm’s structure.)
详细模版⽅法模式介绍可点此链接:
2. 命令模式(Command Pattern):将⼀个请求封装为⼀个对象,从⽽使你可⽤不同的请求对客户进⾏参数化,对请求排队或记录请求⽇志,以及
⽀持可取消的操作。(Encapsulate a request as an object,thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.)
详细命令模式介绍可点此链接:
分享派3. 迭代器模式(Iterator Pattern):提供⼀种⽅法顺序访问⼀个聚合对象中各个元素,⽽⼜不需暴露该对象的内部表⽰。(Provide a way to access
the elements of an aggregate object sequentially without exposing its underlying representation.)
详细迭代器模式介绍可点此链接:
4. 观察者模式(Observer Pattern):定义对象间的⼀种⼀对多的依赖关系,以便当⼀个对象的状态发⽣改变时,所有依赖于它的对象都得到通知并
凤凰卫星电视⾃动刷新。(Define a one-to-many dependency between objects so that when one object changes state all its dependents are notified and updated automatically.)
详细观察者模式介绍可点此链接:
5. 中介者模式(Mediator Pattern):⽤⼀个中介对象来封装⼀系列的对象交互。中介者使各对象不需要显⽰地相互引⽤,从⽽使其耦合松散,⽽且
可以独⽴地改变它们之间的交互。(Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly and it lets you vary their interaction independently.)
详细中介者模式介绍可点此链接:
6. 备忘录模式(Memento Pattern):在不破坏封装性的前提下,捕获⼀个对象的内部状态,并在该对象之外保持该状态,这样以后就可以将该对
象恢复到保存的状态。(Without violating encapsulates, captureand externalize an object’s internal state so that the object can be restored to this state later.)
详细备忘录模式介绍可点此链接:
7. 解释器模式(Interpreter Pattern):定义⼀个语⾔,定义它的⽂法的⼀种表⽰,并定义⼀个解释器,该解释器使⽤该表⽰来解释语⾔中的句⼦。
(Given a language, define are presentation for its grammar along with an interpreter that uses there presentation to interpret sentences in the language.)
详细解释器模式介绍可点此链接:
8. 状态模式(State Pattern):允许⼀个对象在其内部状态改变时改变它的⾏为。对象看起来似乎修改了它所属的类。(Allow an object to alter its
behavior when its internal state changes. The object will appear to change its class.)
详细状态模式介绍可点此链接:
9. 策略模式(Strategy Pattern):定义⼀系列的算法,把它们⼀个个封装起来,并且使他们可相互替换。本模式使得算法的变化可以独⽴于使⽤它的
客户。(Define a family of algorithms,encapsulate each one and make them interchangeable. Strategy lets the algorithmvary independently from clients that use it.)
详细策略模式介绍可点此链接:
10. 责任链模式(Chain of Responsibility Pattern):为解除请求的发送者和接收者之间的耦合,⽽使多个对象都有机会处理这个请求。将这些对
象连成⼀条链,并沿着这条链传递该请求,直到有对象处理它。(Avoid coupling the sender of a requestits receiver by giving more than one object a chance to handle the request.Chain the receiving objects and pass the request along the chain until anobject handles it.)
详细责任链模式介绍可点此链接:
11. 访问者模式(Visitor Pattern):表⽰⼀个作⽤于某对象结构中的各元素的操作。它使你可以在不改变各元素类别的前提下定义作⽤于这些元素的新
操作。(Represent an operation to be performedon the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.)
详细访问者模式介绍可点此链接:
此外还有⼀些J2EE模式:这些设计模式特别关注表⽰层。这些模式是由 Sun Java Center 鉴定的。
1. MVC 模式(MVC Pattern)
2. 业务代表模式(Business Delegate Pattern)
3. 组合实体模式(Composite Entity Pattern)
4. 数据访问对象模式(Data Access Object Pattern)
5. 前端控制器模式(Front Controller Pattern)
6. 拦截过滤器模式(Intercepting Filter Pattern)
7. 服务定位器模式(Service Locator Pattern)
8. 传输对象模式(Transfer Object Pattern)
下⾯⽤⼀个图⽚来整体描述⼀下设计模式之间的关系:
谢谢

本文发布于:2024-09-21 18:52:21,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/422942.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:模式   对象   介绍   设计模式   请求
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议