首页 > 代码库 > 读书笔记_java设计模式深入研究 第六章 桥接模式
读书笔记_java设计模式深入研究 第六章 桥接模式
1,桥接模式:将抽象部分与实现部分分离,使他们可以独立变化。桥接模式可以实现的是不同方式的组合完成不同的功能,方式和功能完全分离,使得相互不影响。
2,UML模型:
3,简单代码实例:
/**
*
* @(#) IPost.java
* @Package pattern.chp06.bridge.simple
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.simple;
/**
* 类描述:邮局接口类
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 23, 2014 3:08:36 PM Jing Created.
*
*/
public interface IPost {
/**
*
* 方法说明:发送功能
*
* Author: Jing Create Date: Dec 23, 2014 3:08:57 PM
*
* @return void
*/
public void post();
}
/**
*
* @(#) MarkPost.java
* @Package pattern.chp06.bridge.simple
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.simple;
/**
* 类描述:挂号信
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 23, 2014 3:19:32 PM Jing Created.
*
*/
public class MarkPost implements IPost{
public void post() {
System.out.println("This is MarkPost");
}
}
/**
*
* @(#) SimplePost.java
* @Package pattern.chp06.bridge.simple
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.simple;
/**
* 类描述:平信邮局
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 23, 2014 3:18:39 PM Jing Created.
*
*/
public class SimplePost implements IPost{
public void post() {
System.out.println("This is SimplePost");
}
}
/**
*
* @(#) AbstractThing.java
* @Package pattern.chp06.bridge.simple
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.simple;
/**
* 类描述:抽象事物
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 23, 2014 3:20:37 PM Jing Created.
*
*/
public abstract class AbstractThing {
private IPost post;
public AbstractThing(IPost post) {
this.post = post;
}
public void post(){
post.post();
}
}
/**
*
* @(#) Lettle.java
* @Package pattern.chp06.bridge.simple
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.simple;
/**
* 类描述:普通信件
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 23, 2014 3:21:36 PM Jing Created.
*
*/
public class Letter extends AbstractThing{
public Letter(IPost post) {
super(post);
}
}
/**
*
* @(#) Parcel.java
* @Package pattern.chp06.bridge.simple
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.simple;
/**
* 类描述:挂号
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 23, 2014 3:22:31 PM Jing Created.
*
*/
public class Parcel extends AbstractThing {
public Parcel(IPost post) {
super(post);
}
}
/**
*
* @(#) MainTest.java
* @Package pattern.chp06.bridge.simple
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.simple;
import org.junit.Test;
/**
* 类描述:
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 23, 2014 3:23:16 PM Jing Created.
*
*/
public class MainTest {
@Test
public void testMain() throws Exception {
IPost post = new SimplePost();
Letter letter = new Letter(post);
letter.post();
}
}
4,深入理解桥接模式
-1,桥接模式强调 包含 替代 继承
这种方式,但对于功能和事物来说两者分离,不会相互影响。
-2,JDK中桥接模式:Collectios.sort方法,借助Arrays.sort方法实现其自身内部的排序。
5,应用
实现读取本地或者远程URL文件,文件类型是文本或图像文件。
分析:
基本到手后实现的思路会如下所示,在TextRead中判断是本地还是URL方法,在其中调用不同的流读写。
但是,当文件类型增多时,会实现多个子类重写,同时会破坏原有方法,需要改进原有方法。我们可以对功能方法进行复杂的拆分,如下UML
代码如下:
/**
*
* @(#) Iread.java
* @Package pattern.chp06.bridge.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.read;
/**
* 类描述:抽象功能类
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 24, 2014 9:41:15 AM Jing Created.
*
*/
public interface IRead<T> {
/**
*
* 方法说明:文件读取
*
* Author: Jing
* Create Date: Dec 24, 2014 10:52:28 AM
*
* @return
* @throws Exception
* @return T
*/
T read() throws Exception;
}
/**
*
* @(#) TextRead.java
* @Package pattern.chp06.bridge.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.read;
/**
* 类描述:文本读取
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 24, 2014 10:04:08 AM Jing Created.
*
*/
public class TextRead implements IRead<String> {
AbstractStream stream;
public TextRead(AbstractStream stream) {
this.stream = stream;
}
/**
* 读取文本文件
*/
public String read() throws Exception {
byte[] buf = stream.readBytes();
String s = new String(buf);
return s;
}
}
/**
*
* @(#) ImgRead.java
* @Package pattern.chp06.bridge.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.read;
/**
* 类描述:读取图像文件
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 24, 2014 10:51:58 AM Jing Created.
*
*/
public class ImgRead implements IRead<byte[]> {
AbstractStream stream;
public ImgRead(AbstractStream stream) {
this.stream = stream;
}
public byte[] read() throws Exception {
return stream.readBytes();
}
}
/**
*
* @(#) AbstractStream.java
* @Package pattern.chp06.bridge.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.read;
import java.io.InputStream;
/**
* 类描述:抽象基类流
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 24, 2014 10:05:54 AM Jing Created.
*
*/
public class AbstractStream {
protected InputStream in;
protected int size;
/**
*
* 方法说明:读取对应输入流
*
* Author: Jing Create Date: Dec 24, 2014 10:11:00 AM
*
* @return
* @throws Exception
* @return byte[]
*/
protected byte[] readBytes() throws Exception {
byte[] buf = new byte[size];
in.read(buf);
return buf;
}
/**
*
* 方法说明:关闭输入流
*
* Author: Jing Create Date: Dec 24, 2014 10:11:24 AM
*
* @throws Exception
* @return void
*/
public void close() throws Exception {
in.close();
}
}
/**
*
* @(#) NativeStream.java
* @Package pattern.chp06.bridge.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.read;
import java.io.File;
import java.io.FileInputStream;
/**
* 类描述:本地文件流读取
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 24, 2014 10:21:53 AM Jing Created.
*
*/
public class NativeStream extends AbstractStream{
public NativeStream(String fileName) throws Exception{
File f = new File(fileName);
size = (int) f.length();
in = new FileInputStream(f);
}
}
/**
*
* @(#) URLStream.java
* @Package pattern.chp06.bridge.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.read;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* 类描述:URl文件读取
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 24, 2014 10:27:09 AM Jing Created.
*
*/
public class URLStream extends AbstractStream {
public URLStream(String fileName) throws Exception{
URL url = new URL(fileName);
in = url.openStream();
HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
size = urlCon.getContentLength();
}
}
/**
*
* @(#) AbstractThing.java
* @Package pattern.chp06.bridge.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.read;
/**
* 类描述:抽象事务类
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 24, 2014 10:54:09 AM Jing Created.
*
*/
public abstract class AbstractThing {
IRead<?> read;
public AbstractThing(IRead<?> read) {
this.read = read;
}
Object read() throws Exception{
return read.read();
}
}
/**
*
* @(#) NativeFile.java
* @Package pattern.chp06.bridge.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.read;
/**
* 类描述:本地文件读取
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 24, 2014 10:55:59 AM Jing Created.
*
*/
public class NativeFile extends AbstractThing{
public NativeFile(IRead<?> read) {
super(read);
}
}
/**
*
* @(#) URLFile.java
* @Package pattern.chp06.bridge.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.read;
/**
* 类描述:URL文件读取
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 24, 2014 10:56:43 AM Jing Created.
*
*/
public class URLFile extends AbstractThing {
public URLFile(IRead<?> read) {
super(read);
}
}
/**
*
* @(#) Main.java
* @Package pattern.chp06.bridge.read
*
* Copyright ? JING Corporation. All rights reserved.
*
*/
package pattern.chp06.bridge.read;
/**
* 类描述:
*
* @author: Jing
* @version $Id: Exp$
*
* History: Dec 24, 2014 11:14:31 AM Jing Created.
*
*/
public class Main {
public static void main(String[] args) throws Exception {
//打开远程文件
AbstractStream in = new URLStream("http://js.t.sinajs.cn/t6/webim_prime/js/webim.js?v=1419389776378");
TextRead textRead = new TextRead(in);
AbstractThing thing = new URLFile(textRead);
String s = (String) thing.read();
in.close();
System.out.println(s);
}
}
读书笔记_java设计模式深入研究 第六章 桥接模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。