Structural patterns_Java design patterns
来源:互联网 发布:如何用sql添加字段 编辑:程序博客网 时间:2024/05/29 23:24
Structural patterns prescribe the organization of classes and objects. These patterns are concerned with how classes inherit from each other or how they are composed from other classes.
Common structural patterns include Adapter, Proxy, and Decorator patterns. These patterns are similar in that they introduce a level of indirection between a client class and a class it wants to use. Their intents are different, however. Adapter uses indirection to modify the interface of a class to make it easier for a client class to use it. Decorator uses indirection to add behavior to a class, without unduly affecting the client class. Proxy uses indirection to transparently provide a stand-in for another class.
The Adapter pattern
The Adapter pattern is typically used to allow the reuse of a class that is similar, but not the same, as the class the client class would like to see. Typically the original class is capable of supporting the behavior the client class needs, but does not have the interface the client class expects, and it is not possible or practical to alter the original class. Perhaps the source code is not available, or it is used elsewhere and changing the interface is inappropriate.
Here is an example that wraps OldClass
so a client class can call it using a method, NewMethod()
defined in NewInterface
:
public class OldClassAdapter implements NewInterface { private OldClass ref; public OldClassAdapter(OldClass oc) { ref = oc; } public void NewMethod() { ref.OldMethod(); } }
The Decorator and Proxy pattern
A Proxy is a direct stand-in for another class, and it typically has the same interface as that class because it implements a common interface or an abstract class. The client object is not aware that it is using a proxy. A Proxy is used when access to the class the client would like to use must be mediated in a way that is apparent to the client -- because it requires restricted access or is a remote process, for example.
Decorator, like Proxy, is also a stand-in for another class, and it also has the same interface as that class, usually because it is a subclass. The intent is different, however. The purpose of the Decorator pattern is to extend the functionality of the original class in a way that is transparent to the client class.
Examples of the Decorator pattern in the Java API are found in the classes for processing input and output streams.BufferedReader()
, for example, makes reading text from a file convenient and efficient:
BufferedReader in = new BufferedReader(new FileReader("file.txt"));
The Composite pattern
The Composite pattern prescribes recursive composition for complex objects. The intent is to allow all component objects to be treated in a consistent manner. All objects, simple and complex, that participate in this pattern derive from a common abstract component class that defines common behavior.
Forcing relationships into a part-whole hierarchy in this way minimizes the types of objects that our system (or client subsystem) needs to manage. A client of a paint program, for example, could ask a line to draw itself in the same way it would ask any other object, including a composite object.
- Structural patterns_Java design patterns
- Creational patterns_Java design patterns
- Behavioral patterns_Java design Patterns
- Structural Design Patterns
- Design Pattern----Structural Patterns
- Design Pattern - Structural Patterns - Adapter Pattern
- [Special] Design Pattern - Structural Patterns - Proxy Pattern
- Design Pattern - Structural Patterns - Decorator Pattern
- Design Pattern - Structural Patterns - Composite Pattern
- Design Pattern - Structural Patterns - Facade Pattern
- Design Pattern - Structural Patterns - Flyweight Pattern
- Design Pattern - Structural Patterns - Bridge Pattern
- Concurrency patterns_Java design pattern
- Structural Patterns -- Composite
- SUMMARY OF STRUCTURAL PATTERNS
- Object-Relational Structural Patterns
- PEAA-Object-Relational Structural Patterns
- Design Patterns
- XZ Utils解压缩软件
- Linux Crontab 命令
- SQL语句中行转列案例
- java SE平台搭建
- 《写给24岁的自己》读后感慨
- Structural patterns_Java design patterns
- 查询条件有null的情况
- android之 处理软键盘隐藏
- Matrix--集合--点二维排序
- java学习笔记一
- web.xml 详细介绍
- 第三方支付
- Android平台打电话、发短信、发Email
- How to switch from POST to GET in PHP CURL