How to Write Design Patterns? (Design Patterns 2)
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Title: How to Write Design Patterns? (Design Patterns 2)
Hello friends, In our previous article, 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’re continuing from where we left off. Our goal is to be able to explain how to build a design pattern in a sentence or two. This way, with an explanation stripped of details, we’ll also make them stick better in our minds.
朋友们好,在上一篇文章中,我们讨论了在学习设计模式时容易犯的错误,以及可以用来帮助记忆的技巧。在本文中,我们将从上次中断的地方继续。我们的目标是用一两句话解释如何构建一个设计模式。通过这种去繁就简的解释,我们能更好地将其记在脑海中。
In the next article, we’ll touch on the points where design patterns have structural and purposeful things in common or where they differ from each other. Since we’ll touch on the relationships of each design pattern we examine with other patterns, being able to give short answers to at least the questions of what each one’s purpose is and how it’s written will make our job a lot easier. Now let’s continue by adding to the point where we left off in the previous article.
在下一篇文章中,我们将探讨设计模式在结构和目的上的共同点或差异。由于我们将涉及所研究的每种设计模式与其他模式之间的关系,因此能够至少对每种模式的目的和写法给出简短的回答,将使我们的工作变得容易得多。现在,让我们从上一篇文章结束的地方继续。
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. Example: For example, like everyone in the house using the fridge at home, or a group of neighbors on the street sharing a garbage container. When we need this kind of object while coding, the design pattern that will guarantee it is singleton.
单例模式:目的:将类的对象创建限制为一个实例。换句话说,它确保所有需要该对象的对象都使用同一个实例。示例:例如,就像家里每个人都使用同一个冰箱,或者街上的一群邻居共用一个垃圾桶。当我们在编码时需要这种对象时,能够保证这一点的设计模式就是单例模式。
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.
实现方式:在第一个条件中,对于所有需要该对象的请求,都会检查对象是否已被创建。如果还没有实例,则会在对象上编写一个锁(lock)语句,并在该语句内放置第二个条件,以防止多个请求同时进入。如果实例仍然没有被创建,则进行创建。当在锁内完成工作的请求退出锁语句时,每个后续请求都会看到对象已被创建,因此对象不会被再次创建。
Key Code Block:
private static readonly object _lock = new object();
public static Singleton GetInstance(string value) {
if (_instance == null) {
lock (_lock) {
if (_instance == null) {
_instance = new Singleton();
}
}
}
return _instance;
}
关键代码块:
private static readonly object _lock = new object();
public static Singleton GetInstance(string value) {
if (_instance == null) {
lock (_lock) {
if (_instance == null) {
_instance = new Singleton();
}
}
}
return _instance;
}
Factory Method: Purpose: Used to create instances from different classes derived from the same interface. It’s a single method. Example: For example, let’s think of a factory that produces different models of the same car brand. 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.
工厂方法:目的:用于从派生自同一接口的不同类中创建实例。它是一个单一的方法。示例:例如,想象一个生产同一汽车品牌不同车型的工厂。实现方式:所有要创建的对象都派生自同一个接口。然后,通过一个同样派生自接口的工厂对象中的方法来创建相关对象。
Key Code Block:
public interface IGuiFactory {
public IComponent CreateButton(string color);
public IComponent CreateTextBox(string color);
public IComponent CreateTable(string color);
}
public class GuiFactory:IGuiFactory {
public IComponent CreateButton(string color) {
var button = new Button(color);
button.Draw();
return button;
}
// ... (other methods)
}
关键代码块:
public interface IGuiFactory {
public IComponent CreateButton(string color);
public IComponent CreateTextBox(string color);
public IComponent CreateTable(string color);
}
public class GuiFactory:IGuiFactory {
public IComponent CreateButton(string color) {
var button = new Button(color);
button.Draw();
return button;
}
// ... (其他方法)
}
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. Example: For example, let’s think of a factory where different models of different car brands are produced. You could even add a third dimension, for example, using different factories to produce different models of different brands for different countries.
抽象工厂:目的:创建一组相关但不同的对象。它是一个由多个工厂方法组成的类。有时甚至可以由多个工厂类组成。示例:例如,想象一个生产不同汽车品牌不同车型的工厂。你甚至可以增加第三个维度,例如,使用不同的工厂为不同的国家生产不同品牌的不同车型。
How It’s 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.
实现方式:所有要创建的对象都派生自同一个接口。然后,通过一个同样派生自接口的工厂对象中的多个方法来创建相关对象。如果你想生产不止一个工厂,也可以创建更多的工厂对象。
Key Code Block:
public interface IUniversityFactory {
University CreateUniversity(int id, string name);
Course CreateCourse(int id, string name);
Student CreateStudent(int id, string name, int age);
Prof CreateProf(int id, string name, int age, Course course);
}
// ... (Program implementation)
关键代码块:
public interface IUniversityFactory {
University CreateUniversity(int id, string name);
Course CreateCourse(int id, string name);
Student CreateStudent(int id, string name, int age);
Prof CreateProf(int id, string name, int age, Course course);
}
// ... (程序实现)
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. Example: When we want to buy a car, we go to the dealership. We say which features we want, and after a certain time the car we want is produced at the factory and delivered. Naturally, the production.
建造者模式:目的:通过将复杂对象的创建过程/步骤与其表示分离,允许创建同一对象的不同类型。示例:当我们想买车时,我们会去经销商处。我们说明想要的配置,经过一段时间后,我们想要的车在工厂生产出来并交付。自然地,生产过程。