Java8设计模式最佳实战-设计模式概述(第一天学习记录)

Java8设计模式

Explaining design patterns
解释设计模式
Design patterns are sets of solutions to common design problems that occur over and over
设计模式是对反复出现的常见设计问题的一组解决方案
in development. They work as a solution template in which an abstract solution for a
正在开发中。它们用作解决方案模板,其中
common problem is described and the user then applies it, adapting it to their problem. In
描述了常见问题,然后用户应用它,使其适应他们的问题。在
object-oriented programming, the design pattern provides a way to design reusable classes
面向对象编程中,设计模式提供了一种设计可重用类的方法
and objects for a specific problem as well as defining the relationship between objects and
以及定义对象与对象之间的关系
classes. In addition, design patterns provide a common idiom among programming
类。此外,设计模式在编程中提供了一种常见的习惯用法
languages that allows architects and software developers to communicate about a common
允许架构师和软件开发人员就
and recurring problem regardless of the programming language they are using. With this,
以及反复出现的问题,而不管他们使用的是哪种编程语言。有了这个,
we are able to identify a problem and its solution by the name of the pattern and thinking
我们能够通过模式和思维的名称来识别问题及其解决方案
about a solution by a model point of view in a high abstraction level of language
从一个高抽象层次的语言模型的角度讨论一个解决方案
programming details.
编程细节。
The design patterns theme gained strength in 1994 after the Gang of Four (formed by Rich
1994年“四人帮”(Rich组建)之后,设计模式主题获得了发展
Gamma, Richard Helm, Ralph Johnson, and John Vlissides) wrote Design Patterns: Elements
Gamma、richardhelm、Ralph Johnson和johnvlissides)撰写了设计模式:元素
of Reusable Object‐Oriented Software. Here, they described 23 design patterns that were later
可重用的面向对象软件。后来在这里描述了23种模式
known as GoF design patterns and are still used today.
被称为GoF设计模式,至今仍在使用。

重点词汇

Explaining the Gang of Four design patterns
解释四人帮设计模式
The Gang of Four (GoF) design patterns are 23 patterns that are classified as creational patterns
四人帮(GoF)设计模式是23种被归类为创造性的模式

structural patterns, and behavioral patterns. The creational patterns control the
结构模式和行为模式。创造模式控制着
creation and initialization of the object and class selection; the structural patterns define the
对象和类选择的创建和初始化;结构模式定义
relationship between classes and objects, and the behavioral patterns control the
类和对象之间的关系,以及行为模式控制
communication and interaction between objects. As well as this, the GoF design patterns
对象之间的通信和交互。除此之外,GoF的设计模式
have two types of scope which define the focus of solutions. These scopes are object
有两种定义解决方案重点的范围。这些作用域是对象
scope, which resolves problems about object relations, and class scope, which resolves
scope用于解决有关对象关系的问题,类scope用于解决
problems about class relations.
关于类关系的问题。
The object scope works with composition and the behavior changes are done in a runtime.
对象作用域与组合一起工作,行为更改在运行时完成。
Thus, the object can have a dynamic behavior. The class scope works with inheritance and
因此,对象可以具有动态行为。类作用域使用继承和
its behavior is static-fixed at compile-time way. Then, to change the behavior of a classscope pattern, we need to change the class and recompile.
它的行为在编译时是静态固定的。然后,要更改类作用域模式的行为,我们需要更改类并重新编译。
Patterns classified as class scope solve problems about the relationship between classes and
此分类为类范围的模式解决了类之间关系
are static (fixed at compile time and cannot be changed once compiled). However, patterns
是静态的(在编译时是固定的,一旦编译就不能更改)。然而,模式
classified under the object scope solve problems about the relationship between objects and
在对象范围下进行分类,解决对象之间关系
can be changed at runtime
可以在运行时更改

重点词汇

  • As well as 既…又… ; 除…之外 ; 此外

  • types of 类型 ; 的型态 ; 债券的种类

    扫描二维码关注公众号,回复: 12274923 查看本文章
  • resolves 解决 ; 决心 ; 决定 ; 作出决定 ; 作出决议 ; 表决 ; 坚定的信

The following diagram shows us the three classifications, as well as their patterns and
下图显示了三种分类,以及它们的模式和
scope:
范围:

在这里插入图片描述

In the preceding diagram, we can see the Factory Method pattern on the Class section and
在前面的图中,我们可以在Class部分和
the Abstract Factory pattern on the Object section. This occurs because the Factory Method
对象部分上的抽象工厂模式。这是因为工厂方法
works with inheritance and the abstract method pattern works with composition. Then, the
处理继承,抽象方法模式处理组合。然后
Factory Method is static-fixed at compile time and cannot be changed after compilation.
工厂方法在编译时是静态固定的,编译后不能更改。
However, the Abstract Factory is dynamic and can be changed at runtime.
但是,抽象工厂是动态的,可以在运行时更改。
GoF design patterns are generally described using a graphical notation such as a use case
GoF设计模式通常使用图形表示法来描述,例如用例
diagram, and an example of the implementation’s code. The used notation must be able to
图,以及实现的代码示例。使用的符号必须能够
describe the classes and objects as well as the relationship between these classes and
描述类和对象以及这些类和
objects.
对象。
The pattern’s name is an important part of the design patterns. This is because it is what the
模式的名称是设计模式的重要组成部分。这是因为
developer uses to quickly identify the problem related to the pattern and to understand
开发人员用来快速识别与模式相关的问题并理解
how the pattern will solve it. The name of the pattern must be brief and refer to the problem
模式将如何解决它。模式的名称必须简短并引用问题
and its solution.
以及它的解决方案。

重点词汇

  • 在前面 farther on ; fore

  • 继承 inherit ; succeed ; carry on ; carry forward

  • 抽象方法 abstract method

  • 编译 compile ; translate and edit

  • 静态 static state ; quiescent condition ; steady state ; statics

  • 更改 change ; alter ; alteration

  • 抽象 abstract

pattern is a great tool for designing software development, but its use needs to be
模式是设计软件开发的一个很好的工具,但是它的使用需要
analyzed to determine if the design pattern is really required in order to solve the problem.
分析以确定是否确实需要设计模式来解决问题。

The catalog of Gang of Four design patterns
四人帮设计模式分类目录
Names of design patterns need be succinct, making them easy to identify. This is because
设计模式的名称必须简洁,使其易于识别。这是因为
design patterns create a vocabulary for communicating between developers independent of
设计模式创建了一个独立于
programming language, permitting developers to identify problems and solutions only by
编程语言,允许开发人员通过
name of a design pattern.
设计模式的名称。
In design patterns, a catalog is a set of pattern names which are designed to permit a better
在设计模式中,目录是一组模式名,它们被设计成允许更好的的沟通在
communication between developers.
开发人员之间。
The catalog of GoF’s design patterns has 23 patterns, as 在shown in the preceding diagram.
GoF的设计模式目录有23个模式,如上图所示。
Here is a description of these patterns:
以下是对这些模式的描述:

Abstract Factory: This provides an interface to create objects without specifying
抽象工厂:这提供了一个接口来创建对象,而无需指定
their concrete class, making it possible to decouple the business logic and
它们的具体类,使业务逻辑和
the object creation logic. With this, we can update the object creation logic in an
对象创建逻辑。这样,我们就可以更新对象的创建逻辑
easy way.
简单点。
Adapter: This provides an interface that makes it possible for two incompatible
适配器:这提供了一个接口,使两个不兼容的
interfaces to work together. The adapter pattern works as a bridge between
接口协同工作。适配器模式在
interfaces, adapting these interfaces to work together. Furthermore, the adapter
接口,调整这些接口以协同工作。此外,适配器
can adopt a class or objects.
可以采用一个类或对象。
Bridge: This pattern decouples an abstraction from its implementation, making
桥:这个模式将抽象从它的实现中分离出来,使得
them vary independently. With this, we can modify the implementations without
它们各不相同。有了它,我们可以修改实现而不需要
impacting the abstractions and we can also modify the abstractions without
影响抽象,我们也可以在没有
impacting the implementations. The class of abstraction hides implementations
影响实施。抽象类隐藏了实现
and its complexity.
以及它的复杂性。
Builder: This pattern separates the construction of a complex object from its
Builder:此模式将分开复杂对象的构造与其
representation. With this, we can construct the objects of several representations
表现 。有了它,我们就可以构造多个表示的对象
using the same process to that. Thus, we create a standard process of
用同样的方法。因此,我们创建了一个
construction of objects that have a complex process to construct.
构造一个复杂的过程。
Chain of responsibility: This pattern avoids coupling the sender and receiver of
责任链:这种模式避免了
a request creating some objects that have a chance to treat the requests. These
创建一些对象的请求,这些对象有机会处理这些请求。这些
objects create a chain of receiver objects for a sender’s request. Each object of this
对象为发送方的请求创建接收方对象链。每一个目标
chain receives the request and verifies whether or not it will treat this request.
chain接收请求并验证是否将处理此请求。
Command: This pattern encapsulates a request for an object and creates a
命令:此模式封装对对象的请求并创建
wrapper of requests containing their information about the request. With this, we
包含请求相关信息的请求包装器。有了这个,我们
can do a request to some object sending parameters without knowing about this
可以在不知道的情况下向某些对象发送参数
operation. Furthermore, the command permits us to execute an undo operation.
操作。此外,该命令允许我们执行撤消操作。
Composite: This pattern composes objects into a tree structure, which represents
复合:这个模式将对象组合成一个树结构,它表示
a part-whole hierarchy. It permits you to treat a group of objects as a single
半整体的等级制度。它允许您将一组对象视为单个对象
object.
对象。

重点词汇

  • decouple 分离,隔断

  • incompatible 不一致,不相配 ; 合不来,不能和睦相处 ; 不匹配 ; 配伍禁忌的 ; 不兼容 ; 互斥的

  • work together 一起工作

  • decouples 分离,隔断 ; decouple的第三人称单数

  • abstractions 抽象概念 ; 抽象 ; 出神 ; 心神专注 ; 提取 ; 抽取 ; 分离 ; abstraction的复数

Decorator: This pattern permit extends a functionality of a class with flexibility,
Decorator:此模式允许灵活地扩展类的功能,
without use subclass. It allows you to dynamically attach a new responsibility to
没有use子类。它允许您动态地为
an object.
一个物体。赋予新的责任
Facade: This hides the complexity of the system, applying a unified interface to a
Facade:这隐藏了系统的复杂性,将统一的接口应用于
set of interfaces on a subsystem. This makes the subsystem easy to use.
子系统上的一组接口。这使得子系统易于使用。
Factory Method: This defines an interface for creating an object, and the subclass
Factory方法:它定义了一个用于创建对象的接口和子类
states which class to initiate.
说明要启动哪个类。
Flyweight: This uses sharing to efficiently support a large number of finegrained objects. This pattern reduces the number of objects created.
享元:这使用共享来有效地支持大量细粒度对象。此模式可减少创建的对象数。
Interpreter: This pattern represents language grammar and uses it to interpret
解释器:这个模式代表语言语法并用它来解释
them as sentences of a language.
它们是一种语言的句子。
Iterator: This pattern provides a way to sequentially access the elements of a set
迭代器:这个模式提供了一种顺序访问集合元素的方法
of objects without knowing its underlying representation.
在不知道其基本表示形式的情况下。
Mediator: This reduces the complexity of communication by creating an object
Mediator:这通过创建一个对象来降低通信的复杂性
that encapsulates all the communication and interaction between objects.
它封装了对象之间的所有通信和交互。
Memento: This pattern captures the object’s internal states without hurting
纪念品:这个图案捕捉物体的内部状态而不会造成伤害
encapsulated concepts, with this, the state of the object can be restored by the
封装的概念,有了它,对象的状态可以通过
object. This pattern works as a backup that maintains the current state of an
对象。此模式作为一个备份来维护
object.
对象。
Observer: This defines a one-to-many dependency between objects. This means
观察者:这定义了对象之间的一对多依赖关系。这意味着
that if one object is modified, all of its dependents are automatically notified and
如果一个对象被修改,它的所有从属对象都会被自动通知,并且
updated.
更新。
Prototype: This pattern permits us to create a new object using an object or
原型:这个模式允许我们使用一个对象或
instance as a prototype. This pattern creates a copy of an object, creating a new
实例作为原型。此模式创建一个对象的副本,创建一个新的
object with the same state of the object used as a prototype.
对象的状态与用作原型的对象的状态相同。
Proxy: This pattern creates a surrogate object (proxy object) for another object
代理:此模式为另一个对象创建代理对象(代理对象)
(original object) in order to control the access to the original object.
(原始对象)以控制对原始对象的访问。
State: This permits an object to alter its behavior when its internal state changes.
状态:这允许对象在其内部状态发生变化时改变其行为。
Singleton: This ensures that a class has only one instance in the entire project,
Singleton:这确保了一个类在整个项目中只有一个实例,
and the same instance of the object is returned every time the creation process is
并且每次创建过程
performed/run.
执行/运行。
Strategy: This creates a family of algorithms, encapsulating each one and making
策略:这将创建一系列算法,封装每个算法并使
them interchangeable. This pattern permits you to change the algorithm at
它们可以互换。此模式允许您在
runtime
运行时
Template method: This defines a skeleton for an algorithm in an operation, and
模板方法:它定义了操作中算法的框架,并且
the subclass defines some steps to the algorithm. This pattern algorithm structure
子类定义了算法的一些步骤。这种模式算法结构
and the subclass redefine some steps of this algorithm without modifying its
子类在不修改算法的前提下重新定义了算法的一些步骤
structure.
结构。
Visitor: This represents an operation to be performed on an object structure. This
访问者:这表示要对对象结构执行的操作。这个
pattern permits us to add new operations to an element without modifying its
模式允许我们在不修改类情况下向元素添加新操作
class.

猜你喜欢

转载自blog.csdn.net/Coder_Boy_/article/details/110366971
今日推荐