Skip to content

Commit 477d622

Browse files
committed
Merge pull request iluwatar#42 from joshzambales/master
Updated pull request
2 parents 46a6ff2 + 57c7a48 commit 477d622

File tree

15 files changed

+382
-2
lines changed

15 files changed

+382
-2
lines changed

README.md

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Design pattern samples in Java.
32

43
## Build status:
@@ -62,6 +61,13 @@ Behavioral patterns are concerned with algorithms and the assignment of responsi
6261
* [Callback](#callback)
6362
* [Execute Around](#execute-around)
6463

64+
### Presentation Tier Patterns
65+
66+
Presentation Tier patterns are the top-most level of the application, this is concerned with translating tasks and results to something the user can understand.
67+
68+
* [Intercepting Filter](#intercepting-filter)
69+
70+
6571
## <a name="abstract-factory">Abstract Factory</a> [&#8593;](#list-of-design-patterns)
6672
**Intent:** Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
6773

@@ -431,6 +437,17 @@ Behavioral patterns are concerned with algorithms and the assignment of responsi
431437
**Applicability:** Use the Callback pattern when
432438
* When some arbitrary synchronous or asynchronous action must be performed after execution of some defined activity.
433439

440+
441+
## <a name="intercepting-filter">Intercepting Filter</a> [&#8593;](#list-of-design-patterns)
442+
**Intent:** Provide pluggable filters to conduct necessary pre-processing and post-processing to requests from a client to a target
443+
444+
![alt text](https://github.com/iluwatar/java-design-patterns/blob/master/intercepting-filter/etc/Intercepting-filter.png "Intercepting Filter")
445+
446+
**Applicability:** Use the Intercepting Filter pattern when
447+
* a system uses pre-processing or post-processing requests
448+
* a system should do the authentication/ authorization/ logging or tracking of request and then pass the requests to corresponding handlers
449+
* you want a modular approach to configuring pre-processing and post-processing schemes
450+
434451
**Real world examples:**
435452
* [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.
436453

@@ -453,6 +470,7 @@ Behavioral patterns are concerned with algorithms and the assignment of responsi
453470
**Real world examples:**
454471
* [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain) prototype inheritance
455472

473+
456474
# Frequently asked questions
457475

458476
**<a id="Q1">Q: What is the difference between State and Strategy patterns?</a>**
@@ -476,7 +494,7 @@ The difference is the intent of the patterns. While Proxy controls access to the
476494
1. Fork the repository.
477495
2. Implement the code changes in your fork. Remember to add sufficient comments documenting the implementation.
478496
3. Create a simple class diagram from your example code.
479-
4. Add description of the pattern in README.md and link to the class diagram.
497+
4. Add description of the pattern in README.md and link to the class diagram.
480498
5. Create a pull request.
481499

482500
**For creating/editing UML diagrams** you need one of the following:
@@ -497,10 +515,13 @@ The difference is the intent of the patterns. While Proxy controls access to the
497515
* [Let’s Modify the Objects-First Approach into Design-Patterns-First](http://edu.pecinovsky.cz/papers/2006_ITiCSE_Design_Patterns_First.pdf)
498516
* [Pattern Languages of Program Design](http://www.amazon.com/Pattern-Languages-Program-Design-Coplien/dp/0201607344/ref=sr_1_1)
499517
* [Martin Fowler - Event Aggregator](http://martinfowler.com/eaaDev/EventAggregator.html)
518+
* [TutorialsPoint - Intercepting Filter](http://www.tutorialspoint.com/design_pattern/intercepting_filter_pattern.htm)
519+
* [Presentation Tier Pattern](http://www.javagyan.com/tutorials/corej2eepatterns/presentation-tier-patterns)
500520
* [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)
501521

502522

503523

524+
504525
# License
505526

506527
This project is licensed under the terms of the MIT license.
81.9 KB
Loading

intercepting-filter/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>intercepting-filter</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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.iluwatar;
2+
/**
3+
* Concrete implementation of filter
4+
* This filter is responsible for checking/filtering the input in the address field, returns null if field is empty
5+
* @author joshzambales
6+
*
7+
*/
8+
public class AddressFilter implements Filter{
9+
public String execute(String[] request){
10+
if(request[2].equals("")){
11+
return null;
12+
}else return request[2];
13+
}
14+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.iluwatar;
2+
import java.util.*;
3+
import javax.swing.*;
4+
import javax.swing.table.*;
5+
import java.awt.*;
6+
import java.awt.event.*;
7+
/**
8+
*
9+
* This is an app that checks whether the order request is valid through pre-processing done via Filters
10+
* Each field has its own corresponding Filter
11+
* @author joshzambales
12+
*
13+
*/
14+
public class App{
15+
public static void main(String[] args){
16+
FilterManager filterManager = new FilterManager(new Target());
17+
filterManager.setFilter(new NameFilter());
18+
filterManager.setFilter(new ContactFilter());
19+
filterManager.setFilter(new AddressFilter());
20+
filterManager.setFilter(new DepositFilter());
21+
filterManager.setFilter(new OrderFilter());
22+
23+
Client client = new Client();
24+
client.setFilterManager(filterManager);
25+
}
26+
}
27+
28+
29+
30+
31+
32+
33+
34+
35+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.iluwatar;
2+
import java.util.*;
3+
import javax.swing.*;
4+
import javax.swing.table.*;
5+
import java.awt.*;
6+
import java.awt.event.*;
7+
8+
/**
9+
* The Client class is responsible for handling the input and running them through filters inside the filterManager
10+
*
11+
* This is where Filters come to play as the client pre-processes the request before being displayed in the Target
12+
*
13+
* @author joshzambales
14+
*
15+
*/
16+
public class Client extends JFrame{
17+
private FilterManager filterManager;
18+
private JLabel jl;
19+
private JTextField[] jtFields;
20+
private JTextArea[] jtAreas;
21+
private JButton clearButton, processButton;
22+
public Client(){
23+
super("Client System");
24+
setDefaultCloseOperation(EXIT_ON_CLOSE);
25+
setSize(300,300);
26+
jl = new JLabel("RUNNING...");
27+
jtFields = new JTextField[3];
28+
for(int i = 0; i < 3; i++){
29+
jtFields[i] = new JTextField();
30+
}
31+
jtAreas = new JTextArea[2];
32+
for(int i = 0; i < 2; i++){
33+
jtAreas[i] = new JTextArea();
34+
}
35+
clearButton = new JButton("Clear");
36+
processButton = new JButton("Process");
37+
38+
setup();
39+
}
40+
private void setup(){
41+
setLayout(new BorderLayout());
42+
JPanel panel = new JPanel();
43+
add(jl,BorderLayout.SOUTH);
44+
add(panel, BorderLayout.CENTER);
45+
panel.setLayout(new GridLayout(6,2));
46+
panel.add(new JLabel("Name"));
47+
panel.add(jtFields[0]);
48+
panel.add(new JLabel("Contact Number"));
49+
panel.add(jtFields[1]);
50+
panel.add(new JLabel("Address"));
51+
panel.add(jtAreas[0]);
52+
panel.add(new JLabel("Deposit Number"));
53+
panel.add(jtFields[2]);
54+
panel.add(new JLabel("Order"));
55+
panel.add(jtAreas[1]);
56+
panel.add(clearButton);
57+
panel.add(processButton);
58+
59+
clearButton.addActionListener(new ActionListener(){
60+
@Override
61+
public void actionPerformed(ActionEvent e){
62+
for(JTextArea i : jtAreas){
63+
i.setText("");
64+
}
65+
for(JTextField i : jtFields){
66+
i.setText("");
67+
}
68+
}
69+
});
70+
71+
processButton.addActionListener(new ActionListener(){
72+
@Override
73+
public void actionPerformed(ActionEvent e){
74+
String request = String.format("%s&%s&%s&%s&%s",jtFields[0].getText(),jtFields[1].getText(),jtAreas[0].getText(),jtFields[2].getText(),jtAreas[1].getText());
75+
76+
jl.setText(sendRequest(request));
77+
}
78+
});
79+
80+
JRootPane rootPane = SwingUtilities.getRootPane(processButton);
81+
rootPane.setDefaultButton(processButton);
82+
setVisible(true);
83+
}
84+
public void setFilterManager(FilterManager filterManager){
85+
this.filterManager = filterManager;
86+
}
87+
public String sendRequest(String request){
88+
return filterManager.filterRequest(request);
89+
}
90+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.iluwatar;
2+
/**
3+
* Concrete implementation of filter
4+
* This filter checks for the contact field in which it checks if the input consist of numbers and it also checks if the input follows the length constraint (11 digits)
5+
* @author joshzambales
6+
*
7+
*/
8+
public class ContactFilter implements Filter{
9+
public String execute(String[] request){
10+
if(request[1].equals("") || request[1].matches(".*[^\\d]+.*") || request[1].length() != 11){
11+
return null;
12+
}else return request[1];
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.iluwatar;
2+
/**
3+
* Concrete implementation of filter
4+
*
5+
* This checks for the deposit code, returns null when deposit field is empty
6+
* @author joshzambales
7+
*
8+
*/
9+
public class DepositFilter implements Filter{
10+
public String execute(String[] request){
11+
if(request[3].equals("")){
12+
return null;
13+
}else return request[3];
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.iluwatar;
2+
/**
3+
* Filter interface
4+
* Filters perform certain tasks prior or after execution of request by request handler.
5+
* In this case, before the request is handled by the target, the request undergoes through each Filter
6+
* @author joshzambales
7+
*
8+
*/
9+
public interface Filter{
10+
public String execute(String[] request);
11+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.iluwatar;
2+
import java.util.*;
3+
/**
4+
* Filter Chain carries multiple filters and help to execute them in defined order on target.
5+
*
6+
* @author joshzambales
7+
*/
8+
public class FilterChain{
9+
private ArrayList<Filter> filters = new ArrayList<Filter>();
10+
private final Target target;
11+
12+
public FilterChain(Target target){
13+
this.target = target;
14+
}
15+
public void addFilter(Filter filter){
16+
filters.add(filter);
17+
}
18+
19+
public String execute(String request){
20+
String tempout[] = new String[filters.size()];
21+
22+
String tempin[] = request.split("&");
23+
int i = 0;
24+
try{
25+
for(Filter filter:filters){
26+
tempout[i] = null;
27+
tempout[i++] = filter.execute(tempin);
28+
}
29+
}catch(Exception e){
30+
return "NOT ENOUGHT INPUT";
31+
}
32+
33+
if(tempout[4] == null){
34+
return "INVALID ORDER!";
35+
}else if(tempout[3] == null){
36+
return "INVALID DEPOSIT NUMBER!";
37+
}else if(tempout[2] == null){
38+
return "INVALID ADRDESS!";
39+
}else if(tempout[1] == null){
40+
return "INVALID Contact Number!";
41+
}else if(tempout[0] == null){
42+
return "INVALID Name!";
43+
}else{
44+
target.execute(tempout);
45+
return "RUNNING...";
46+
}
47+
}
48+
49+
}

0 commit comments

Comments
 (0)