Wednesday, December 12, 2012

Design Patterns : Abstract Factory Pattern 1


Some years ago in an interview for an entry-to-middle level job,
I was asked by the interviewer about the Abstract Factory.
The interviewer then intentionally tried to confuse me by asking what
is a Factory and what are the differences between the Two.
I provided him the following example.


Suppose say you are the CTO of a new company and want
the computers in your new organization to be set up immediately.
Now even assuming that the CTO is immensely tech savvy you can not expect
him to buy a bulk amount of systems or build them under supervision
as per the requirement,


The first would not serve the minimum fitness-for-purpose, for the
simple reason that the Servers which would host the website of the company
the workstation on which the employees do their daily work
the laptops on which the Sales Executive provides the Client Presentations
all need to be different.

The second would serve the minimum fitness-for-purpose, but would
not serve the cost-effectiveness. Also the man days necessary to supervise would
outweigh the benefits. So the solution must meet the fitness-for-purpose
within the budget.


If the following listing encapsulates the Computer Object


package com.company.self.study.core.java;

/**
 *
 * Abstract Factory Pattern Example
 *
 * @author administrator
 *
 */
public interface Computer {

   /**
    *
    *
    *
    * @return
    *
    public Component getProcessorCore();

   /**
    *
    *
    *
    * @return
    *
    public Component getMemory();


   /**
    *
    *
    *
    * @return
    *
    public Component getHardDisk();

}


Then we can define the concrete implementation of the different kinds
of Computers which needed to be ordered based on specification, based on
the below listed Specifications.


Server


package com.company.self.study.core.java;

    /**
    *
    * Abstract Factory Pattern Example
    *
    *
    *
    * @author administrator
    *
    *
public class Server implements Computer {

    private Component hardDisk;
    private Component memory;
    private Component processorCore;

    /**
    *
    *
    *
    */
    public Server() {
    processorCore = new ProcessorCore(" DecaCore ");
    hardDisk = new HardDisk(" 100 TB ");
    memory = new Memory(" 40 GB ");
    }

    /**
    *
    *
    *
    * @param hardDisk
    * @param memory
    * @param processorCore
    */
    public Server(Component hardDisk, Component memory, Component processorCore) {
    this();
    this.hardDisk = hardDisk;
    this.memory = memory;
    this.processorCore = processorCore;
    }


    @Override
    public Component getHardDisk() {
    // TODO Auto-generated method stub
    return hardDisk;
    }

    @Override
    public Component getMemory() {
    // TODO Auto-generated method stub
    return memory;
    }

    @Override
    public Component getProcessorCore() {
    // TODO Auto-generated method stub
    return processorCore;
    }

    /**
    *
    *
    *
    */
   
@Override
    public String toString() {
    return " server with " + processorCore + " " + memory + " RAM " + hardDisk +   " physical disk ";
    }
}


WorkStation


package com.company.self.study.core.java;

/**
*
* Abstract Factory Pattern Example
*
*
* @author administrator
*
*/
public class WorkStation implements Computer {

private Component hardDisk;
private Component memory;
private Component processorCore;


/**
*
*
*
*/
public WorkStation() {
super();
processorCore = new ProcessorCore(" QuadCore ");
hardDisk = new HardDisk(" 1 TB ");
memory = new Memory(" 8 GB ");
}
/**
*
*
*
* @param hardDisk
* @param memory
* @param processorCore
*/
public WorkStation(Component hardDisk, Component memory, Component processorCore) {
super();
this.hardDisk = hardDisk;
this.memory = memory;
this.processorCore = processorCore;
}

@Override
public Component getHardDisk() {
// TODO Auto-generated method stub
return hardDisk;
}

@Override
public Component getMemory() {
// TODO Auto-generated method stub
return memory;
}

@Override
public Component getProcessorCore() {
// TODO Auto-generated method stub
return processorCore;
}
/**
*
*
*
*/
@Override
public String toString() {
return " workstation with " + processorCore + " " + memory + " RAM " + hardDisk + " physical disk ";
}

}


NoteBook


package com.company.self.study.core.java;

/**
*
* Abstract Factory Pattern Example
*
*
* @author administrator
*
*/
public class NoteBook implements Computer {

private Component hardDisk;
private Component memory;
private Component processorCore;


/**
*
*
*
*/
public NoteBook() {
super();
processorCore = new ProcessorCore(" QuadCore ");
hardDisk = new HardDisk(" 100 GB ");
memory = new Memory(" 4 GB ");
}
/**
*
*
*
* @param hardDisk
* @param memory
* @param processorCore
*/
public NoteBook(Component hardDisk, Component memory, Component processorCore) {
super();
this.hardDisk = hardDisk;
this.memory = memory;
this.processorCore = processorCore;
}

@Override
public Component getHardDisk() {
// TODO Auto-generated method stub
return hardDisk;
}

@Override
public Component getMemory() {
// TODO Auto-generated method stub
return memory;
}

@Override
public Component getProcessorCore() {
// TODO Auto-generated method stub
return processorCore;
}

/**
*
*
*
*/
@Override
public String toString() {
return " notebook with " + processorCore + " " + memory + " RAM " + hardDisk + " physical disk ";
}

}


Tablet


package com.company.self.study.core.java;


/**
*
* Abstract Factory Pattern Example
*
*
*
* @author administrator
*
*/
public class Tablet implements Computer {

private Component hardDisk;
private Component memory;
private Component processorCore;


/**
*
*
*
* @param hardDisk
* @param memory
* @param processorCore
*/
public Tablet() {
super();
processorCore = new ProcessorCore(" DualCore ");
hardDisk = new HardDisk(" 40 GB ");
memory = new Memory(" 2 GB ");
}



/**
*
*
*
* @param hardDisk
* @param memory
* @param processorCore
*/
public Tablet(Component hardDisk, Component memory, Component processorCore) { super();
this.hardDisk = hardDisk;
this.memory = memory;
this.processorCore = processorCore;
}

@Override
public Component getHardDisk() {
// TODO Auto-generated method stub
return hardDisk;
}

@Override
public Component getMemory() {
// TODO Auto-generated method stub
return memory;
}

@Override
public Component getProcessorCore() {
// TODO Auto-generated method stub
return processorCore;
}

/**
*
*
*
*/
@Override
public String toString() {
return " tablet with " + processorCore + " " + memory + " RAM " + hardDisk + " physical disk ";
}


}


No comments: