I found some reading materials about factory design pattern (parallel hierarchy) whereby a class in a hierarchy has a corresponding class in the another hierarchy. So I wrote the code as below:
package com.factory3;
public abstract class Subject {
abstract SubjectPlanner createPlanner();
}
package com.factory3;
public class English extends Subject{
public SubjectPlanner createPlanner(){
return new EnglishPlanner(this);
}
}
package com.factory3;
public class Mathematic extends Subject{
public SubjectPlanner createPlanner(){
return new MathematicPlanner(this);
}
}
package com.factory3;
import java.util.Date;
public abstract class SubjectPlanner {
private Subject s;
public SubjectPlanner(Subject s){
this.s = s;
}
public abstract Date getAvailable();
}
package com.factory3;
import java.util.Date;
public class EnglishPlanner extends SubjectPlanner {
public EnglishPlanner(Subject s) {
super(s);
}
public Date getAvailable(){
return new Date();
}
}
package com.factory3;
import java.util.Date;
public class MathematicPlanner extends SubjectPlanner{
public MathematicPlanner(Subject s){
super(s);
}
public Date getAvailable(){
return new Date();
}
}
package com.factory3;
public class SubjectFactory {
public static Subject getSubject(String s){
if(s.equalsIgnoreCase("english")){
return new English();
}else{
return new Mathematic();
}
}
}
package com.factory3;
public class Test {
public static void main(String[] args){
SubjectPlanner sp = SubjectFactory.getSubject("english").createPlanner();
System.out.println(sp.getAvailable());
}
}
Can I classify the code above as parallel hierarchy? Your help is kindly appreciated.
Thank You.