Repository navigation
Expand file tree
/
Copy pathBehavioral-Design-Patterns-Note.txt
More file actions
77 lines (41 loc) · 2.7 KB
/
Copy pathBehavioral-Design-Patterns-Note.txt
File metadata and controls
77 lines (41 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Behavioral
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
=> This design patterns is all about Class's objects communication. i.e How do you want a behavior in software?
=> Behavioral patterns are about identifying common communication patterns between objects and realize these patterns.
1. Chain of Responsibility - A way of passing a request between a chain of objects
Example: Loan or Leave approval process
javax.servlet.Filter#doFilter()
2. Iterator - Sequentially access the elements of a collection
Example: Next/Previous buttons on TV
All implementations of java.util.Iterator & java.util.Enumeration
3. State - Alter an object's behavior when its state changes
Example: A fan wall control
4. Observer - A way of notifying change to a number of classes
Example: Bidding or auction
Publish/Subscribe JMS API
5. Visitor - Defines a new operation to a class without change
Example: Taxi
6. Template - Defer the exact steps of an algorithm to a subclass
Example: A blueprint
-> All non-abstract methods of java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.
-> All non-abstract methods of java.util.AbstractList, java.util.AbstractSet and java.util.AbstractMap.
-> javax.servlet.http.HttpServlet, all the doXXX() methods by default sends a HTTP 405 "Method Not Allowed" error to the response.
You're free to implement none or any of them.
-> JMSTemplate HibernateTemplate and JdbcTemplate in Spring
7. Command - Encapsulate a command request as an object
Example: The "Guest Check" at a diner is an example of a Command pattern.
The waiter or waitress takes an order or command from a customer and encapsulates that order by writing it on the check.
The order is then queued for a short order cook. Note that the pad of "checks" used by each waiter is not dependent on the menu,
and therefore they can support commands to cook many different items.
All implementations of java.lang.Runnable
8. Memento - Capture and restore an object's internal state
Example: save the state in a game & Undo/Redo operation in Windows
All implementations of java.io.Serializable
9. Mediator - Defines simplified communication between classes
Example: Air Traffic Controller(ATC)
10. Strategy - A Strategy defines a set of algorithms that can be used interchangeably.
Example : Modes of transportation
-> java.util.Comparator#compare(), executed by among others Collections#sort().
-> javax.servlet.http.HttpServlet, the service() and all doXXX() methods take HttpServletRequest
and HttpServletResponse and the implementor has to process them (and not to get hold of them as instance variables!).
-> javax.servlet.Filter#doFilter()