Skip to content

Commit ddace11

Browse files
committed
merged master fixed conflict
2 parents 6d4e473 + 46a6ff2 commit ddace11

File tree

18 files changed

+468
-2
lines changed

18 files changed

+468
-2
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
![Build status](https://travis-ci.org/iluwatar/java-design-patterns.svg?branch=master)
66

7+
## Introduction
8+
9+
Design patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system.
10+
11+
Design patterns can speed up the development process by providing tested, proven development paradigms.
12+
13+
Reusing design patterns helps to prevent subtle issues that can cause major problems, and it also improves code readability for coders and architects who are familiar with the patterns.
14+
715
## <a name="list-of-design-patterns">List of Design Patterns</a>
816

917
### Creational Patterns
@@ -14,8 +22,9 @@ Creational design patterns abstract the instantiation process. They help make a
1422
* [Builder](#builder)
1523
* [Factory Method](#factory-method)
1624
* [Prototype](#prototype)
25+
* [Property](#property)
1726
* [Singleton](#singleton)
18-
27+
1928
### Structural Patterns
2029

2130
Structural patterns are concerned with how classes and objects are composed to form larger structures.
@@ -50,6 +59,7 @@ Behavioral patterns are concerned with algorithms and the assignment of responsi
5059
* [Double Checked Locking](#double-checked-locking)
5160
* [Null Object](#null-object)
5261
* [Callback](#callback)
62+
* [Execute Around](#execute-around)
5363

5464
### Presentation Tier Patterns
5565

@@ -427,6 +437,7 @@ Presentation Tier patterns are the top-most level of the application, this is co
427437
**Applicability:** Use the Callback pattern when
428438
* When some arbitrary synchronous or asynchronous action must be performed after execution of some defined activity.
429439

440+
<<<<<<< HEAD
430441
## <a name="intercepting-filter">Intercepting Filter</a> [&#8593;](#list-of-design-patterns)
431442
**Intent:** Provide pluggable filters to conduct necessary pre-processing and post-processing to requests from a client to a target
432443

@@ -436,6 +447,29 @@ Presentation Tier patterns are the top-most level of the application, this is co
436447
* a system uses pre-processing or post-processing requests
437448
* a system should do the authentication/ authorization/ logging or tracking of request and then pass the requests to corresponding handlers
438449
* you want a modular approach to configuring pre-processing and post-processing schemes
450+
=======
451+
**Real world examples:**
452+
* [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.
453+
454+
## <a name="execute-around">Execute Around</a> [&#8593;](#list-of-design-patterns)
455+
**Intent:** Execute Around idiom frees the user from certain actions that should always be executed before and after the business method. A good example of this is resource allocation and deallocation leaving the user to specify only what to do with the resource.
456+
457+
![alt text](https://github.com/iluwatar/java-design-patterns/blob/master/execute-around/execute-around.png "Execute Around")
458+
459+
**Applicability:** Use the Execute Around idiom when
460+
* You use an API that requires methods to be called in pairs such as open/close or allocate/deallocate.
461+
462+
## <a name="property">Property</a> [&#8593;](#list-of-design-patterns)
463+
**Intent:** Create hierarchy of objects and new objects using already existing objects as parents.
464+
465+
![alt text](https://github.com/iluwatar/java-design-patterns/blob/master/property/etc/property.jpg "Property")
466+
467+
**Applicability:** Use the Property pattern when
468+
* when you like to have objects with dynamic set of fields and prototype inheritance
469+
470+
**Real world examples:**
471+
* [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain) prototype inheritance
472+
>>>>>>> origin/master
439473
440474
# Frequently asked questions
441475

@@ -481,8 +515,14 @@ The difference is the intent of the patterns. While Proxy controls access to the
481515
* [Let’s Modify the Objects-First Approach into Design-Patterns-First](http://edu.pecinovsky.cz/papers/2006_ITiCSE_Design_Patterns_First.pdf)
482516
* [Pattern Languages of Program Design](http://www.amazon.com/Pattern-Languages-Program-Design-Coplien/dp/0201607344/ref=sr_1_1)
483517
* [Martin Fowler - Event Aggregator](http://martinfowler.com/eaaDev/EventAggregator.html)
518+
<<<<<<< HEAD
484519
* [TutorialsPoint - Intercepting Filter](http://www.tutorialspoint.com/design_pattern/intercepting_filter_pattern.htm)
485520
* [Presentation Tier Pattern](http://www.javagyan.com/tutorials/corej2eepatterns/presentation-tier-patterns)
521+
=======
522+
* [Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions](http://www.amazon.com/Functional-Programming-Java-Harnessing-Expressions/dp/1937785467/ref=sr_1_1)
523+
524+
525+
>>>>>>> origin/master
486526
487527
# License
488528

callback/src/main/java/com/iluwatar/App.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.iluwatar;
22

33
/**
4-
* Callback pattern is more native for dynamic languages where function are first-class citizen.
4+
* Callback pattern is more native for functional languages where function is treated as first-class citizen.
55
* Prior to Java8 can be simulated using simple (alike command) interfaces.
66
*/
77
public class App {

execute-around/execute-around.png

4.48 KB
Loading

execute-around/execute-around.ucls

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
3+
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
4+
<class id="1" language="java" name="com.iluwatar.SimpleFileWriter" project="execute-around"
5+
file="/execute-around/src/main/java/com/iluwatar/SimpleFileWriter.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="88" width="261" x="213" y="298"/>
7+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</class>
13+
<interface id="2" language="java" name="com.iluwatar.FileWriterAction" project="execute-around"
14+
file="/execute-around/src/main/java/com/iluwatar/FileWriterAction.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="88" width="164" x="514" y="298"/>
16+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</interface>
22+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
23+
sort-features="false" accessors="true" visibility="true">
24+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
25+
<operations public="true" package="true" protected="true" private="true" static="true"/>
26+
</classifier-display>
27+
<association-display labels="true" multiplicity="true"/>
28+
</class-diagram>

execute-around/pom.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>execute-around</artifactId>
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<scope>test</scope>
16+
</dependency>
17+
</dependencies>
18+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.iluwatar;
2+
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
6+
/**
7+
* The Execute Around idiom specifies some code to be executed before and after
8+
* a method. Typically the idiom is used when the API has methods to be executed in
9+
* pairs, such as resource allocation/deallocation or lock acquisition/release.
10+
*
11+
* In this example, we have SimpleFileWriter class that opens and closes the file
12+
* for the user. The user specifies only what to do with the file by providing the
13+
* FileWriterAction implementation.
14+
*
15+
*/
16+
public class App {
17+
18+
public static void main( String[] args ) throws IOException {
19+
20+
new SimpleFileWriter("testfile.txt", new FileWriterAction() {
21+
22+
@Override
23+
public void writeFile(FileWriter writer) throws IOException {
24+
writer.write("Hello");
25+
writer.append(" ");
26+
writer.append("there!");
27+
}
28+
});
29+
}
30+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.iluwatar;
2+
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
6+
/**
7+
*
8+
* Interface for specifying what to do with the file resource.
9+
*
10+
*/
11+
public interface FileWriterAction {
12+
13+
void writeFile(FileWriter writer) throws IOException;
14+
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.iluwatar;
2+
3+
import java.io.FileWriter;
4+
import java.io.IOException;
5+
6+
/**
7+
*
8+
* SimpleFileWriter handles opening and closing file for the user. The user
9+
* only has to specify what to do with the file resource through FileWriterAction
10+
* parameter.
11+
*
12+
*/
13+
public class SimpleFileWriter {
14+
15+
public SimpleFileWriter(String filename, FileWriterAction action) throws IOException {
16+
FileWriter writer = new FileWriter(filename);
17+
try {
18+
action.writeFile(writer);
19+
} finally {
20+
writer.close();
21+
}
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.iluwatar;
2+
3+
import java.io.IOException;
4+
5+
import org.junit.Test;
6+
7+
/**
8+
*
9+
* Tests execute-around example.
10+
*
11+
*/
12+
public class AppTest {
13+
14+
@Test
15+
public void test() throws IOException {
16+
String[] args = {};
17+
App.main(args);
18+
}
19+
}

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@
4141
<module>null-object</module>
4242
<module>event-aggregator</module>
4343
<module>callback</module>
44+
<<<<<<< HEAD
4445
<module>intercepting-filter</module>
4546
</modules>
47+
=======
48+
<module>execute-around</module>
49+
<module>property</module>
50+
</modules>
51+
>>>>>>> origin/master
4652

4753
<dependencyManagement>
4854
<dependencies>

0 commit comments

Comments
 (0)