My Factory Pattern
Here is the factory pattern I have implemented. I want to know, exactly which pattern this belongs to. I liked this. Comments and suggestions are welcome.
///////////////////////////////////////////////////////////
//
// ViewFactory.java
// Implementation of the Class ViewFactory
// Generated by Enterprise Architect
// Created on:9/28/02
// Original author: Vijendra Kotian
//
///////////////////////////////////////////////////////////
// Modification history:
//
//
///////////////////////////////////////////////////////////
public class ViewFactory implements ViewFactoryInterface {
private ViewFactoryInterface vf;
protected ViewFactory(){
}
public ViewFactory(String type){
System.out.println("ViewFactory created");
vf=createNewView(type);
}
public void finalize() throws Throwable {
super.finalize();
}
private ViewFactoryInterface createNewView(String type){
char c=(char)type.charAt(0);
switch(c){
case 'a':
case 'A':
return new AWTViewFactory();
case 'm':
case 'M':
return new MotifViewFactory();
}
return null;
}
public void openView(ViewFactoryInterface param){
vf=param;
}
public void print(){
if(vf!=null) vf.print();
else System.out.println("Vf is null!!!");
}
public static void main(String[] s){
ViewFactoryInterface vf=new ViewFactory("MOTIF");
vf.print();
ViewFactoryInterface vf1=new ViewFactory("Awt");
vf1.print();
vf1.openView(vf);
vf1.print();
ViewFactoryInterface vf2=new ViewFactory();
vf2.print();
}
}
public interface ViewFactoryInterface {
public void print();
public void openView(ViewFactoryInterface vfi);
}
import ViewFactory;
public class MotifViewFactory extends ViewFactory{
public MotifViewFactory(){
//super("Motif");
System.out.println("New Motif view factory created");
}
public void finalize() throws Throwable {
super.finalize();
}
public void print(){
System.out.println("Motif print");
}
}
import ViewFactory;
public class AWTViewFactory extends ViewFactory{
public AWTViewFactory(){
//super("AWT");
System.out.println("Awt View Factory created");
}
public void finalize() throws Throwable {
super.finalize();
}
public void print(){
System.out.println("AWT Print");
}
}
-Vijendra K

