Magazine
 
Structural Design Patterns
 

//
questions.questDB = new CsharpQuestions();
//
questions.questDB = new CplusplusQuestions();

questions.display();
questions.next();

questions.newOne(“What is polymorphism? ”);
questions.newOne(“How many types of
polymorphism are there in java?”);
questions.displayAll();
}}

Here is the output of the above program:

C:\ Command Prompt
C:\> javac TestBridge.java
C:\> java TestBridge
What is Java?
~~~~~~~~~~~~~~~~~~~~~~~~
Question Catalog: Java Language
What is Java?
What is marker interface?
What is cross-platform?
How multiple polymorphism is achieved in
java?
How many types of exception handling are
there in java?
Define the keyword final for variable, method,
and class in java?
What is multi-tasking?
What is multi-threading?
What is polymorphism?
How many types of polymorphism are there in
java?
~~~~~~~~~~~~~~~~~~~~~~~~

The above example tries to show how the Bridge pattern decouples the interface from its implementation. One can easily notice that the class JavaQuestion can be launched independently working as an independent system.

III. Composite Pattern:

Individual objects as well as the composite objects can be represented with the Composite Design Pattern. Composite pattern represents

 

 

these objects with a tree structure. Suppose, within a company, there is an employee hierarchy where a manager has its subordinates, Project Leader also has the subordinates, while the developer has no subordinates.

Benefits: It is more flexible as compared to the static inheritance. It simplifies coding by implementing each feature in a class. It enhances the capability of an object as the new classes are created to add new features and make some changes.

Usage: This design pattern is used when the responsibilities are needed to be added dynamically to the individual objects without affecting other objects. Where an object’s responsibilities may vary from time to time. Now, let’s try to solve the above problem technically.

Develop a class “Employee” having the getters and setters for the attributes empname, empsal and emp subordinates.

Employee.java

class Employee {
String Empname;
double Empsalary;
Employee(String n, double s){
Empname = n;
Empsalary = s;
}
String getName() {
return Empname;
}
double getSalary() {
return Empsalary;
}
public String toString() {
return ”Employee” + Empname;
}
}

For example, General Manager may have several employee and some of them are Managers, further these managers have several employees. To illustrate all these things, let’s design a simple Manager class.

Oct 2007 | Java Jazz Up | 53
previous
index
next
 
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,   Download PDF