Java review-design pattern
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java review-design pattern相关的知识,希望对你有一定的参考价值。
Design Patterns (Factory, Abstract Factory, singleton, DAO, Proxy):
1. Factory: In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.
2. Abstract Factory: Abstract Factory patterns work around a super-factory which creates other factories. This factory is also called as factory of factories. In Abstract Factory pattern an interface is responsible for creating a factory of related objects without explicitly specifying their classes. Each generated factory can give the objects as per the Factory pattern.
3.Singleton: This pattern involves a single class which is responsible to create an object while making sure that only single object gets created. This class provides a way to access its only object which can be accessed directly without need to instantiate the object of the class.
4.Builder: A Builder class builds the final object step by step. This builder is independent of other objects.
5. Prototype: This pattern involves implementing a prototype interface which tells to create a clone of the current object. This pattern is used when creation of object directly is costly. For example, an object is to be created after a costly database operation. We can cache the object, returns its clone on next request and update the database as and when needed thus reducing database calls.
6.Adapter: Adapter pattern works as a bridge between two incompatible interfaces. A single class which is responsible to join functionalities of independent or incompatible interfaces.
7. Bridge: Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently.
8. Filter pattern or Criteria pattern: is a design pattern that enables developers to filter a set of objects using different criteria and chaining them in a decoupled way through logical operations.
9.Decorator pattern allows a user to add new functionality to an existing object without altering its structure.
10.Facade pattern hides the complexities of the system and provides an interface to the client using which the client can access the system. This type of design pattern comes under structural pattern as this pattern adds an interface to existing system to hide its complexities.
11. In proxy pattern, a class represents functionality of another class. This type of design pattern comes under structural pattern. In proxy pattern, we create object having original object to interface its functionality to outer world.
12.chain of responsibility pattern creates a chain of receiver objects for a request. This pattern decouples sender and receiver of a request based on type of request.
13.Data Access Object Pattern or DAO pattern is used to separate low level data accessing API or operations from high level business services.
个人分析:
1.Factory,AbstractFactory 还有 Singleton 都是生成对象的设计模式
工厂是放入参数,通过接口产生对象,封装内部的生成的过程
抽象工厂是生成工厂的工厂,是工厂类的父类,可以抽象为接口,根据不同的类来实现不同的工厂。
单例模式是通过单例方法直接生成一个唯一的对象,并且不需要初始化对象就可以使用
2. builder和prototype
builder的实际意义就是通过一步一步生成的模式最终建立一个对象,例如stringbuilder
prototype是克隆一个原来有的对象,将原先对象保存起来,可以在克隆出来的对象进行操作再返回,也可以直接返回克隆的对象
3. adapter, bridge,filter
adapter:两个不相同接口相互适应
bridge: decoupling
filter:设置标准,过滤掉一些对象,
4.Decorator,Facade
Decorator:在不改变原来对象的前提下,建立一个新的对象,
Facde: 通过接口隐藏复杂的程序逻辑,值给用户暴露一个封装好的内容
5. In proxy pattern,chain of responsibility pattern
proxy:代理模式,创建一个对象来集成原来的对象,并且通过原来的对象与外部世界进行交互
chain of responsibility: 由一系列的发送者和接收者连接而成,并且在处理请求的时候并不知道发送者是谁。
以上是关于Java review-design pattern的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final) A. Single Wildcard Patter