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

Explaining Abstract Factory
解释抽象工厂
Sometimes, we need to create a family of objects in a project. Imagine that we have an ecommerce and we have various kinds of products such as cell phones, notebooks, and
有时,我们需要在项目中创建一个对象族。想象一下,我们有一个电子商务,我们有各种各样的产品,如手机、笔记本和
tablets. These products are objects of the same family. If we create objects throughout a
平板。这些产品是同一系列的对象。如果我们在
software, we will face problems if we then need to modify the initialization process of this
软件方面,如果我们需要修改初始化对象过程,我们将面临的问题
object.


Using Abstract Factory will help us to solve problems including a system which should be
使用抽象工厂将帮助我们解决问题,包括一个应该
independent of how its products are created, a system that should use one of the multiple
独立于其产品是如何创建的,一个系统应该使用多个
families of products, and a system that should work with objects which are designed to be
产品系列,以及一个系统,该系统应与设计为
used together. Using this pattern will be beneficial as it isolates concrete classes. This means
一起使用。使用这个模式将是有益的,因为它隔离了具体的类。这意味着
that with this pattern, we can control which class of objects that can be initiated on
通过这个模式,我们可以控制可以初始化的对象类
software. Furthermore, it permits the exchange of products easily and provides consistency
软件。此外,它还可以方便地交换产品并提供一致性
among products.
在产品中。
The Abstract Factory pattern creates a single point of creation for objects and if we need to
如果我们需要创建一个抽象的工厂,那么我们需要创建一个抽象的工厂
change the algorithm of object creation, we need only modify the concrete factory. In the
改变对象创建的算法,只需要修改具体的工厂。在
following diagram, you can see the structure of Abstract Factory and how it is designed:
下图中,您可以看到在这里插入图片描述
抽象工厂的结构以及它是如何设计的:

重点词汇

In our example, the Abstract Factory’s structure has three main
在我们的示例中,抽象工厂的结构有三个主要部分
classes—AbstractFactory, Product, and Sale. The concrete classes of
类抽象工厂、产品和销售。具体类别
AbstractFactory are CellPhoneFactory, NotebookFactory, and TabletFactory.
AbstractFactory是CellPhoneFactory、NotebookFactory和TableFactory。
CellPhoneFactory is a concrete class responsible for creating the concrete classes
CellPhoneFactory是一个负责创建具体类的具体类
CellphoneProduct and CellphoneSale, NotebookFactory is a concrete class
CellphoneProduct and CellphoneSale, NotebookFactory是一个具体的类
responsible for creating the concrete classes NotebookProduct and NotebookSale, and
负责创建NotebookProduct和NotebookSale的具体类,以及
the TabletFactory is a concrete class responsible for creating the concrete classes
TabletFactory是一个具体类,负责创建具体类
TabletProduct and TabletSale. A Client is a class responsible for using
TabletProduct和TabletSale。客户端是负责使用
AbstractFactory to create AbstractProduct and AbstractSale. The concrete factory
AbstractFactory创建AbstractProduct和AbstractSale。具体工厂
is created at runtime and it then creates the concrete product and sale.
在运行时创建,然后创建具体的产品和销售。
The Abstract Factory pattern is sometimes used with another pattern such as Singleton,
抽象工厂模式有时与另一个模式(如Singleton)一起使用,
which we described earlier. Abstract Factory is a single point of creation, and often we need
我们之前已经描述过了。抽象工厂是一个单一的创作点,我们经常需要
only one instance of it in an entire system. With this, using a Singleton pattern can help us
整个系统中只有一个实例。因此,使用单例模式可以帮助我们
create a design better and more efficiently.
创造一个更好更有效的设计。
This pattern is often used in frameworks and APIs that have a difficult creation process for
这种模式通常用于那些对
an object, such as connections or sessions.
对象,如连接或会话。

Explaining Facade
解释门面
Projects can sometimes turn out to be very complex and big, making them difficult to
项目有时会变得非常复杂和庞大,这使得它们很难做到
design and organize. To solve this, a great solution is to break a system into subsystems
设计和组织。为了解决这个问题,一个很好的解决方案是将一个系统分解成子系统
(divide and conquer) and make them less complex and better organized.
(分而治之)并使之更简单,更有条理。
The Facade pattern creates a higher-level interface to hide a complexity of a set of interfaces
Facade模式创建了一个更高级别的接口,以隐藏一组接口的复杂性
in a subsystem. This pattern reduces the complexity and coupling, minimizing
在子系统中。这种模式降低了复杂性和耦合性,将
communication and dependencies between subsystems. In the following diagram, you can
子系统之间的通信和依赖性。在下图中,您可以
see the structure of Facade and how it is designed
看看门面的结构和它是如何设计的

扫描二维码关注公众号,回复: 12274911 查看本文章

在这里插入图片描述

In the preceding diagram, we can see the Facade pattern encapsulating all of the calls to
我们可以在前面的图中看到封装所有Facade的调用
subsystems and hiding these calls from the client. The system has one interface, Facade,
子系统,并对客户端隐藏这些调用。系统只有一个接口,Facade,
and the client calls this interface in order to call subsystems. Thus, clients does not call
客户端调用这个接口来调用子系统。因此,客户端不会调用
the subsystems directly. With this solution, the client doesn’t need to know about the
直接分系统。有了这个解决方案,客户不需要知道
subsystem and its complexity.
子系统及其复杂性。
This pattern is often used in projects and systems that have high complexity and need to be
此模式通常用于具有高度复杂性且需要
broken down into subsystems.
分解成子系统。

重点词汇

  • preceding 在…之前发生 ; 先于 ; 走在…前面 ; precede的现在分词

  • encapsulating 简述 ; 概括 ; 压缩 ; encapsulate的现在分词

  • all of 一共 ; 足足 ; 至少

  • in order to 为了;以便;目的在于 ; 要想……

  • does not 不 ; 不是 ; 否定就用

  • need to know 需了解;需知道

  • broken down 临时出故障的 ; 受损事故发生故障

Explaining Iterator
解释迭代器
Imagine that we want a way to access elements of an aggregate object sequentially without
设想一下,我们需要一种无需
exposing its internal structure. The Iterator pattern does just that.
暴露其内部结构。迭代器模式就是这样做的。
The Iterator pattern is responsible for sequentially accessing the aggregate object and
迭代器模式负责顺序访问聚合对象和
defining an interface to access the elements without exposing the internal structure. This
定义一个接口来访问元素而不暴露内部结构。这个
interface doesn’t put a new element on the aggregate object, but simply reads elements to it.
接口不在聚合对象上放置新元素,而只是向它读取元素。
In the following diagram, you can see the structure of an Iterator and how it is designed
在下面的图表中,您可以看到迭代器的结构以及它是如何设计的

在这里插入图片描述

In the preceding diagram, we can see the Aggregate and Iterator interfaces with their
在前面的图中,我们可以看到聚合和迭代器接口
concrete subclasses. The client is the class that uses the Iterator to access elements of
具体的子类。客户机是使用迭代器访问
Aggregate.
聚合。
This pattern is used on Java collections such as list, deque, and set. Understanding this
此模式用于Java集合,如list、deque和set。明白吗
pattern will help you to understand Java collections.
模式将帮助您理解Java集合。

Explaining Proxy
解释代理
Sometimes, creating a new object can be a big process and several rules can be involved in
有时,创建一个新对象可能是一个很大的过程,可能涉及到几个规则
creating this object. Imagine that we want to create a list of objects, and these objects
正在创建此对象。假设我们要创建一个对象列表,这些对象
represent telecommunication equipment, which has a lot of calculus to generate the
表示电信设备,它有很多微积分来生成
information of each object. As well as this, these objects will not be accessed at the same
每个对象的信息。同时,也不会同时访问这些对象
time but will be accessed on demand. A good strategy is to create each object when it is
时间,但将按需访问。一个好的策略是在每个对象都存在的时候创建它
accessed, thereby minimizing the cost and time it takes to create all objects and only access
访问,从而将创建所有对象和仅访问所需的成本和时间降至最低
some. The Proxy can help us to solve this
一些。代理可以帮助我们解决这个问题

The Proxy pattern is a pattern that surrogates an object instance (original object) to another
代理模式是将一个对象实例(原始对象)代理到另一个对象实例的模式
object instance (Proxy object) that permitting access control to the original object. In the
允许对原始对象进行访问控制的对象实例(代理对象)。在
following diagram, you can see the structure of Proxy and how it is designed:
下图中,您可以看到代理的结构以及它是如何设计的:

在这里插入图片描述

From the preceding diagram, we can see a structure of the Proxy pattern. If Subject is an
从前面的图中,我们可以看到代理模式的结构。如果主题是
interface that clients use to access object operations, then RealSubject is the class of the
接口,则RealSubject是
original object and Proxy is the class that works as a Proxy. Then, when the client accesses
原始对象和代理是用作代理的类。然后,当客户端访问
the object, they will access the Proxy object, and the Proxy object will then access the
对象,它们将访问代理对象,然后代理对象将访问
RealSubject object and return this object to the client.
RealSubject对象,并将此对象返回给客户端。
This pattern is used in frameworks and APIs that implement JPA specification and object
此模式用于实现JPA规范和对象关系映射的框架和api中
relational mapping (ORM).
关系映射(ORM)。

猜你喜欢

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