Magazine
 
Design Pattern
 
 
Also, the Not an Administrator

public class NotAnAdministrator extends Person {
public NotanAdministrator(String fullNname) {
System.out.println(“Hello Mr. “ + fullNname + “you are not the
administrator of this organization”);
}}

Now, we will create the Verification class which will return a
message depending on the data provided.

public class Verification {
public static void main(String args[]) {
Verification verify = new Verification();
verify.getEmployee(args[0], args[1]);
}
public Employee getEmployee(String name, String Id) {
if ((name.equals(“Zulfiqar”)&&(Id.equals(“123542”)))
return new Administrator(name);
else
return new NotanAdministrator(name);
}}

This class takes two arguments from the system at runtime
and prints the names and a message accordingly.

Running the program:

After compilation, run the code with the arguments Zulfiqar
and 123542: java Zulfiqar 123542
The result returned is: “Hello Mr. Zulfiqar

You are the administrator of the organization”.

II Abstract Factory Pattern:


This pattern is one level of abstraction higher than factory pattern. This means that the abstract factory returns the factory of classes. Like Factory pattern returned one of the several sub-classes, this returns such factory which later will return one of the subclasses.

It provides a way to encapsulate a group of several related factories. This method is used when to return one of several related classes of objects and each of which have the capability of returning several objects of different types on request. This pattern provides separation from the implementation details of a set of objects from its general usage. This pattern hides the concrete subclass from the client and should be used when the system is independent of how the components are organized.

This pattern allows to interchange the concrete classes
without changing the code that they uses even at runtime.

 

However this pattern incurs the risk of unnecessary complexity.

Benefits: The client does no need to specify the type of the concrete class because the abstract factory creates the actual concrete objects by reading the type of the concrete object from the configuration and returns the abstract pointer of those objects. The client can only access to these objects through their abstract interfaces. It defines a class library of products that provides expose to interface and creates the families of related objects as Kit. It tries to enforce the constraints and includes the related patterns. It provides independency required by the system from how its products are created, composed and support for a system or a family of systems to be extensible.

Usage: It is used to construct the complex objects that are independent of how to make up the objects and how the parts are assembled. The construction process must allow the constructed object to represent differently. It is used to make the concrete classes isolate from their super classes. The client code have no need to add the header files, class declarations and also no need to know about the concrete class. The abstract factory class creates the objects of the concrete class these objects are accessed by the client’s code.

Let’s take an example to clearly understand this pattern.

Suppose we need the configuration of a Computer. RAM, Hard disk and Processor are the different parts of computer and workstation Server and PC are the different types of computers.

Therefore we are taking the Computer as the abstract base
class.

package creational.abstractfactory;
public abstract class Computer {
public abstract Parts getHarddisk();
public abstract Parts getRAM();
public abstract Parts getProcessor();
}
package creational.abstractfactory;
public class Parts {
public String configuration;
public Parts(String configuration) {
this.configuration = configuration;
}
public String getConfiguration() {
return configuration;
}
package creational.abstractfactory;
public class PC extends Computer {

August 2007 | Java Jazz Up |26
 
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            Download PDF