首页 > 代码库 > 读书笔记_java设计模式深入研究 第六章 桥接模式

读书笔记_java设计模式深入研究 第六章 桥接模式

1,桥接模式:将抽象部分与实现部分分离,使他们可以独立变化。桥接模式可以实现的是不同方式的组合完成不同的功能,方式和功能完全分离,使得相互不影响。
2,UML模型:
技术分享
技术分享
3,简单代码实例:
  1. /**
  2. *
  3. * @(#) IPost.java
  4. * @Package pattern.chp06.bridge.simple
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.simple;
  10. /**
  11. * 类描述:邮局接口类
  12. *
  13. * @author: Jing
  14. * @version $Id: Exp$
  15. *
  16. * History: Dec 23, 2014 3:08:36 PM Jing Created.
  17. *
  18. */
  19. public interface IPost {
  20. /**
  21. *
  22. * 方法说明:发送功能
  23. *
  24. * Author: Jing Create Date: Dec 23, 2014 3:08:57 PM
  25. *
  26. * @return void
  27. */
  28. public void post();
  29. }
  1. /**
  2. *
  3. * @(#) MarkPost.java
  4. * @Package pattern.chp06.bridge.simple
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.simple;
  10. /**
  11. * 类描述:挂号信
  12. *
  13. * @author: Jing
  14. * @version $Id: Exp$
  15. *
  16. * History: Dec 23, 2014 3:19:32 PM Jing Created.
  17. *
  18. */
  19. public class MarkPost implements IPost{
  20. public void post() {
  21. System.out.println("This is MarkPost");
  22. }
  23. }
  1. /**
  2. *
  3. * @(#) SimplePost.java
  4. * @Package pattern.chp06.bridge.simple
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.simple;
  10. /**
  11. * 类描述:平信邮局
  12. *
  13. * @author: Jing
  14. * @version $Id: Exp$
  15. *
  16. * History: Dec 23, 2014 3:18:39 PM Jing Created.
  17. *
  18. */
  19. public class SimplePost implements IPost{
  20. public void post() {
  21. System.out.println("This is SimplePost");
  22. }
  23. }
  1. /**
  2. *
  3. * @(#) AbstractThing.java
  4. * @Package pattern.chp06.bridge.simple
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.simple;
  10. /**
  11. * 类描述:抽象事物
  12. *
  13. * @author: Jing
  14. * @version $Id: Exp$
  15. *
  16. * History: Dec 23, 2014 3:20:37 PM Jing Created.
  17. *
  18. */
  19. public abstract class AbstractThing {
  20. private IPost post;
  21. public AbstractThing(IPost post) {
  22. this.post = post;
  23. }
  24. public void post(){
  25. post.post();
  26. }
  27. }
  1. /**
  2. *
  3. * @(#) Lettle.java
  4. * @Package pattern.chp06.bridge.simple
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.simple;
  10. /**
  11. * 类描述:普通信件
  12. *
  13. * @author: Jing
  14. * @version $Id: Exp$
  15. *
  16. * History: Dec 23, 2014 3:21:36 PM Jing Created.
  17. *
  18. */
  19. public class Letter extends AbstractThing{
  20. public Letter(IPost post) {
  21. super(post);
  22. }
  23. }
  1. /**
  2. *
  3. * @(#) Parcel.java
  4. * @Package pattern.chp06.bridge.simple
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.simple;
  10. /**
  11. * 类描述:挂号
  12. *
  13. * @author: Jing
  14. * @version $Id: Exp$
  15. *
  16. * History: Dec 23, 2014 3:22:31 PM Jing Created.
  17. *
  18. */
  19. public class Parcel extends AbstractThing {
  20. public Parcel(IPost post) {
  21. super(post);
  22. }
  23. }
  1. /**
  2. *
  3. * @(#) MainTest.java
  4. * @Package pattern.chp06.bridge.simple
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.simple;
  10. import org.junit.Test;
  11. /**
  12. * 类描述:
  13. *
  14. * @author: Jing
  15. * @version $Id: Exp$
  16. *
  17. * History: Dec 23, 2014 3:23:16 PM Jing Created.
  18. *
  19. */
  20. public class MainTest {
  21. @Test
  22. public void testMain() throws Exception {
  23. IPost post = new SimplePost();
  24. Letter letter = new Letter(post);
  25. letter.post();
  26. }
  27. }

4,深入理解桥接模式
-1,桥接模式强调 包含 替代  继承
这种方式,但对于功能和事物来说两者分离,不会相互影响。
-2,JDK中桥接模式:Collectios.sort方法,借助Arrays.sort方法实现其自身内部的排序。
5,应用
        实现读取本地或者远程URL文件,文件类型是文本或图像文件。
分析:
        基本到手后实现的思路会如下所示,在TextRead中判断是本地还是URL方法,在其中调用不同的流读写。

技术分享
但是,当文件类型增多时,会实现多个子类重写,同时会破坏原有方法,需要改进原有方法。我们可以对功能方法进行复杂的拆分,如下UML
技术分享
代码如下:
  1. /**
  2. *
  3. * @(#) Iread.java
  4. * @Package pattern.chp06.bridge.read
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.read;
  10. /**
  11. * 类描述:抽象功能类
  12. *
  13. * @author: Jing
  14. * @version $Id: Exp$
  15. *
  16. * History: Dec 24, 2014 9:41:15 AM Jing Created.
  17. *
  18. */
  19. public interface IRead<T> {
  20. /**
  21. *
  22. * 方法说明:文件读取
  23. *
  24. * Author: Jing
  25. * Create Date: Dec 24, 2014 10:52:28 AM
  26. *
  27. * @return
  28. * @throws Exception
  29. * @return T
  30. */
  31. T read() throws Exception;
  32. }
  1. /**
  2. *
  3. * @(#) TextRead.java
  4. * @Package pattern.chp06.bridge.read
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.read;
  10. /**
  11. * 类描述:文本读取
  12. *
  13. * @author: Jing
  14. * @version $Id: Exp$
  15. *
  16. * History: Dec 24, 2014 10:04:08 AM Jing Created.
  17. *
  18. */
  19. public class TextRead implements IRead<String> {
  20. AbstractStream stream;
  21. public TextRead(AbstractStream stream) {
  22. this.stream = stream;
  23. }
  24. /**
  25. * 读取文本文件
  26. */
  27. public String read() throws Exception {
  28. byte[] buf = stream.readBytes();
  29. String s = new String(buf);
  30. return s;
  31. }
  32. }
  1. /**
  2. *
  3. * @(#) ImgRead.java
  4. * @Package pattern.chp06.bridge.read
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.read;
  10. /**
  11. * 类描述:读取图像文件
  12. *
  13. * @author: Jing
  14. * @version $Id: Exp$
  15. *
  16. * History: Dec 24, 2014 10:51:58 AM Jing Created.
  17. *
  18. */
  19. public class ImgRead implements IRead<byte[]> {
  20. AbstractStream stream;
  21. public ImgRead(AbstractStream stream) {
  22. this.stream = stream;
  23. }
  24. public byte[] read() throws Exception {
  25. return stream.readBytes();
  26. }
  27. }
  1. /**
  2. *
  3. * @(#) AbstractStream.java
  4. * @Package pattern.chp06.bridge.read
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.read;
  10. import java.io.InputStream;
  11. /**
  12. * 类描述:抽象基类流
  13. *
  14. * @author: Jing
  15. * @version $Id: Exp$
  16. *
  17. * History: Dec 24, 2014 10:05:54 AM Jing Created.
  18. *
  19. */
  20. public class AbstractStream {
  21. protected InputStream in;
  22. protected int size;
  23. /**
  24. *
  25. * 方法说明:读取对应输入流
  26. *
  27. * Author: Jing Create Date: Dec 24, 2014 10:11:00 AM
  28. *
  29. * @return
  30. * @throws Exception
  31. * @return byte[]
  32. */
  33. protected byte[] readBytes() throws Exception {
  34. byte[] buf = new byte[size];
  35. in.read(buf);
  36. return buf;
  37. }
  38. /**
  39. *
  40. * 方法说明:关闭输入流
  41. *
  42. * Author: Jing Create Date: Dec 24, 2014 10:11:24 AM
  43. *
  44. * @throws Exception
  45. * @return void
  46. */
  47. public void close() throws Exception {
  48. in.close();
  49. }
  50. }
  1. /**
  2. *
  3. * @(#) NativeStream.java
  4. * @Package pattern.chp06.bridge.read
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.read;
  10. import java.io.File;
  11. import java.io.FileInputStream;
  12. /**
  13. * 类描述:本地文件流读取
  14. *
  15. * @author: Jing
  16. * @version $Id: Exp$
  17. *
  18. * History: Dec 24, 2014 10:21:53 AM Jing Created.
  19. *
  20. */
  21. public class NativeStream extends AbstractStream{
  22. public NativeStream(String fileName) throws Exception{
  23. File f = new File(fileName);
  24. size = (int) f.length();
  25. in = new FileInputStream(f);
  26. }
  27. }
  1. /**
  2. *
  3. * @(#) URLStream.java
  4. * @Package pattern.chp06.bridge.read
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.read;
  10. import java.net.HttpURLConnection;
  11. import java.net.URL;
  12. /**
  13. * 类描述:URl文件读取
  14. *
  15. * @author: Jing
  16. * @version $Id: Exp$
  17. *
  18. * History: Dec 24, 2014 10:27:09 AM Jing Created.
  19. *
  20. */
  21. public class URLStream extends AbstractStream {
  22. public URLStream(String fileName) throws Exception{
  23. URL url = new URL(fileName);
  24. in = url.openStream();
  25. HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
  26. size = urlCon.getContentLength();
  27. }
  28. }
  1. /**
  2. *
  3. * @(#) AbstractThing.java
  4. * @Package pattern.chp06.bridge.read
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.read;
  10. /**
  11. * 类描述:抽象事务类
  12. *
  13. * @author: Jing
  14. * @version $Id: Exp$
  15. *
  16. * History: Dec 24, 2014 10:54:09 AM Jing Created.
  17. *
  18. */
  19. public abstract class AbstractThing {
  20. IRead<?> read;
  21. public AbstractThing(IRead<?> read) {
  22. this.read = read;
  23. }
  24. Object read() throws Exception{
  25. return read.read();
  26. }
  27. }
  1. /**
  2. *
  3. * @(#) NativeFile.java
  4. * @Package pattern.chp06.bridge.read
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.read;
  10. /**
  11. * 类描述:本地文件读取
  12. *
  13. * @author: Jing
  14. * @version $Id: Exp$
  15. *
  16. * History: Dec 24, 2014 10:55:59 AM Jing Created.
  17. *
  18. */
  19. public class NativeFile extends AbstractThing{
  20. public NativeFile(IRead<?> read) {
  21. super(read);
  22. }
  23. }
  1. /**
  2. *
  3. * @(#) URLFile.java
  4. * @Package pattern.chp06.bridge.read
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.read;
  10. /**
  11. * 类描述:URL文件读取
  12. *
  13. * @author: Jing
  14. * @version $Id: Exp$
  15. *
  16. * History: Dec 24, 2014 10:56:43 AM Jing Created.
  17. *
  18. */
  19. public class URLFile extends AbstractThing {
  20. public URLFile(IRead<?> read) {
  21. super(read);
  22. }
  23. }
  1. /**
  2. *
  3. * @(#) Main.java
  4. * @Package pattern.chp06.bridge.read
  5. *
  6. * Copyright ? JING Corporation. All rights reserved.
  7. *
  8. */
  9. package pattern.chp06.bridge.read;
  10. /**
  11. * 类描述:
  12. *
  13. * @author: Jing
  14. * @version $Id: Exp$
  15. *
  16. * History: Dec 24, 2014 11:14:31 AM Jing Created.
  17. *
  18. */
  19. public class Main {
  20. public static void main(String[] args) throws Exception {
  21. //打开远程文件
  22. AbstractStream in = new URLStream("http://js.t.sinajs.cn/t6/webim_prime/js/webim.js?v=1419389776378");
  23. TextRead textRead = new TextRead(in);
  24. AbstractThing thing = new URLFile(textRead);
  25. String s = (String) thing.read();
  26. in.close();
  27. System.out.println(s);
  28. }
  29. }
































读书笔记_java设计模式深入研究 第六章 桥接模式