0
点赞
收藏
分享

微信扫一扫

设计模式-桥接、装饰者模式备忘 #yyds干货盘点#

理念:用组合去替代继承


桥接模式


定义

将抽象和实现解耦,让它们可以独立变化。

不要把逻辑写死,通过抽象和接口去组合,更加容易拓展和灵活

举个栗子

原本

例如下图,写死逻辑,具体到代码里面就是很多判断和大段代码,很丑

设计模式-桥接、装饰者模式 #yyds干货盘点# _设计模式


应用桥梁模式后

例如下图:抽象出父类甲和乙,通过组合的形式,组合甲乙的继承和实现类,使用的时候,具体继承类通过构造方法的形式注入乙的实现类,通过统一的抽象父类的方法去使用。

设计模式-桥接、装饰者模式 #yyds干货盘点# _桥接模式_02

装饰器模式

定义

通过继承相同抽象父类,装饰器类可以为原始类绑定新的行为,实现对原始功能的增强。


举个栗子

InputStream is = new FileInputStream("C:\\test.txt");
InputStream bIs = new BufferedInputStream(is);
DataInputStream dIs = new DataInputStream(bIs);
//装饰类的构造方法套娃,原始FileInputStream和装饰类都继承了相同的【抽象父类InputStream】,读的时候就可以加上对原始功能的增强
int ch = dIs.readInt();

请看源码

public class FileInputStream extends InputStream
{
......
}
/**
* A FilterInputStream contains some other input stream, which it uses as its basic source of data, possibly transforming the data along the way or providing additional functionality. The class FilterInputStream itself simply overrides all methods of InputStream with versions that pass all requests to the contained input stream. Subclasses of FilterInputStream may further override some of these methods and may also provide additional methods and fields.
* FilterInputStream 包含一些其他输入流,它用作其基本数据源,可能沿途转换数据或提供附加功能。 FilterInputStream 类本身只是简单地覆盖 InputStream 的所有方法,其版本将所有请求传递给包含的输入流。 FilterInputStream 的子类可能会进一步覆盖其中一些方法,并且还可能提供额外的方法和字段。
* 1.0
* 作者:Jonathan Payne
*/
public class FilterInputStream extends InputStream
{
protected volatile InputStream in;
protected FilterInputStream(InputStream in) {
this.in = in;
}

......
}

在调用父类方法 (总是委派给被封装对象) 之前或之后执行自身的行为。

/**
* A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refilled as necessary from the contained input stream, many bytes at a time. The mark operation remembers a point in the input stream and the reset operation causes all the bytes read since the most recent mark operation to be reread before new bytes are taken from the contained input stream.
* BufferedInputStream 向另一个输入流添加了功能,即缓冲输入并支持标记和重置方法的能力。创建 BufferedInputStream 时,会创建一个内部缓冲区数组。当流中的字节被读取或跳过时,内部缓冲区会根据需要从包含的输入流中重新填充,一次很多字节。标记操作会记住输入流中的一个点,并且重置操作会导致在从包含的输入流中获取新字节之前重新读取自最近一次标记操作以来读取的所有字节。
*
* @author Arthur van Hoff
* @since 1.0
*/
public class BufferedInputStream extends FilterInputStream {

public BufferedInputStream(InputStream in) {
this(in, DEFAULT_BUFFER_SIZE);
}

public BufferedInputStream(InputStream in, int size) {
super(in);
if (size <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
buf = new byte[size];
}

public synchronized int read() throws IOException {
...
//调用传入的InputStream继承类的读方法,实现层层套娃增强功能
int n = in.read(buffer, pos, buffer.length - pos);
...
}

......
}
/**
* A data input stream lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.
* A DataInputStream is not safe for use by multiple concurrent threads. If a DataInputStream is to be used by more than one thread then access to the data input stream should be controlled by appropriate synchronization.
*
* 数据输入流允许应用程序以与机器无关的方式从底层输入流中读取原始 Java 数据类型。应用程序使用数据输出流写入数据,这些数据稍后可以由数据输入流读取。
* 多个并发线程使用 DataInputStream 是不安全的。如果一个 DataInputStream 被多个线程使用,那么对数据输入流的访问应该由适当的同步控制。
*
* @author Arthur van Hoff
* @see java.io.DataOutputStream
* @since 1.0
*/
public class DataInputStream extends FilterInputStream implements DataInput {

public DataInputStream(InputStream in) {
super(in);
}

//类似BufferedInputStream,在调用父类方法 (总是委派给被封装对象) 之前或之后执行自身的行为。
......

}
举报

相关推荐

0 条评论