Patterns in Java (in English)

本文探讨Java类库中设计模式的应用场景。介绍了Java 2中的Collection/Iterator属于迭代器模式;以过滤流代码为例,从装饰器和桥接模式角度分析;还提到适配器类用于实现监听器默认动作,Java的事件机制符合观察者模式,并推荐了设计模式相关书籍。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 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 Zurich, Sydney, Urbana, Hawthorne;

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

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

 

 

 

### LeetCode Top 100 Problems Solutions in C Language LeetCode offers a wide range of algorithmic challenges that can be solved using various programming languages including C. For the popular or so-called "Top 100" problems on this platform, finding solutions specifically written in C might require exploring community contributions since official documentation may focus more on Python, Java, and JavaScript examples. To locate these resources: - **Community Contributions**: The site's discussion boards contain numerous posts where users share their implementations across different coding dialects. Searching within discussions with filters set to 'C' as the language tag could yield desired results. For instance, when dealing with subsets generation problem mentioned indirectly through provided references[^1], one approach involves recursive backtracking which has been implemented effectively by many contributors in C code snippets available online outside direct links given here but following similar logic patterns found at such platforms. A generic template for solving subset-related issues via recursion in C looks like below: ```c #include <stdio.h> #include <stdlib.h> void generateSubsets(int* nums, int numsSize, int* currentSubset, int currentIndex, int** result, int* returnSize, int** columnSizes); int main() { // Example usage would go here... } // Function definition goes here... ``` Regarding integer overflow checks during arithmetic operations as alluded to earlier[^2]: When manipulating integers especially under conditions prone to causing overflows (like multiplying large numbers), it is crucial to implement safeguards before performing calculations. This ensures program stability against unexpected behavior due to exceeding data type limits. Lastly, concerning linked list manipulations based upon node count constraints specified previously[^3]: Handling singly-linked lists often entails traversing elements while applying specific rules per element encountered until reaching terminal nodes according to defined criteria. #### Handling Palindrome Construction from Character Strings Mentioned Indirectly Through Provided References [^4] Given string `s` composed of lowercase English letters, constructing longest possible palindrome requires counting character frequencies first then deciding how they fit into symmetrical arrangement around center point(s). Example implementation strategy includes tallying occurrences efficiently followed by strategic placement ensuring maximum length adherence without violating palindromic properties. In summary, accessing curated collections of high-quality C-based resolutions for frequently asked questions posed atop competitive programming sites necessitates leveraging collective wisdom shared publicly alongside adapting general algorithms tailored towards target environments.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值