|
Design Pattern |
|
1. Log.java
public
abstract class Log {
public abstract void write(String
log);
}
2.
DatabaseLog.java
public
class DatabaseLog extends Log {
// Write to the Database
public void write(String log){
}
}
3.
DecoratorLog.java
public
abstract DecoratorLog extends Log {
protected Log objLog;
public DecoratorLog(Log objLog) {
this.objLog = objLog;
}
public void write(String log){
// Write to the Textfile
objLog.write(log);
}
}
4.
DecoratorLogA.java
public
class DecoratorLogA extends
DecoratorLog {
public DecoratorLogA(Log aLog) {
super(objLog);
}
public void write(String log){
// Decorator A behavior here
objLog.write(String log);
}
}
5.
DecoratorLogB.java
public
class DecoratorLogB extends
DecoratorLog {
public DecoratorLogB(Log aLog) {
super(objLog);
}
public void write(String log){
// Decorator B behavior here
objLog.write(String log);
}
} |
|
In the given code, the method write(String log) of the abstract class Log is extended by
the decorator class (DecoratorLog) and its subclasses to add the new functionality (such
as write to the Textfile) to the method.
You can also define an interface in place of an
abstract class shown as:
Log.java
interface Log{
public void write(String log);
}
Once you have defined the interface, you can
implement its behaviors in any class e.g.
public class DatabaseLog implements Log
{public void write(String log){// Decorator A
behavior here}}
II. Facade Design Pattern
The Facade Pattern is an object-oriented design pattern, which provides a simplified interface to a larger body of code. This pattern can make the task of accessing a large number of modules much simpler by providing an additional and a higher-level interface layer to an entire subsystem of objects that makes the subsystem easier to use. Thus, it hides the complexities and the implementation of the subsystem from clients.
A facade is an object that is placed between the group of objects and its client who accesses the system.
Facade Pattern : When to use
• When a way of hiding a complex system within a simple interface is required.
• When lots of dependencies between client and its implementation class of an
abstraction are required.
• When you want to make subsystems in layers.
|
|
|
Nov 2007 | Java Jazz Up |38 |
|
|
|
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 Download PDF |
|
|
|
|
|
|