GeezerVison is showing github.com — Work towards #56: Splitting up the README · wangwch/java-design-patterns@fbb12b5
File treeExpand file treeCollapse file tree Open diff view settings resource-acquisition-is-initialization
Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +1-759Lines changed: 1 addition & 759 deletions - Display the source diff
- Display the rich diff
Large diffs are not rendered by default. Collapse file Copy file name to clipboard +25Lines changed: 25 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- | | 2 | +layout: pattern | | 3 | +title: Abstract Factory | | 4 | +folder: abstract-factory | | 5 | +categories: | | 6 | + - pattern_cat | | 7 | + - creational | | 8 | +tags: pattern_tag | | 9 | +--- | | 10 | + | | 11 | +**Intent:** Provide an interface for creating families of related or dependent | | 12 | +objects without specifying their concrete classes. | | 13 | + | | 14 | + | | 15 | + | | 16 | +**Applicability:** Use the Abstract Factory pattern when | | 17 | + | | 18 | +* a system should be independent of how its products are created, composed and represented | | 19 | +* a system should be configured with one of multiple families of products | | 20 | +* a family of related product objects is designed to be used together, and you need to enforce this constraint | | 21 | +* you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations | | 22 | + | | 23 | +**Real world examples:** | | 24 | + | | 25 | +* [javax.xml.parsers.DocumentBuilderFactory](http://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html) |
Collapse file Copy file name to clipboard +23Lines changed: 23 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- | | 2 | +layout: pattern | | 3 | +title: Adapter | | 4 | +folder: adapter | | 5 | +categories: pattern_cat | | 6 | +tags: pattern_tag | | 7 | +--- | | 8 | + | | 9 | +**Intent:** Convert the interface of a class into another interface the clients | | 10 | +expect. Adapter lets classes work together that couldn't otherwise because of | | 11 | +incompatible interfaces. | | 12 | + | | 13 | + | | 14 | + | | 15 | +**Applicability:** Use the Adapter pattern when | | 16 | + | | 17 | +* you want to use an existing class, and its interface does not match the one you need | | 18 | +* you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that don't necessarily have compatible interfaces | | 19 | +* you need to use several existing subclasses, but it's impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class. | | 20 | + | | 21 | +**Real world examples:** | | 22 | + | | 23 | +* [java.util.Arrays#asList()](http://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#asList%28T...%29) |
Collapse file Copy file name to clipboard +26Lines changed: 26 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- | | 2 | +layout: pattern | | 3 | +title: Async Method Invocation | | 4 | +folder: async-method-invocation | | 5 | +categories: pattern_cat | | 6 | +tags: pattern_tag | | 7 | +--- | | 8 | + | | 9 | +**Intent:** Asynchronous method invocation is pattern where the calling thread | | 10 | +is not blocked while waiting results of tasks. The pattern provides parallel | | 11 | +processing of multiple independent tasks and retrieving the results via | | 12 | +callbacks or waiting until everything is done. | | 13 | + | | 14 | + | | 15 | + | | 16 | +**Applicability:** Use async method invocation pattern when | | 17 | + | | 18 | +* you have multiple independent tasks that can run in parallel | | 19 | +* you need to improve the performance of a group of sequential tasks | | 20 | +* you have limited amount of processing capacity or long running tasks and the | | 21 | + caller should not wait the tasks to be ready | | 22 | + | | 23 | +**Real world examples:** | | 24 | + | | 25 | +* [FutureTask](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/FutureTask.html), [CompletableFuture](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CompletableFuture.html) and [ExecutorService](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html) (Java) | | 26 | +* [Task-based Asynchronous Pattern](https://msdn.microsoft.com/en-us/library/hh873175.aspx) (.NET) |
Collapse file Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- | | 2 | +layout: pattern | | 3 | +title: Bridge | | 4 | +folder: bridge | | 5 | +categories: pattern_cat | | 6 | +tags: pattern_tag | | 7 | +--- | | 8 | + | | 9 | +**Intent:** Decouple an abstraction from its implementation so that the two can | | 10 | +vary independently. | | 11 | + | | 12 | + | | 13 | + | | 14 | + | | 15 | +**Applicability:** Use the Bridge pattern when | | 16 | + | | 17 | +* you want to avoid a permanent binding between an abstraction and its implementation. This might be the case, for example, when the implementation must be selected or switched at run-time. | | 18 | +* both the abstractions and their implementations should be extensible by subclassing. In this case, the Bridge pattern lets you combine the different abstractions and implementations and extend them independently | | 19 | +* changes in the implementation of an abstraction should have no impact on clients; that is, their code should not have to be recompiled. | | 20 | +* you have a proliferation of classes. Such a class hierarchy indicates the need for splitting an object into two parts. Rumbaugh uses the term "nested generalizations" to refer to such class hierarchies | | 21 | +* you want to share an implementation among multiple objects (perhaps using reference counting), and this fact should be hidden from the client. A simple example is Coplien's String class, in which multiple objects can share the same string representation. |
Collapse file Copy file name to clipboard +23Lines changed: 23 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- | | 2 | +layout: pattern | | 3 | +title: Builder | | 4 | +folder: builder | | 5 | +categories: creational | | 6 | +tags: pattern_tag | | 7 | +--- | | 8 | + | | 9 | +**Intent:** Separate the construction of a complex object from its | | 10 | +representation so that the same construction process can create different | | 11 | +representations. | | 12 | + | | 13 | + | | 14 | + | | 15 | +**Applicability:** Use the Builder pattern when | | 16 | + | | 17 | +* the algorithm for creating a complex object should be independent of the parts that make up the object and how they're assembled | | 18 | +* the construction process must allow different representations for the object that's constructed | | 19 | + | | 20 | +**Real world examples:** | | 21 | + | | 22 | +* [java.lang.StringBuilder](http://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html) | | 23 | +* [Apache Camel builders](https://github.com/apache/camel/tree/0e195428ee04531be27a0b659005e3aa8d159d23/camel-core/src/main/java/org/apache/camel/builder) |
Collapse file Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- | | 2 | +layout: pattern | | 3 | +title: Business Delegate | | 4 | +folder: business-delegate | | 5 | +categories: pattern_cat | | 6 | +tags: pattern_tag | | 7 | +--- | | 8 | + | | 9 | +**Intent:** The Business Delegate pattern adds an abstraction layer between | | 10 | +presentation and business tiers. By using the pattern we gain loose coupling | | 11 | +between the tiers and encapsulate knowledge about how to locate, connect to, | | 12 | +and interact with the business objects that make up the application. | | 13 | + | | 14 | + | | 15 | + | | 16 | +**Applicability:** Use the Business Delegate pattern when | | 17 | + | | 18 | +* you want loose coupling between presentation and business tiers | | 19 | +* you want to orchestrate calls to multiple business services | | 20 | +* you want to encapsulate service lookups and service calls |
Collapse file Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- | | 2 | +layout: pattern | | 3 | +title: Callback | | 4 | +folder: callback | | 5 | +categories: pattern_cat | | 6 | +tags: pattern_tag | | 7 | +--- | | 8 | + | | 9 | +**Intent:** Callback is a piece of executable code that is passed as an | | 10 | +argument to other code, which is expected to call back (execute) the argument | | 11 | +at some convenient time. | | 12 | + | | 13 | + | | 14 | + | | 15 | +**Applicability:** Use the Callback pattern when | | 16 | + | | 17 | +* when some arbitrary synchronous or asynchronous action must be performed after execution of some defined activity. | | 18 | + | | 19 | +**Real world examples:** | | 20 | + | | 21 | +* [CyclicBarrier] (http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CyclicBarrier.html#CyclicBarrier%28int,%20java.lang.Runnable%29) constructor can accept callback that will be triggered every time when barrier is tripped. |
Collapse file Copy file name to clipboard +24Lines changed: 24 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- | | 2 | +layout: pattern | | 3 | +title: Chain of responsibility | | 4 | +folder: chain-of-responsibility | | 5 | +categories: pattern_cat | | 6 | +tags: pattern_tag | | 7 | +--- | | 8 | + | | 9 | +**Intent:** Avoid coupling the sender of a request to its receiver by giving | | 10 | +more than one object a chance to handle the request. Chain the receiving | | 11 | +objects and pass the request along the chain until an object handles it. | | 12 | + | | 13 | + | | 14 | + | | 15 | +**Applicability:** Use Chain of Responsibility when | | 16 | + | | 17 | +* more than one object may handle a request, and the handler isn't known a priori. The handler should be ascertained automatically | | 18 | +* you want to issue a request to one of several objects without specifying the receiver explicitly | | 19 | +* the set of objects that can handle a request should be specified dynamically | | 20 | + | | 21 | +**Real world examples:** | | 22 | + | | 23 | +* [java.util.logging.Logger#log()](http://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html#log%28java.util.logging.Level,%20java.lang.String%29) | | 24 | +* [Apache Commons Chain](https://commons.apache.org/proper/commons-chain/index.html) |
Collapse file Copy file name to clipboard +31Lines changed: 31 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +--- | | 2 | +layout: pattern | | 3 | +title: Command | | 4 | +folder: command | | 5 | +categories: pattern_cat | | 6 | +tags: pattern_tag | | 7 | +--- | | 8 | + | | 9 | +**Intent:** Encapsulate a request as an object, thereby letting you | | 10 | +parameterize clients with different requests, queue or log requests, and | | 11 | +support undoable operations. | | 12 | + | | 13 | + | | 14 | + | | 15 | +**Applicability:** Use the Command pattern when you want to | | 16 | + | | 17 | +* parameterize objects by an action to perform. You can express such parameterization in a procedural language with a callback function, that is, a function that's registered somewhere to be called at a later point. Commands are an object-oriented replacement for callbacks. | | 18 | +* specify, queue, and execute requests at different times. A Command object can have a lifetime independent of the original request. If the receiver of a request can be represented in an address space-independent way, then you can transfer a command object for the request to a different process and fulfill the request there | | 19 | +* support undo. The Command's execute operation can store state for reversing its effects in the command itself. The Command interface must have an added Unexecute operation that reverses the effects of a previous call to execute. Executed commands are stored in a history list. Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling unexecute and execute, respectively | | 20 | +* support logging changes so that they can be reapplied in case of a system crash. By augmenting the Command interface with load and store operations, you can keep a persistent log of changes. Recovering from a crash involves reloading logged commands from disk and re-executing them with the execute operation | | 21 | +* structure a system around high-level operations build on primitive operations. Such a structure is common in information systems that support transactions. A transaction encapsulates a set of changes to data. The Command pattern offers a way to model transactions. Commands have a common interface, letting you invoke all transactions the same way. The pattern also makes it easy to extend the system with new transactions | | 22 | + | | 23 | +**Typical Use Case:** | | 24 | + | | 25 | +* to keep a history of requests | | 26 | +* implement callback functionality | | 27 | +* implement the undo functionality | | 28 | + | | 29 | +**Real world examples:** | | 30 | + | | 31 | +* [java.lang.Runnable](http://docs.oracle.com/javase/8/docs/api/java/lang/Runnable.html) |
|