php设计模式介绍_设计模式介绍

php设计模式介绍

What is Design Patterns?

什么是设计模式?

Design Patterns are models of code that solve classic problems. They are solutions to software design problems that you can find in a real-world application. A Design Pattern is not a code that is ready to be used in your application, but it is a model that you can use to solve a problem.

设计模式是解决经典问题的代码模型。 它们是您可以在实际应用程序中发现的软件设计问题的解决方案。 设计模式不是可以在您的应用程序中使用的代码,而是可以用来解决问题的模型

Design patterns are language neutral, so they can be applied to any language that supports object-orientation.

设计模式是语言无关的,因此它们可以应用于支持对象导向的任何语言。

Why and when should we use Design Patterns?

为什么以及何时应该使用设计模式?

Design Patterns exists to help us, developers. They enable us to implement tried and tested solutions (that is also used by other developers) to known problems, this way saving time and effort during the implementation of the code. They also define common words to talk about specific problems, making it easier to communicate, for example, you could say “you can use a factory in this case”, and other developers will know what they need to do.

设计模式可以帮助我们开发人员。 它们使我们能够实施久经考验的解决方案(也供其他开发人员使用)以解决已知问题,从而节省了代码实施过程中的时间和精力。 他们还定义了常用词来讨论特定问题,从而使沟通更加容易,例如,您可以说“在这种情况下可以使用工厂”,其他开发人员将知道他们需要做什么。

Sometimes you do not need a design pattern, and this is good to keep in mind, we should just use it when it’s necessary. Each design pattern can be used for a specific situation, and that’s one of the reasons why it’s important to know about them, because you can have a problem that is a known problem in the software development world and already exists a good solution to solve it, by using a design pattern. This way you do not need to ‘reinvent the wheel’, you can just use something that already exists and works well. So the best way to know when you should use it or not, it’s to learn them and understand which problem each design pattern solve.

有时您不需要设计模式,牢记这一点很好,我们仅在必要时使用它 。 每个设计模式都可以用于特定的情况,这就是了解它们很重要的原因之一,因为您可能遇到的问题是软件开发领域中的已知问题,并且已经存在解决该问题的好方法,通过使用设计模式。 这样,您就不需要“重新发明轮子”,您可以使用已经存在并且可以正常工作的东西。 因此,了解何时应该使用或不使用它的最佳方法是学习它们并了解每种设计模式可以解决哪些问题。

Gang of Four (GoF)

四人帮(GoF)

In the year of 1995, four developers, Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, also known as ‘Gang of Four’ (GoF), published the book ‘Design Patterns: Elements of Reusable Object-Oriented Software’, and they introduced the concept of patterns, introducing 23 patterns of design. Those patterns from Gang of Four (GoF), are generally considered the foundation for all other patterns, and they are categorized into three groups:

在1995年,四位开发人员Erich Gamma,Richard Helm,Ralph Johnson和John Vlissides,也被称为“四人帮”(GoF),出版了《设计模式:可重用的面向对象软件的元素》一书,他们介绍了模式的概念,介绍了23种设计模式。 来自“四人帮”(GoF)的那些模式通常被认为是所有其他模式的基础,它们分为三类:

  • Creational — Concern with the process of creation (construction) of objects.

    创建性的 -与对象的创建(构造)过程有关。

  • Structural — Deals with the composition of classes and objects.

    结构性 -处理类和对象的组成。

  • Behavioral — Characterize the ways in which classes or objects interact and distribute responsibility.

    行为 -表征类或对象交互和分配责任的方式。

The elements of a Design Pattern

设计模式的要素

The book ‘Design Patterns’, says that a pattern has four essential elements:

《设计模式》一书说,模式具有四个基本要素:

  • A name — that is used to describe a design problem, the solution and the consequences.

    名称 -用于描述设计问题,解决方案和后果的名称。

  • The problem — that describes when we need to apply the pattern, explaining the problem and its context.

    问题 -描述何时需要应用模式,解释问题及其背景。

  • The solution — that describes the elements that make up the design, the relationships, the responsibilities and collaborations. The solutions do not describe a concrete implementation, because a pattern is like a template which can be applied in many different situations, but provides an abstract description of a design problem and how a general arrangement of elements (classes and objects) solves it.

    解决方案 -描述构成设计的元素,关系,职责和协作。 这些解决方案没有描述具体的实现,因为模式就像可以在许多不同情况下应用的模板一样,但是提供了对设计问题的抽象描述,以及元素(类和对象)的一般布置如何解决该问题。

  • The consequences — that are the results of applying the pattern.

    结果 -是应用模式的结果。

Here there is a brief explanation about each of the 23 patterns. Those definitions are also from the book ‘Design Patterns’ from the GoF:

这里有23种模式的简要说明。 这些定义也来自GoF的《设计模式》一书:

Creation Pattern

创作模式

  • Abstract Factory — Provide an interface for creating families of related or dependent objects without specifying their concrete classes.

    抽象工厂 —提供一个用于创建相关或相关对象族的接口,而无需指定其具体类。

  • Build — Separate the construction of a complex object from its representation so that the same construction process can create different representations.

    构建 —将复杂对象的构造与其表示分离,以便同一构造过程可以创建不同的表示。

  • Factory Method — Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

    工厂方法 —定义用于创建对象的接口,但让子类决定要实例化的类。 工厂方法允许类将实例化延迟到子类。

  • Prototype — Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

    原型 -指定使用原型实例创建的对象的种类,并通过复制此原型来创建新对象。

  • Singleton — Ensure a class only has one instance, and provide a global point of access to it.

    单例 —确保一个类只有一个实例,并提供对其的全局访问点。

Structural Pattern

结构模式

  • Adapter — Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.

    适配器 -将类的接口转换为客户端期望的另一个接口。 适配器使类可以协同工作,否则由于接口不兼容而无法实现。

  • Bridge — Decouple an abstraction from its implementation so that the two can vary independently.

    —将抽象与其实现分离,以便两者可以独立变化。

  • Composite — Compose objects into tree structures to represent part-whole

    合成 —将对象合成为树结构以表示整体

    hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

    层次结构。 复合可以使客户统一对待单个对象和对象组成。

  • Decorator — Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

    装饰器 -动态将附加职责附加到对象。 装饰器为子类提供了灵活的替代方案,以扩展功能。

  • Facade — Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

    外观 —为子系统中的一组接口提供统一的接口。 Facade定义了一个更高级别的界面,使子系统更易于使用。

  • Flyweight — Use sharing to support large numbers of fine-grained objects

    Flyweight —使用共享来支持大量细粒度的对象

    efficiently.

    有效率的。

  • Proxy — Provide a surrogate or placeholder for another object to control access to it.

    代理 -为另一个对象提供代理或占位符,以控制对其的访问。

Behavioral Pattern

行为模式

  • Chain of Responsibility — Avoid coupling the sender of a request to its 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 an object handles it.

    责任链 —通过给多个对象一个处理请求的机会,避免将请求的发送者与其接收者联系起来。 链接接收对象,并将请求沿着链传递,直到对象处理该请求。

  • Command — Encapsulate a request as an object, thereby letting you parameterized clients with different requests, queue or log requests, and support undoable operations.

    命令 -将请求封装为对象,从而使您可以为具有不同请求,队列或日志请求的客户端参数化,并支持可撤销的操作。

  • Interpreter — Given a language, define a represention for its grammar along with an interpreter that uses the representation to interpret sentences in the language.

    口译员 -给定一种语言,请定义其语法的表示形式以及使用该表示形式来解释该语言句子的解释器。

  • Iterator — Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation.

    迭代器 —提供一种在不暴露其基础表示的情况下顺序访问聚合对象的元素的方法。

  • Mediator — 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.

    介体 —定义一个封装一组对象如何交互的对象。 介体通过防止对象之间显式地相互引用来促进松散耦合,并且它使您可以独立地更改其交互。

  • Memento — Without violating encapsulation, capture and externalize an object’s internal state so that the object can be restored to this state later.

    备忘录 —在不违反封装的情况下,捕获并外部化对象的内部状态,以便以后可以将对象恢复到此状态。

  • Observer — Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

    观察者(Observer) -定义对象之间的一对多依赖关系,以便当一个对象改变状态时,其所有依赖关系都会得到通知并自动更新。

  • State — Allow an object to alter its behavior when its internal state changes. The object will appear to change its class.

    状态 —允许对象在其内部状态更改时更改其行为。 该对象似乎将更改其类。

  • Strategy — Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

    策略 -定义一系列算法,封装每个算法,并使它们可互换。 策略使算法独立于使用该算法的客户端而变化。

  • Template Method — Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

    模板方法 -定义操作中算法的框架,将某些步骤推迟到子类。 模板方法允许子类在不更改算法结构的情况下重新定义算法的某些步骤。

  • Visitor — Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

    访客 —代表要在对象结构的元素上执行的操作。 访问者使您可以定义新操作,而无需更改其所操作元素的类。

Conclusion

结论

Design Patterns exists to help us, developers. Of course that also exists an initial learning curve to know how to work with them, but once that you learned it, they can make your life easier. In the next articles, I will show examples of how we can use some of those patterns.

设计模式可以帮助我们开发人员。 当然,这也存在一个初步的学习曲线,以了解如何与他们合作,但是一旦您学会了它,它们便可以使您的生活更轻松。 在下一篇文章中,我将展示一些示例,说明如何使用这些模式。

Thanks for reading!

谢谢阅读!

dofactory — .NET Design Patterns

工厂— .NET设计模式

Design Patterns: Elements of Reusable Object-Oriented Software — Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Grady Booch

设计模式:可重用的面向对象软件的元素-Erich Gamma,Richard Helm,Ralph Johnson,John Vlissides,Grady Booch

翻译自: https://medium.com/swlh/design-patterns-introduction-220f811db857

php设计模式介绍

你可能感兴趣的:(php,设计模式,python)