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

Understanding the advantages of design
了解设计的优势
patterns
模式
Creating an object-oriented design is a tough task. This is because we have several
创建面向对象的设计是一项艰巨的任务。这是因为我们有几个
important elements to think about with regard to the scenario that we’ll work in and the
关于我们将要工作的场景和
problem we’ll solve. This includes defining the appropriate objects that we need to create in
我们要解决的问题。这包括定义需要在其中创建的适当对象
order to reach the solution; defining the granularity of objects and looking at what
以达到解决方案;定义对象的粒度并查看
interfaces we need to create. These tasks need to be addressed during the creation process
我们需要创建的接口。这些任务需要在创建过程中解决
in design. Objects can be created to represent a real-world object or to represent a process
在设计上。可以创建对象来表示真实世界的对象或表示过程
with its algorithms and responsibilities. Furthermore, we even need to consider the number
以及它的算法和职责。此外,我们甚至需要考虑这个数字
of objects, their size, and the interface we need to access.
对象的大小,以及我们需要访问的接口。
Design patterns are great tools for helping us to identify classes and objects that don’t
设计模式是一个很好的工具,它可以帮助我们识别出不存在的类和对象
represent real-world objects and objects that are less-obvious abstractions. Moreover,
表示真实世界的对象和不太明显的抽象对象。而且,
design patterns help us to apply the finest granularity to objects and they also allow us to
设计模式帮助我们将最细的粒度应用于对象,它们也允许我们
analyze a problem and solution as a model. Design patterns make the design flexible,
分析问题和解决方案作为模型。设计模式使设计更加灵活,
providing a decoupling between classes and objects. They also provide the ability to
提供类和对象之间的解耦。它们还提供了
organize solutions, allowing delegate responsibilities to classes with the best way.
组织解决方案,允许以最佳方式将职责委派给类。
Building software is an expensive process for companies because it requires capable
对于公司来说,构建软件是一个昂贵的过程,因为它需要有能力的
professionals and infrastructure to build and maintain the software. Design patterns, with
构建和维护软件的专业人员和基础设施。设计模式,使用
their flexibility and decoupled design, make maintenance easy and therefore decrease its
他们的灵活性和解耦设计,使维护容易,因此减少了它
cost.
成本。

Understanding the basic design patterns of
了解 基础的设计模式
the Java world
Java世界
All GoF patterns have a good purpose and solve major problems of object-oriented design,
所有GoF模式都有很好的用途,解决了面向对象设计的主要问题,
but some patterns are most commonly used in the Java and Java EE ecosystem. In this book,
但是有些模式在Java和javaee生态系统中最常用。在这本书里,
these patterns are treated as basic design patterns because they are most commonly used to
这些模式被视为基本设计模式,因为它们最常用于
implement solutions on Java’s APIs, frameworks, and algorithms. Consequently,
在Java的api、框架和算法上实现解决方案。因此,
understanding these patterns will help us to understand these APIs, frameworks, and
理解这些模式将有助于我们理解这些api、框架和
algorithms, and we’ll, in turn, be able to create a better solution using Java. These patterns
算法,然后我们将能够使用Java创建一个更好的解决方案。这些模式
are Singleton, Abstract Factory, Facade, Iterator, and Proxy
是Singleton、抽象工厂、Facade、Iterator和Proxy

Explaining Singleton
解释单例
In a software project, in some solutions, we may want to ensure that a class has only one
在软件项目中,在某些解决方案中,我们可能希望确保一个类只有一个
instance of an object throughout the project and that this object is accessible at any point in
对象的实例,并且此对象在
the project. Creating a global instance or static instance will not ensure that this class will
项目。创建全局实例或静态实例不能确保该类
not be used at another point in another instance. The best way to solve this is by using the
不能在另一个实例的另一个点上使用。解决这个问题的最好方法是使用
Singleton pattern, which ensures that there is only one instance of a class in the entire
单例模式,它确保整个类中只有一个实例
project. In the following diagram, we are showing the structure of Singleton and how it is
项目。在下面的图表中,我们展示了Singleton的结构以及它是怎样的
designed:
设计:

在这里插入图片描述

Here, we have one class called Singleton which has a private constructor, as well as a
这里,我们有一个名为Singleton的类,它有一个私有构造函数,以及一个
reference variable of Singleton and a method for returning its unique instance. A good
Singleton的引用变量及其唯一实例的返回方法。好的
example of an application is a situation in which we want to create a class responsible for
应用程序的示例是这样一种情况,我们希望创建一个类来负责
application configurations (paths to some resource, parameters to access filesystems,
应用程序配置(某些资源的路径、访问文件系统的参数,
behaviors of the environment). Often, the application has some configurations and we need
环境行为)。通常,应用程序有一些配置,我们需要
a class to represent these application configurations. Thus, this class of application
表示这些应用程序配置的类。因此,这类应用程序
configuration doesn’t need various instances, but only one instance.
配置不需要各种实例,只需要一个实例。
Another application of Singleton is when we want to create an Abstract Factory that will be
Singleton的另一个应用是当我们想要创建一个抽象工厂时
explained in the following subsection. Generally, we will have only one Abstract Factory
在下面的小节中解释。一般来说,我们只有一个抽象工厂
throughout the application. With this, we can use a Singleton to guarantee that we will
在整个应用程序中。有了这个,我们可以用一个单例来保证我们将
have only one instance of Abstract Factory.
只有一个抽象工厂实例。
This pattern is often used in frameworks and APIs, but it is common for this pattern to be
这种模式经常在框架和api中使用,但是这种模式很常见
found in the code of projects, mainly on Java EE.
在项目代码中找到,主要是在javaee上。
The use of the Singleton pattern can be a good practice depending on the scenario, but
根据场景的不同,使用Singleton模式可能是一个很好的实践,但是
depending on the scenario the use of Singleton can be a bad practice. The Singleton should
根据场景的不同,使用Singleton可能是一种不好的做法。单例应该
not be used when the object is stateful and maintain a state, because with Singleton the
当对象是有状态且保持状态时不使用,因为使用Singleton
same instance of the object is shared by all processes of application and if some process
对象的同一实例由应用程序的所有进程共享,如果某个进程
updates a state of this object all processes of application will be impacted by this update.
更新此对象的状态应用程序的所有进程都将受此更新的影响。
Furthermore, we can have a problem with the concurrent update of the state of a Singleton.
此外,我们可能会遇到单例状态的并发更新问题。

重点词汇

猜你喜欢

转载自blog.csdn.net/Coder_Boy_/article/details/110367890