Reputation: 13
I am new in patterns and trying to figure it out. I read a couple of tutorials about the Factory method and did this: I have a parent class A with a couple of fields with getters and setters. There are also two more classes B and C (more are planned in the future) that inherit from A and have several more fields with getters and setters. All tutorials of this pattern usually have an abstract class or interface that classes B and C implement. But I use instead the usual class A, which in some cases should be used as classes B and C. In my program there is some automatic classification, using which I can understand in runtime which class I need. Is this an implementation of the Factory method pattern? And there are no problems in this code?
public class A {
private String x1 = "some value";
private String x2;
public String x1() {
return x1;
}
public void setX1(String x1) {
this.x1 = x1;
}
public String getX2() {
return x2;
}
public void setX2(String x2) {
this.x2 = x2;
}
}
public class B extends A {
private String x3;
private String x4;
public String getX3() {
return x3;
}
public void setX3(String x3) {
this.x3 = x3;
}
public String getX4() {
return x4;
}
public void setX4(String x4) {
this.x4 = x4;
}
}
public class C extends A {
private String x5;
private String x6;
public String getX5() {
return x5;
}
public void setX5(String x5) {
this.x5 = x5;
}
public String getX6() {
return x6;
}
public void setX6(String x6) {
this.x6 = x6;
}
}
public class MyClassFactory {
public static A getMyClass(String criteria)
{
switch (criteria){
case "class B":
return new B();
case "class C":
return new C();
default:
return new A();
}
}
}
Upvotes: 0
Views: 1582
Reputation: 757
From the first glance your code seems like a factory pattern implementation (wiki link). From wikipedia:
A (software) design pattern is a general solution to a common problem in software design. It is a description or template for how to solve a problem, that can be used in different situations. A design pattern typically shows relationship and interaction between classes or objects, without specifying final application classes or objects that are involved. Patterns identify and specify abstractions that are above the level of single classes and instances.
Having said that there is no right or wrong implementation for a pattern IMHO. The pattern is used to guide, guarantee and guard structure choices that the engineer will make. However sometimes developers get carried and try to fit everything they do in a pattern even if the pattern is not the best solution for their problem and usually then the code becomes messy.
Upvotes: 1
Reputation: 2668
Yes, this is a factory pattern implementation, since you abstract the logic of the objects creation away from the client and delegate this responsibility to another method that chooses which type to create based in some common criteria.
One disadvantage of your code is that subclasses B and C add some public getters and setters that won't be visible right after creation because the type of the object returned is A (if though the underlying object might be B or C indeed). You would to cast the created object to B or C.
Example:
MyClassFactory.getMyClass("class B").getX3() // you can't call getX3() since this method belongs to B, but the object returned by the factory is typed as A
Upvotes: 0