标签:
1,桥接模式:将抽象部分与实现部分分离,使他们可以独立变化。桥接模式可以实现的是不同方式的组合完成不同的功能,方式和功能完全分离,使得相互不影响。
/**
*
* @(#) 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();
}
}
/**
*
* @(#) 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);
}
}
标签:
原文地址:http://blog.csdn.net/mergades/article/details/42168199