Design Patterns
239 pág.

Design Patterns


DisciplinaEngenharia de Software I5.620 materiais57.328 seguidores
Pré-visualização50 páginas
Process Order ORD1 Dated D1 For Customer 
C1.
Sending via email: Unable to Process Order ORD1 Dated D1 For Customer 
C1.
Writing to console: Order Dispatched.
Sending via email: Order Dispatched.
*/
Another Example
Below is another example of this pattern in Java. In this example we have different roles, each having a fixed
purchasing limit and a successor. Every time a user in a role receives a purchase request that exceeds his or her limit,
the request is passed to his or her successor.
The PurchasePower abstract class with the abstract method processRequest.
abstract class PurchasePower {
 protected final double base = 500;
 protected PurchasePower successor;
 public void setSuccessor(PurchasePower successor) {
 this.successor = successor;
 }
 abstract public void processRequest(PurchaseRequest request);
}
Four implementations of the abstract class above: Manager, Director, Vice President, President
class ManagerPPower extends PurchasePower {
 private final double ALLOWABLE = 10 * base;
 public void processRequest(PurchaseRequest request) {
 if (request.getAmount() < ALLOWABLE) {
Chain-of-responsibility pattern 123
 System.out.println(&quot;Manager will approve $&quot; + 
request.getAmount());
 } else if (successor != null) {
 successor.processRequest(request);
 }
 }
}
class DirectorPPower extends PurchasePower {
 private final double ALLOWABLE = 20 * base;
 public void processRequest(PurchaseRequest request) {
 if (request.getAmount() < ALLOWABLE) {
 System.out.println(&quot;Director will approve $&quot; + 
request.getAmount());
 } else if (successor != null) {
 successor.processRequest(request);
 }
 }
}
class VicePresidentPPower extends PurchasePower {
 private final double ALLOWABLE = 40 * base;
 public void processRequest(PurchaseRequest request) {
 if (request.getAmount() < ALLOWABLE) {
 System.out.println(&quot;Vice President will approve $&quot; + 
request.getAmount());
 } else if (successor != null) {
 successor.processRequest(request);
 }
 }
}
class PresidentPPower extends PurchasePower {
 private final double ALLOWABLE = 60 * base;
 
 public void processRequest(PurchaseRequest request) {
 if (request.getAmount() < ALLOWABLE) {
 System.out.println(&quot;President will approve $&quot; + 
request.getAmount());
 } else {
 System.out.println( &quot;Your request for $&quot; + 
request.getAmount() + &quot; needs a board meeting!&quot;);
 }
 }
}
Chain-of-responsibility pattern 124
The following code defines the PurchaseRequest class that keeps the request data in this example.
class PurchaseRequest {
 private int number;
 private double amount;
 private String purpose;
 public PurchaseRequest(int number, double amount, String purpose) {
 this.number = number;
 this.amount = amount;
 this.purpose = purpose;
 }
 public double getAmount() {
 return amount;
 }
 public void setAmount(double amt) {
 amount = amt;
 }
 
 public String getPurpose() {
 return purpose;
 }
 public void setPurpose(String reason) {
 purpose = reason;
 }
 public int getNumber(){
 return number;
 }
 public void setNumber(int num) {
 number = num;
 } 
}
In the following usage example, the successors are set as follows: Manager -> Director -> Vice President ->
President
class CheckAuthority {
 public static void main(String[] args) {
 ManagerPPower manager = new ManagerPPower();
 DirectorPPower director = new DirectorPPower();
 VicePresidentPPower vp = new VicePresidentPPower();
 PresidentPPower president = new PresidentPPower();
 manager.setSuccessor(director);
 director.setSuccessor(vp);
 vp.setSuccessor(president);
 
 // Press Ctrl+C to end.
Chain-of-responsibility pattern 125
 try {
 while (true) {
 System.out.println(&quot;Enter the amount to check who 
should approve your expenditure.&quot;);
 System.out.print(&quot;>&quot;);
 double d = Double.parseDouble(new BufferedReader(new 
InputStreamReader(System.in)).readLine());
 manager.processRequest(new PurchaseRequest(0, d, 
&quot;General&quot;));
 }
 } catch(Exception e) {
 System.exit(1);
 } 
 }
}
External links
\u2022 Article &quot;The Chain of Responsibility pattern's pitfalls and improvements [1]&quot; by Michael Xinsheng Huang
\u2022 Article &quot;Follow the Chain of Responsibility [2]&quot; by David Geary
\u2022 Article &quot;Pattern Summaries: Chain of Responsibility [3]&quot; by Mark Grand
\u2022 Behavioral Patterns - Chain of Responsibility Pattern [4]
\u2022 Descriptions from Portland Pattern Repository [5]
\u2022 Apache Jakarta Commons Chain [6]
\u2022 PerfectJPattern Open Source Project [7], Provides a context-free and type-safe implementation of the Chain of
Responsibility Pattern in Java
\u2022 Chain.NET(NChain) [8] - Ready-to-use, generic and lightweight implementation of the Chain of Responsibility
pattern for .NET and Mono
\u2022 Chain of Responsibility Design Pattern [9] - An Example
\u2022 Sourcemaking Tutorial [10]
References
[1] http:/ / www. javaworld. com/ javaworld/ jw-08-2004/ jw-0816-chain. html
[2] http:/ / www. javaworld. com/ javaworld/ jw-08-2003/ jw-0829-designpatterns. html
[3] http:/ / developer. com/ java/ other/ article. php/ 631261
[4] http:/ / allapplabs. com/ java_design_patterns/ chain_of_responsibility_pattern. htm
[5] http:/ / c2. com/ cgi/ wiki?ChainOfResponsibilityPattern
[6] http:/ / jakarta. apache. org/ commons/ chain/
[7] http:/ / perfectjpattern. sourceforge. net/ dp-chainofresponsibility. html
[8] http:/ / nchain. sourceforge. net
[9] http:/ / patrickmcguirk. net/ papers/ chainofresponsibility. html
[10] http:/ / sourcemaking. com/ design_patterns/ chain_of_responsibility
Command pattern 126
Command pattern
In object-oriented programming, the command pattern is a design pattern in which an object is used to represent
and encapsulate all the information needed to call a method at a later time. This information includes the method
name, the object that owns the method and values for the method parameters.
Three terms always associated with the command pattern are client, invoker and receiver. The client instantiates the
command object and provides the information required to call the method at a later time. The invoker decides when
the method should be called. The receiver is an instance of the class that contains the method's code.
Using command objects makes it easier to construct general components that need to delegate, sequence or execute
method calls at a time of their choosing without the need to know the owner of the method or the method parameters.
Uses
Command objects are useful for implementing:
Multi-level undo
If all user actions in a program are implemented as command objects, the program can keep a stack of the most
recently executed commands. When the user wants to undo a command, the program simply pops the most
recent command object and executes its undo() method.
Transactional behavior
Similar to undo, a database engine or software installer may keep a list of operations that have been or will be
performed. Should one of them fail, all others can be reverted or discarded (usually called rollback). For
example, if two database tables which refer to each other must be updated, and the second update fails, the
transaction can be rolled back, so that the first