|
Design Pattern |
|
it serves to that service for that particular
client.
Let us consider an example of a restaurant,
which is based on command pattern
structure. Where the first step is the order
from the customer to the waiter for the food.
In the next command or step waiter takes the
order and forwards the order to the cook in
the kitchen. The cook can make various types
of food but now he prepares the ordered item
and hands it over to the waiter who in turn
serves to the customer.
Let us consider this example in term of java
code. As we know that the first step is the
order. The order is made of command in which
the customer gives the order for the food to
the waiter.
Order.java
package bahavioral.command;
public class Order {
private String cmd;
public Order(String cmd) {
this.cmd = cmd;
}
}
The next step is belonging to the waiter who
takes the order and forwards it to the cook.
After that waiter calls the prepareFood
method of the cook who in turn cooks. |
|
Waiter.java
package bahavioral.command;
public class Waiter {
public Food takeOrder(Customer cust, Order
ord) {
Cook cook = new Cook();
Food food = cook.prepareOrder(ord, this);
return food;
}
}
The waiter takes command and wraps it in an
order; the order is linked to a specific
customer. For, the cook, the order is linked to
a cook and also Food is linked to the Order.
Cook.java
package bahavioral.command;
public class Cook {
public Food prepareOrder(Order ord, Waiter
wtr) {
Food food = getCookedFood(ord);
return food;
}
public Food getCookedFood(Order ord) {
Food food = new Food(ord);
return food;
}
Here, the waiter takes command and wraps it
in an order; the order is associated to a
particular customer. The order is associated
to a cook and also Food is associated to the
Order.
The order is an object, which depends on the
command. The food item will change as soon
as the command changes. This is loose
coupling between the client and the
implementation.
Interpreter Pattern:
The Interpreter Pattern is a design pattern
that defines a grammatical representation for
a language along with an interpreter to
interpret sentences in the language. The best
example of an interpreted language is Java
itself, which converts the English-written code
to a byte code format, so that all the
operating systems can understand it. |
|
|
Dec
2007 | Java Jazz Up | 67 |
|
|
View All Topics |
All Pages of this Issue |
Pages:
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 ,
78 ,
79 ,
80 ,
81 ,
82 , Download PDF |
|
|
|
|
|
|
|
|
|