java 设计模式 示例_Java设计模式–示例教程

java 设计模式 示例

Design Patterns are very popular among software developers. A design pattern is a well-described solution to a common software problem. I have written extensively on java design patterns. You can download PDF eBook (130+ pages) by subscribing to our newsletter.

设计模式在软件开发人员中非常流行。 设计模式是对常见软件问题的精心描述的解决方案。 我已经广泛编写了有关Java设计模式的文章 。 您可以订阅我们的新闻通讯,下载PDF电子书(超过130页)。

Java设计模式 (Java Design Patterns)

Some of the benefits of using design patterns are:

使用设计模式的一些好处是:

  1. Design Patterns are already defined and provides industry standard approach to solve a recurring problem, so it saves time if we sensibly use the design pattern. There are many java design patterns that we can use in our java based projects.

    设计模式已经定义,并提供了解决重复出现的问题的行业标准方法 ,因此,如果我们明智地使用设计模式,可以节省时间。 我们可以在基于Java的项目中使用许多Java设计模式。
  2. Using design patterns promotes reusability that leads to more robust and highly maintainable code. It helps in reducing total cost of ownership (TCO) of the software product.

    使用设计模式可提高可重用性 ,从而导致更健壮和高度可维护的代码。 它有助于降低软件产品的总拥有成本(TCO)。
  3. Since design patterns are already defined, it makes our code easy to understand and debug. It leads to faster development and new members of team understand it easily.

    由于已经定义了设计模式,因此使我们的代码易于理解和调试。 它导致更快的开发,并且团队的新成员很容易理解它。

java design patterns

Java Design Patterns are divided into three categories – creational, structural, and behavioral design patterns. This post serves as an index for all the java design patterns articles I have written so far.


Java设计模式分为三类- 创新结构行为设计模式。 这篇文章是我到目前为止编写的所有Java设计模式文章的索引。

设计模式视频教程 (Design Patterns Video Tutorials)

Recently I started video tutorials on Design Patterns and they are uploaded on YouTube. Please subscribe to my YouTube channel as I am planning to upload a lot more videos on Core Java, Spring Framework etc.

最近,我开始了有关“设计模式”的视频教程,并将它们上传到YouTube。 请订阅我的YouTube频道,因为我打算在Core Java,Spring Framework等上上传更多视频。

演示地址

创新设计模式 (Creational Design Patterns)

Creational design patterns provide solution to instantiate a object in the best possible way for specific situations.

创新的设计模式为特定情况下以最佳方式实例化对象提供了解决方案。

  1. 单例模式 (Singleton Pattern)

    Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the Java virtual machine. It seems to be a very simple design pattern but when it comes to implementation, it comes with a lot of implementation concerns. The implementation of the Singleton pattern has always been a controversial topic among developers. Check out Singleton Design Pattern to learn about different ways to implement Singleton pattern and pros and cons of each of the method. This is one of the most discussed java design patterns.

    单例模式限制了类的实例化,并确保Java虚拟机中仅存在该类的一个实例。 这似乎是一个非常简单的设计模式,但是当涉及到实现时,它会带来很多实现方面的问题。 Singleton模式的实现一直是开发人员中有争议的话题。 查看Singleton设计模式,以了解实现Singleton模式的不同方法以及每种方法的利弊。 这是讨论最多的Java设计模式之一。

  2. 工厂模式 (Factory Pattern)

    Factory design pattern is used when we have a super class with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern take out the responsibility of instantiation of a class from client program to the factory class. We can apply Singleton pattern on Factory class or make the factory method static. Check out Factory Design Pattern for example program and factory pattern benefits. This is one of the most widely used java design pattern.

    当我们具有包含多个子类的超类并且基于输入时,我们需要返回其中一个子类,将使用工厂设计模式。 这种模式承担了从客户端程序到工厂类的类实例化的责任。 我们可以在Factory类上应用Singleton模式,或将factory方法设为静态。 请查看Factory Design Pattern,以了解示例程序和Factory Pattern的好处。 这是使用最广泛的Java设计模式之一。

  3. 抽象工厂模式 (Abstract Factory Pattern)

    Abstract Factory pattern is similar to Factory pattern and it’s a factory of factories. If you are familiar with the factory design pattern in java, you will notice that we have a single Factory class that returns the different sub-classes based on the input provided and factory class uses if-else or switch statement to achieve this.

    In Abstract Factory pattern, we get rid of if-else block and have a factory class for each sub-class and then an Abstract Factory class that will return the sub-class based on the input factory class. Check out Abstract Factory Pattern to know how to implement this pattern with example program.

    摘要工厂模式类似于工厂模式,它是工厂的工厂。 如果您熟悉Java中的工厂设计模式,则会注意到我们有一个Factory类,该类根据提供的输入返回不同的子类,并且工厂类使用if-else或switch语句来实现此目的。

    在抽象工厂模式中,我们摆脱了if-else块,为每个子类都有一个工厂类,然后有一个抽象工厂类,它将根据输入的工厂类返回该子类。 查看Abstract Factory Pattern,以了解如何使用示例程序实现此模式。

  4. 建造者模式 (Builder Pattern)

    This pattern was introduced to solve some of the problems with Factory and Abstract Factory design patterns when the Object contains a lot of attributes. Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object. Check out Builder Pattern for example program and classes used in JDK.

    当对象包含很多属性时,引入此模式是为了解决工厂和抽象工厂设计模式的某些问题。 Builder模式通过提供一种逐步构建对象并提供一种将实际返回最终Object的方法的方式,解决了具有大量可选参数和状态不一致的问题。 查阅Builder模式,了解JDK中使用的示例程序和类。

  5. 原型模式 (Prototype Pattern)

    Prototype pattern is used when the Object creation is a costly affair and requires a lot of time and resources and you have a similar object already existing. So this pattern provides a mechanism to copy the original object to a new object and then modify it according to our needs. This pattern uses java cloning to copy the object.

    Prototype design pattern mandates that the Object which you are copying should provide the copying feature. It should not be done by any other class. However whether to use the shallow or deep copy of the Object properties depends on the requirements and it’s a design decision. Check out Prototype Pattern for sample program.

    当创建对象的事务非常昂贵并且需要大量时间和资源并且您已经有一个相似的对象时,可以使用原型模式。 因此,此模式提供了一种机制,可将原始对象复制到新对象,然后根据需要进行修改。 此模式使用Java克隆来复制对象。

    原型设计模式要求您要复制的对象应提供复制功能。 不应由其他任何班级完成。 但是,使用对象属性的浅表副本还是深表副本取决于要求,这是设计决定。 检出样例程序的原型模式

结构设计模式 (Structural Design Patterns)

Structural patterns provide different ways to create a class structure, for example using inheritance and composition to create a large object from small objects.

结构模式提供了创建类结构的不同方法,例如使用继承和组合从小对象创建大对象。

  1. 适配器图案 (Adapter Pattern)

    The adapter design pattern is one of the structural design patterns and it’s used so that two unrelated interfaces can work together. The object that joins these unrelated interfaces is called an Adapter. As a real-life example, we can think of a mobile charger as an adapter because the mobile battery needs 3 volts to charge but the normal socket produces either 120V (US) or 240V (India). So the mobile charger works as an adapter between the mobile charging socket and the wall socket. Check out Adapter Pattern for example program and it’s usage in Java.

    适配器设计模式是结构设计模式之一,并且使用它是为了使两个不相关的接口可以一起工作。 连接这些不相关接口的对象称为适配器。 举一个实际的例子,我们可以将移动充电器看作是适配器,因为移动电池需要3伏才能充电,但普通插座产生的电压为120V(美国)或240V(印度)。 因此,移动充电器可充当移动充电插座和壁式插座之间的适配器。 查看示例程序的适配器模式及其在Java中的用法。

  2. 复合图案 (Composite Pattern)

    Composite pattern is one of the Structural design patterns and is used when we have to represent a part-whole hierarchy. When we need to create a structure in a way that the objects in the structure have to be treated the same way, we can apply the composite design pattern.

    Let’s understand it with a real-life example – A diagram is a structure that consists of Objects such as Circle, Lines, Triangle etc and when we fill the drawing with color (say Red), the same color also gets applied to the Objects in the drawing. Here drawing is made up of different parts and they all have the same operations. Check out Composite Pattern article for different component of composite pattern and example program.

    复合模式是结构设计模式之一,在我们必须表示部分整体层次结构时使用。 当我们需要以必须以相同方式对待结构中的对象的方式创建结构时,可以应用复合设计模式。

    让我们用一个真实的例子来理解它-图表是由诸如圆形,直线,三角形等对象组成的结构,当我们用颜色(例如红色)填充图形时,相同的颜色也将应用于绘图。 这里的绘图由不同的部分组成,它们都具有相同的操作。 查阅Composite Pattern文章,了解复合模式的不同组成部分和示例程序。

  3. 代理模式 (Proxy Pattern)

    Proxy pattern intent is to “Provide a surrogate or placeholder for another object to control access to it”. The definition itself is very clear and proxy pattern is used when we want to provide controlled access of a functionality.

    Let’s say we have a class that can run some command on the system. Now if we are using it, it’s fine but if we want to give this program to a client application, it can have severe issues because client program can issue a command to delete some system files or change some settings that you don’t want. Check out Proxy Pattern post for the example program with implementation details.

    代理模式的意图是“为另一个对象提供代理或占位符,以控制对其的访问”。 定义本身非常清晰,当我们要提供功能的受控访问时,将使用代理模式。

    假设我们有一个可以在系统上运行某些命令的类。 现在,如果我们正在使用它,那就很好了,但是如果我们想将此程序提供给客户端应用程序,它可能会遇到严重的问题,因为客户端程序可以发出命令来删除某些系统文件或更改某些不需要的设置。 查阅Proxy Pattern文章,获取带有实现细节的示例程序。

  4. 跳线模式 (Flyweight Pattern)

    Flyweight design pattern is used when we need to create a lot of Objects of a class. Since every object consumes memory space that can be crucial for low memory devices, such as mobile devices or embedded systems, flyweight design pattern can be applied to reduce the load on memory by sharing objects. String Pool implementation in java is one of the best example of Flyweight pattern implementation. Check out Flyweight Pattern article for sample program and implementation process.

    当我们需要创建一个类的许多对象时,可以使用Flyweight设计模式。 由于每个对象都会占用对于低内存设备(例如移动设备或嵌入式系统)至关重要的内存空间,因此可以应用轻量级设计模式来通过共享对象来减少内存负载。 Java中的字符串池实现是Flyweight模式实现的最佳示例之一。 查看Flyweight Pattern文章,了解示例程序和实现过程。

  5. 外墙图案 (Facade Pattern)

    Facade Pattern is used to help client applications to easily interact with the system. Suppose we have an application with a set of interfaces to use MySql/Oracle database and to generate different types of reports, such as HTML report, PDF report etc. So we will have a different set of interfaces to work with different types of database. Now a client application can use these interfaces to get the required database connection and generate reports. But when the complexity increases or the interface behavior names are confusing, the client application will find it difficult to manage it. So we can apply Facade pattern here and provide a wrapper interface on top of the existing interface to help client application. Check out Facade Pattern post for implementation details and sample program.

    外观模式用于帮助客户端应用程序轻松与系统进行交互。 假设我们有一个带有一组接口的应用程序,以使用MySql / Oracle数据库并生成不同类型的报告,例如HTML报告,PDF报告等。因此,我们将拥有一组不同的接口来与不同类型的数据库一起使用。 现在,客户端应用程序可以使用这些接口来获取所需的数据库连接并生成报告。 但是,当复杂性增加或接口行为名称令人困惑时,客户端应用程序将发现很难对其进行管理。 因此,我们可以在此处应用Facade模式,并在现有接口之上提供包装器接口,以帮助客户端应用程序。 查看Facade Pattern帖子以获取实现细节和示例程序。

  6. 桥型 (Bridge Pattern)

    When we have interface hierarchies in both interfaces as well as implementations, then bridge design pattern is used to decouple the interfaces from implementation and hiding the implementation details from the client programs. Like Adapter pattern, it’s one of the Structural design pattern.

    The implementation of bridge design pattern follows the notion to prefer Composition over inheritance. Check out Bridge Pattern post for implementation details and sample program.

    当我们在接口和实现中都具有接口层次结构时,则使用网桥设计模式将接口与实现分离,并在客户端程序中隐藏实现细节。 与适配器模式一样,它也是“结构”设计模式之一。

    桥梁设计模式的实现遵循的概念是:优先于继承而不是继承。 请参阅Bridge Pattern帖子以获取实施细节和示例程序。

  7. 装饰图案 (Decorator Pattern)

    The decorator design pattern is used to modify the functionality of an object at runtime. At the same time, other instances of the same class will not be affected by this, so individual object gets the modified behavior. The decorator design pattern is one of the structural design pattern (such as Adapter Pattern, Bridge Pattern, Composite Pattern) and uses abstract classes or interface with the composition to implement.

    We use inheritance or composition to extend the behavior of an object but this is done at compile time and it’s applicable to all the instances of the class. We can’t add any new functionality to remove any existing behavior at runtime – this is when Decorator pattern comes into the picture. Check out Decorator Pattern post for sample program and implementation details.

    装饰器设计模式用于在运行时修改对象的功能。 同时,同一类的其他实例将不受此影响,因此单个对象将获得修改后的行为。 装饰器设计模式是结构设计模式之一(例如适配器模式,桥接器模式,复合模式),并使用抽象类或与该组合的接口来实现。

    我们使用继承或组合来扩展对象的行为,但这是在编译时完成的,并且适用于该类的所有实例。 我们无法添加任何新功能来删除运行时的任何现有行为-这是Decorator模式出现的时候。 查看Decorator Pattern帖子,获取示例程序和实现细节。

行为设计模式 (Behavioral Design Patterns)

Behavioral patterns provide solution for the better interaction between objects and how to provide lose coupling and flexibility to extend easily.

行为模式为对象之间更好的交互以及如何提供丢失的耦合和灵活扩展提供了解决方案。

  1. 模板方法模式 (Template Method Pattern)

    Template Method is a behavioral design pattern and it’s used to create a method stub and deferring some of the steps of implementation to the subclasses. Template method defines the steps to execute an algorithm and it can provide a default implementation that might be common for all or some of the subclasses.

    Suppose we want to provide an algorithm to build a house. The steps need to be performed to build a house are – building a foundation, building pillars, building walls, and windows. The important point is that we can’t change the order of execution because we can’t build windows before building the foundation. So, in this case, we can create a template method that will use different methods to build the house. Check out Template Method Pattern post for implementation details with example program.

    模板方法是一种行为设计模式,用于创建方法存根并将某些实现步骤推迟到子类。 模板方法定义了执行算法的步骤,它可以提供默认实现,该实现对于所有或某些子类可能是通用的。

    假设我们要提供一种建造房屋的算法。 建造房屋所需执行的步骤是–建造地基,建造Struts,建造墙壁和窗户。 重要的是,我们无法更改执行顺序,因为我们无法在构建基础之前构建窗口。 因此,在这种情况下,我们可以创建一个模板方法,该模板方法将使用不同的方法来建造房屋。 请查看模板方法模式文章,以获取示例程序的实现详细信息。

  2. 中介者模式 (Mediator Pattern)

    The mediator design pattern is used to provide a centralized communication medium between different objects in a system. The mediator design pattern is very helpful in an enterprise application where multiple objects are interacting with each other. If the objects interact with each other directly, the system components are tightly coupled with each other that makes maintainability cost higher and not flexible to extend easily. Mediator pattern focuses on to provide a mediator between objects for communication and help in implementing lose-coupling between objects.

    Air traffic controller is a great example of a mediator pattern where the airport control room works as a mediator for communication between different flights. The mediator works as a router between objects and it can have it’s own logic to provide a way of communication. Check out Mediator Pattern post for implementation details with example program.

    中介器设计模式用于在系统中不同对象之间提供集中式通信介质。 中介器设计模式在企业应用程序中非常有用,在该应用程序中多个对象相互交互。 如果对象彼此直接交互,则系统组件彼此紧密耦合,这使可维护性成本更高,并且不易于扩展。 调解器模式致力于在对象之间提供一个调解器以进行通信,并帮助实现对象之间的损耗耦合。

    空中交通管制员是调解员模式的一个很好的例子,其中机场控制室充当不同航班之间通信的调解员。 介体充当对象之间的路由器,并且可以具有自己的逻辑来提供通信方式。 请查看Mediator Pattern帖子,以获取示例程序的实现细节。

  3. 责任链模式 (Chain of Responsibility Pattern)

    Chain of responsibility pattern is used to achieve loose coupling in software design where a request from the client is passed to a chain of objects to process them. Then the object in the chain will decide themselves who will be processing the request and whether the request is required to be sent to the next object in the chain or not.

    We know that we can have multiple catch blocks in a try-catch block code. Here every catch block is kind of a processor to process that particular exception. So when an exception occurs in the try block, it’s sent to the first catch block to process. If the catch block is not able to process it, it forwards the request to next object in chain i.e next catch block. If even the last catch block is not able to process it, the exception is thrown outside of the chain to the calling program.

    ATM dispense machine logic can be implemented using Chain of Responsibility Pattern, check out the linked post.

    责任链模式用于实现软件设计中的松散耦合,其中将来自客户端的请求传递到对象链以对其进行处理。 然后,链中的对象将自行决定将由谁处理请求,以及是否需要将请求发送到链中的下一个对象。

    我们知道在try-catch块代码中可以有多个catch块。 在这里,每个catch块都是一种处理器,用于处理该特定异常。 因此,当try块中发生异常时,会将其发送到第一个catch块进行处理。 如果catch块无法处理它,则将请求转发到链中的下一个对象,即next catch块。 如果即使最后一个catch块也无法处理它,则将异常抛出到调用程序的链外。

    可以使用“责任链模式”实施ATM分配机逻辑,签出链接的帖子。

  4. 观察者模式 (Observer Pattern)

    Observer design pattern is useful when you are interested in the state of an object and want to get notified whenever there is any change. In observer pattern, the object that watch on the state of another object are called Observer and the object that is being watched is called Subject.

    Java provides an inbuilt platform for implementing Observer pattern through java.util.Observable class and java.util.Observer interface. However, it’s not widely used because the implementation is really simple and most of the times we don’t want to end up extending a class just for implementing Observer pattern as java doesn’t provide multiple inheritances in classes.

    Java Message Service (JMS) uses Observer pattern along with Mediator pattern to allow applications to subscribe and publish data to other applications. Check out Observer Pattern post for implementation details and example program.

    当您对对象的状态感兴趣并希望在发生任何更改时得到通知时,观察者设计模式很有用。 在观察者模式中,监视另一个对象状态的对象称为Observer ,而正在监视的对象称为Subject

    Java提供了一个内置平台,用于通过java.util.Observable类和java.util.Observer接口实现Observer模式。 但是,它的使用并不广泛,因为实现起来非常简单,并且大多数时候我们不想只为了实现Observer模式而扩展一个类,因为Java在类中没有提供多个继承。

    Java消息服务(JMS)使用Observer模式和Mediator模式来允许应用程序向其他应用程序订阅和发布数据。 请查看《 观察者模式》一文,以了解实施细节和示例程序。

  5. 策略模式 (Strategy Pattern)

    Strategy pattern is used when we have multiple algorithm for a specific task and client decides the actual implementation to be used at runtime.

    Strategy pattern is also known as Policy Pattern. We define multiple algorithms and let client application pass the algorithm to be used as a parameter. One of the best examples of this pattern is the Collections.sort() method that takes the Comparator parameter. Based on the different implementations of Comparator interfaces, the Objects are getting sorted in different ways.

    Check out Strategy Pattern post for implementation details and example program.

    当我们对一个特定的任务有多种算法并且客户决定在运行时使用的实际实现时,将使用策略模式。

    策略模式也称为策略模式。 我们定义了多种算法,并让客户端应用程序将算法用作参数。 此模式的最佳示例之一是采用Comparator参数的Collections.sort()方法。 基于Comparator接口的不同实现,将以不同的方式对对象进行排序。

    查阅Strategy Pattern帖子,了解实施细节和示例程序。

  6. 命令模式 (Command Pattern)

    Command Pattern is used to implement lose coupling in a request-response model. In command pattern, the request is send to the invoker and invoker pass it to the encapsulated command object. Command object passes the request to the appropriate method of Receiver to perform the specific action.

    Let’s say we want to provide a File System utility with methods to open, write and close the file and it should support multiple operating systems such as Windows and Unix.

    To implement our File System utility, first of all, we need to create the receiver classes that will actually do all the work. Since we code in terms of Java interfaces, we can have FileSystemReceiver interface and it’s implementation classes for different operating system flavors such as Windows, Unix, Solaris etc. Check out Command Pattern post for the implementation details with example program.

    命令模式用于在请求-响应模型中实现丢失耦合。 在命令模式中,请求被发送到调用者, 调用者将其传递给封装的命令对象。 命令对象将请求传递给Receiver的适当方法以执行特定操作。

    假设我们要为File System实用程序提供打开,写入和关闭文件的方法,并且它应支持Windows和Unix等多种操作系统。

    为了实现我们的文件系统实用程序,首先,我们需要创建实际上将完成所有工作的接收器类。 由于我们使用Java接口进行编码,因此我们可以拥有FileSystemReceiver接口,并且它是适用于不同操作系统版本(例如Windows,Unix,Solaris等)的实现类。有关示例程序的实现详细信息,请参阅“ 命令模式”文章。

  7. 状态模式 (State Pattern)

    State design pattern is used when an Object change it’s behavior based on it’s internal state.

    If we have to change the behavior of an object based on its state, we can have a state variable in the Object and use if-else condition block to perform different actions based on the state. State pattern is used to provide a systematic and loosely coupled way to achieve this through Context and State implementations.

    Check out State Pattern post for implementation details with example program.

    当对象根据其内部状态更改其行为时,将使用状态设计模式。

    如果必须根据对象的状态更改其行为,则可以在对象中使用状态变量,并使用if-else条件块根据该状态执行不同的操作。 状态模式用于通过上下文和状态实现提供一种系统的,松散耦合的方式来实现此目的。

    请查看State Pattern帖子以获取示例程序的实现细节。

  8. 访客模式 (Visitor Pattern)

    Visitor pattern is used when we have to perform an operation on a group of similar kind of Objects. With the help of visitor pattern, we can move the operational logic from the objects to another class.

    For example, think of a Shopping cart where we can add a different type of items (Elements), when we click on the checkout button, it calculates the total amount to be paid. Now we can have the calculation logic in item classes or we can move out this logic to another class using visitor pattern. Let’s implement this in our example of a visitor pattern. Check out Visitor Pattern post for implementation details.

    当我们必须对一组相似类型的对象执行操作时,将使用访问者模式。 借助访问者模式,我们可以将操作逻辑从对象移动到另一个类。

    例如,假设有一个购物车,我们可以在其中添加其他类型的项目(元素),当我们单击结帐按钮时,它将计算要支付的总金额。 现在我们可以将计算逻辑包含在项目类中,或者可以使用访问者模式将此逻辑移到另一个类中。 让我们在访问者模式示例中实现这一点。 请查看访问者模式帖子以获取实施细节。

  9. 口译模式 (Interpreter Pattern)

    is used to defines a grammatical representation for a language and provides an interpreter to deal with this grammar.

    The best example of this pattern is java compiler that interprets the java source code into byte code that is understandable by JVM. Google Translator is also an example of an interpreter pattern where the input can be in any language and we can get the output interpreted in another language.

    Check out Interpreter Pattern post for example program.

    用于定义语言的语法表示形式,并提供解释器来处理这种语法。

    这种模式的最佳示例是Java编译器,它将Java源代码解释为JVM可以理解的字节代码。 Google Translator也是解释器模式的一个示例,其中输入可以使用任何语言,而我们可以使用另一种语言来解释输出。

    查看示例程序的解释器模式文章。

  10. 迭代器模式 (Iterator Pattern)

    Iterator pattern in one of the behavioral pattern and it’s used to provide a standard way to traverse through a group of Objects. Iterator pattern is widely used in Java Collection Framework where Iterator interface provides methods for traversing through a collection.

    Iterator pattern is not only about traversing through a collection, but we can also provide different kind of iterators based on our requirements. Iterator pattern hides the actual implementation of traversal through the collection and client programs just use iterator methods. Check out Iterator Pattern post for example program and implementation details.

    迭代器模式是一种行为模式,它用于提供遍历一组对象的标准方式。 Iterator模式在Java Collection Framework中得到了广泛使用,其中Iterator接口提供了遍历集合的方法。

    迭代器模式不仅涉及遍历集合,而且我们还可以根据需求提供不同种类的迭代器。 迭代器模式通过集合隐藏了遍历的实际实现,而客户端程序仅使用迭代器方法。 查看Iterator Pattern帖子,了解示例程序和实现细节。

  11. 纪念图案 (Memento Pattern)

    The memento design pattern is used when we want to save the state of an object so that we can restore later on. Memento pattern is used to implement this in such a way that the saved state data of the object is not accessible outside of the object, this protects the integrity of saved state data.

    Memento pattern is implemented with two objects – Originator and Caretaker. The originator is the object whose state needs to be saved and restored and it uses an inner class to save the state of Object. The inner class is called Memento and it’s private so that it can’t be accessed from other objects.

    Check out Memento Pattern for sample program and implementation details.

    当我们要保存对象的状态以便以后可以恢复时,将使用memento设计模式。 使用Memento模式以这种方式实现该目的,即无法在对象外部访问对象的已保存状态数据,这可以保护已保存状态数据的完整性。

    记忆模式由两个对象实现-发起者和看守者。 始发者是需要保存和恢复其状态的对象,它使用内部类保存Object的状态。 内部类称为Memento,它是私有的,因此不能从其他对象访问它。

    请查看Memento模式以获取示例程序和实现细节。

杂项设计模式 (Miscellaneous Design Patterns)

There are a lot of design patterns that doesn’t come under GoF design patterns. Let’s look at some of these popular design patterns.

GoF设计模式下没有很多设计模式。 让我们看一下其中一些流行的设计模式。

  1. DAO设计模式 (DAO Design Pattern)

    DAO design pattern is used to decouple the data persistence logic to a separate layer. DAO is a very popular pattern when we design systems to work with databases. The idea is to keep the service layer separate from the Data Access layer. This way we implement Separation of Logic in our application.

    Checkout DAO Pattern for complete details and example program.

    DAO设计模式用于将数据持久性逻辑解耦到单独的层。 当我们设计用于数据库的系统时,DAO是一种非常流行的模式。 想法是使服务层与数据访问层分开。 这样,我们在应用程序中实现了逻辑分离。

    检出DAO模式以获取完整的详细信息和示例程序。

  2. 依赖注入模式 (Dependency Injection Pattern)

    Dependency Injection allows us to remove the hard-coded dependencies and make our application loosely coupled, extendable, and maintainable. We can implement dependency injection in java to move the dependency resolution from compile-time to runtime. Spring framework is built on the principle of dependency injection.

    Read more about Dependency Injection Pattern to understand how to implement it in our Java application.

    依赖注入使我们可以删除硬编码的依赖,并使我们的应用程序松散耦合,可扩展和可维护。 我们可以在Java中实现依赖注入,以将依赖解析从编译时移至运行时。 Spring框架基于依赖注入的原理构建。

    阅读有关依赖注入模式的更多信息,以了解如何在我们的Java应用程序中实现它。

  3. MVC模式 (MVC Pattern)

    MVC Pattern is one of the oldest architectural pattern for creating web applications. MVC stands for Model-View-Controller.

    Checkout MVC Pattern for more details and complete example code.

    MVC模式是用于创建Web应用程序的最古老的体系结构模式之一。 MVC代表模型-视图-控制器。

    查看MVC模式以获取更多详细信息和完整的示例代码。

That’s all for different design patterns in Java, this post intent is to provide an index to browse all of them easily.

这就是Java中不同设计模式的全部内容,本文旨在提供一个索引,以轻松浏览所有这些内容。

GitHub Repository. GitHub存储库中签出Java Design Patterns示例代码。

翻译自: https://www.journaldev.com/1827/java-design-patterns-example-tutorial

java 设计模式 示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值