Patterns in Java (in English)

原创 2003年03月07日 08:43:00

Patterns in Java <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

This article is contributed by Wang HaiLong.

Preface

The Java class library heavily employs Design Patterns. This article discusses such scenarios.

Iterator

Collection/Iterator in Java 2 is Iterator Pattern.

Decorator and Bridge

Let's see some Java code about filter stream.

Reading Socket

ServerSocket s = new ServerSocket(8189);

Socket incoming = s.accept();

BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));

 

Reading data from file by buffering mode

FileInputStream fin = new FileInputStream("employee.dat");

BufferedInputStream bin = new BufferedInputStream(fin);

DataInputStream din = new DataInputStream(bin);

 

Reading data in advance and by buffering mode

PushbackInputStream pbin = new PushbackInputStream(new BufferedInputStream(new FileInputStream("employee.dat")));

 

Reading data from zipped file

ZipInputStream zin = new ZipInputStream(new FileInputStream("employee.zip"));

DataInputStream din = new DataInputStream(zin);

 

From different points of view, we can say that the above code uses either Decorator Pattern or Bridge Pattern.

From the Decorator view, filter stream is Decorator, the parameter passed to its constructor is Component (Decorator and Component are participants in Decorator Pattern).

From the Bridge view, filter stream is Abstraction, the parameter passed to its constructor is Implementor (Abstraction and Implementor are participants in Bridge Pattern).

Adapter

There are Classes named "Adapter" such as WindowAdapter, ComponentAdapter and so on. But the aim of these Adapters is to implement default actions for listeners.

Observer

The Event mechanics in Java can be described as Observer Pattern. Listeners are Observers, and Event.getSource() return the Observable. One Observer can observe more than one Observable; one Observable can be observed by more than one Observer, which is called "Multicast".

Appendix

Some great books about Design Patterns:

<<Design Patterns>> by <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Zurich, Sydney, Urbana, Hawthorne;

<<Thinking in C++>> and <<Thinking in Java >> by Bruce Eckel;

<<The Design Patterns Java Companion>> by James W. Cooper.

 

 

 

java设计模式(Design Patterns)

设计模式(Design Patterns)                                   ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用、...
  • zhihui1017
  • zhihui1017
  • 2015年11月09日 10:54
  • 1404

Design Patterns: Elements of Reusable Object-Oriented Software

 GoF:(Gang of Four,GOF设计模式)---四人组      Design Patterns: Elements of Reusable Object-Oriented Softwar...
  • trueman_zh
  • trueman_zh
  • 2009年11月09日 11:27
  • 446

sentence patterns of English writing

1. …is one of the most...sth. that we have ever done.Lack of interpersonal skills is one of the most...
  • bill1973
  • bill1973
  • 2007年10月23日 02:46
  • 489

[LeetCode 273] Integer to English Word

Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
  • sbitswc
  • sbitswc
  • 2015年09月18日 07:21
  • 5206

java设计模式大全 Design pattern samples in Java(最经典最全的资料)

Design pattern samples in Java.Build status:IntroductionDesign patterns are formalized best practice...
  • changemyself
  • changemyself
  • 2015年06月19日 13:10
  • 10416

[leetcode] 273.Integer to English Words

题目: Convert a non-negative integer to its english words representation. Given input is guaranteed t...
  • u014673347
  • u014673347
  • 2015年08月31日 10:56
  • 696

Design Pattern----Creational Patterns

设想有这么一个问题,在一个迷宫游戏中
  • ccsu_001
  • ccsu_001
  • 2014年06月20日 17:14
  • 914

[leetcode-273]Integer to English Words(java)

问题描述: Convert a non-negative integer to its english words representation. Given input is guaranteed...
  • zdavb
  • zdavb
  • 2015年08月31日 12:55
  • 654

Django1.10中没有了patterns以后

背景:Django1.10以后的版本已经不再引入patterns了,所以需要修改一下原有的代码。 原有的代码: 1. view.py # coding: utf-8 from django.http ...
  • aduocd
  • aduocd
  • 2017年03月03日 14:27
  • 3027

django patterns使用方式

参考:http://www.360doc.com/content/14/0115/20/12050012_345553896.shtml
  • zhangweigangweiwu
  • zhangweigangweiwu
  • 2016年08月09日 11:19
  • 1464
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Patterns in Java (in English)
举报原因:
原因补充:

(最多只允许输入30个字)