How Are Design Patterns Used Together? (Design Patterns 3)
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
How Are Design Patterns Used Together? (Design Patterns 3)
Hello friends, In our previous articles, we talked about the kinds of mistakes we make while trying to learn design patterns and and the techniques that can be applied to make them stick better. In this article, we’ll touch on some structurally fundamental topics and then examine the structural similarities of design patterns with each other and how they’re used together.
大家好,朋友们。在之前的文章中,我们讨论了在学习设计模式时容易犯的错误,以及如何通过技巧来加深记忆。在本文中,我们将探讨一些结构上的基础课题,并分析设计模式之间的结构相似性以及它们如何协同工作。
As we also mentioned in our first article, our goal is to make design patterns stay in our minds longer. Apart from that, we’ll often have to use design patterns nested within each other too. Depending on our needs, we can use two or more design patterns together. But knowing the structural similarities or differences while using them will make integration easier for us. That’s exactly why structural similarities are also touched on in many resources.
正如我们在第一篇文章中所提到的,我们的目标是让设计模式在脑海中停留得更久。此外,我们经常需要将设计模式嵌套使用。根据需求,我们可以将两种或多种模式结合起来。了解它们在结构上的异同点,将使集成变得更加容易。这正是许多资料中都会涉及结构相似性的原因。
In general, the most fundamental similarities across all of them come from applying principles. We can say that abstraction is the heart of design patterns. It’s one of the principles that should never be compromised. For example, so that upper classes don’t depend on lower classes, instances of lower classes aren’t created directly in upper classes. If you notice, in all of them, the interface (that is, the abstraction) of the related object is passed as a parameter, either through the constructor or by passing a parameter through another function. In other words, an object instance isn’t created directly.
总的来说,所有模式最根本的相似之处在于对原则的应用。我们可以说,抽象是设计模式的核心,这是绝不能妥协的原则之一。例如,为了使上层类不依赖于下层类,下层类的实例不会直接在上层类中创建。如果你注意观察,会发现所有模式中,相关对象的接口(即抽象)都是通过构造函数或函数参数传递的。换句话说,对象实例不会被直接创建。
Another similarity is that, as much as possible, the main object isn’t changed, or rather, it’s coded so that it won’t change. For this, either the main object is passed as a parameter to another object, or the other object is passed as a parameter to the main object. The texts where I talk about the relationships between design patterns are quoted from the refactoring guru website. I especially recommend that you visit the two resources below. refactoring guru
另一个相似之处是,尽可能不改变主对象,或者说,代码编写方式旨在避免改变它。为此,要么将主对象作为参数传递给另一个对象,要么将另一个对象作为参数传递给主对象。我讨论设计模式之间关系的文字引用自 refactoring guru 网站。我特别建议您访问以下两个资源。refactoring guru
Creational Design Patterns
创建型设计模式
They’re patterns that create objects instead of instantiating objects directly. This gives the program more flexibility in deciding which objects need to be created for a particular situation.
这些模式用于创建对象,而不是直接实例化对象。这为程序在特定情况下决定需要创建哪些对象提供了更大的灵活性。
Singleton: Purpose: Restricts object creation for a class to one instance. To put it a bit differently, it ensures that a single object is used by all the objects that need it. How It’s Done: in the first condition, for all requests that need the object, it’s checked whether the object has been created. If there’s no instance of the object yet, a lock statement is written on an object, and a second condition is placed inside this statement against the possibility of more than one request getting in, and if the instance still hasn’t been created, it’s created. As the requests that finish their work inside the lock exit the lock statement, every incoming request will see that the object has been created, so the object won’t be created again. Since a single facade object is sufficient in most cases, a Facade class can often be turned into a Singleton.
单例模式 (Singleton): 目的:限制一个类只能创建一个实例。换句话说,它确保所有需要该对象的组件都使用同一个实例。实现方式:在第一个条件中,对于所有需要该对象的请求,先检查对象是否已创建。如果尚未创建实例,则在对象上编写一个锁语句,并在该语句内放置第二个条件,以防止多个请求同时进入;如果实例仍未创建,则进行创建。当在锁内完成工作的请求退出锁语句后,每个后续请求都会看到对象已被创建,因此对象不会再次被创建。由于在大多数情况下单个外观对象就足够了,因此外观类 (Facade) 通常可以转换为单例。
Fly_weight would resemble Singleton if you somehow managed to reduce all the shared states of the objects to just one flyweight object. But there are two fundamental differences between these patterns: There should be only one Singleton instance, whereas a Flyweight class can have multiple instances with different intrinsic states. The Singleton object can be mutable. Flyweight objects are immutable.
如果你能设法将对象的所有共享状态缩减为一个享元对象,那么享元模式 (Flyweight) 会类似于单例模式。但这两种模式之间有两个根本区别:单例模式只能有一个实例,而享元类可以有多个具有不同内部状态的实例。单例对象可以是可变的,而享元对象是不可变的。
Factory Method: Purpose: Used to create instances from different classes derived from the same interface. It’s a single method. How Is It Done?: All the objects to be created are derived from the same interface. Then the related object is created with a method inside a factory object that is also derived from an interface. Many designs start by using Factory Method and evolve toward Abstract Factory, Prototype or Builder. Abstract Factory classes are often based on a set of Factory Methods, but you can also use Prototype to compose the methods in these classes. We can use Factory Method together with the iterator to let collection subclasses return different types of iterators that are compatible with the collections. Factory Method is a specialization of Template Method. In other words, a Factory Method can serve as a step inside a large Template Method.
工厂方法 (Factory Method): 目的:用于创建派生自同一接口的不同类的实例。它是一个单一的方法。实现方式:所有要创建的对象都派生自同一接口。然后,通过工厂对象内的一个方法来创建相关对象,该工厂对象也派生自一个接口。许多设计从使用工厂方法开始,并演变为抽象工厂、原型或生成器模式。抽象工厂类通常基于一组工厂方法,但你也可以使用原型模式来组合这些类中的方法。我们可以将工厂方法与迭代器结合使用,让集合子类返回与集合兼容的不同类型的迭代器。工厂方法是模板方法的一种特化。换句话说,工厂方法可以作为大型模板方法中的一个步骤。
Abstract Factory: Purpose: Creates a group of related but different objects. It’s a class, made up of multiple factory methods. It can even sometimes be made up of more than one factory class. How Is It Done?: All the objects to be created are derived from the same interface. Then the related objects are created with multiple methods inside a factory object that is also derived from an interface. If you want to produce more than one factory, more factory objects can be produced too. Many designs start by using Factory Method and evolve toward Abstract Factory, Prototype or Builder. Builder focuses on constructing complex objects step by step. Abstract Factory specializes in creating the members of related objects. While Abstract Factory returns the product immediately, Builder lets us run some additional steps before fetching the product. Abstract Factory classes are often based on a set of Factory Methods, but we can also use Prototype to compose the methods in these classes. Abstract Factory can serve as an alternative to Facade when you only want to hide the way subsystem objects are created from the client code. Abstract Factory, Builder and Prototype can be written as singletons.
抽象工厂 (Abstract Factory): 目的:创建一组相关但不同的对象。它是一个由多个工厂方法组成的类,有时甚至由多个工厂类组成。实现方式:所有要创建的对象都派生自同一接口。然后,通过派生自接口的工厂对象内的多个方法来创建相关对象。如果你想生产多个工厂,也可以创建更多的工厂对象。许多设计从工厂方法开始,演变为抽象工厂、原型或生成器。生成器专注于逐步构建复杂对象,而抽象工厂专注于创建相关对象的成员。抽象工厂立即返回产品,而生成器允许我们在获取产品之前执行一些额外步骤。抽象工厂类通常基于一组工厂方法,但我们也可以使用原型模式来组合这些类中的方法。当你只想向客户端代码隐藏子系统对象的创建方式时,抽象工厂可以作为外观模式的替代方案。抽象工厂、生成器和原型模式都可以写成单例。
Builder: Purpose: By separating the creation process/steps of a complex object from its representation, it allows different types of the same object to be created. How Is It Done?: In its simplest form, with one or more builder objects, the parts of the related objects are written so that they’re created in different functions. Then, with a director object, one or more builder objects are passed as parameters to the function that will create the related object. Many designs start by using
生成器 (Builder): 目的:通过将复杂对象的创建过程/步骤与其表示分离,允许创建同一对象的不同类型。实现方式:以最简单的形式,通过一个或多个生成器对象,将相关对象的各个部分编写在不同的函数中进行创建。然后,通过一个指导者 (Director) 对象,将一个或多个生成器对象作为参数传递给创建相关对象的函数。许多设计从使用…开始。