Patterns in java (in English)

zhaozj2021-02-16  55

Patterns in java

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 itrator 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 InputStreamRead);

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 FileInputStream (New FileInputStream ("Employee.dat")))

Reading Data from Zipped File

ZipinputStream ZIN = New FileInputStream (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 Partic).

From The Bridge View, Filter Stream Is Abstract, The Parameter Passed To ITS Constructor Is Implementor (Abstract and Implementor Are Particles).

Adapter

There Are Classes Named "Adapter" Such As Windowadapter, Componentadapter and So. But The AIM of these Adapters is to mailment 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

Zurich

,

Sydney

,

Urbana

,

Hawthorne

;

<< Thinking in C >> and << Thinking in java >> by bruce eckel;

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

转载请注明原文地址:https://www.9cbs.com/read-23526.html

New Post(0)