I have taken a sample example to test the AbstractFactory pattern. The following is the code I picked up from http://ift.tt/1Mo03kX
public class FactoryFmProto
{
static class Expression
{
protected String str;
public Expression(String s)
{
str = s;
}
public Expression cloan()
{
return null;
}
public String toString()
{
return str;
}
}
static abstract class Factory
{
protected Expression prototype = null;
public Expression makePhrase()
{
return prototype.cloan();
}
public abstract Expression makeCompromise();
public abstract Expression makeGrade();
}
static class PCFactory extends Factory
{
public PCFactory()
{
prototype = new PCPhrase();
}
public Expression makeCompromise()
{
return new Expression("\"do it your way, any way, or no way\"");
}
public Expression makeGrade()
{
return new Expression("\"you pass, self-esteem intact\"");
}
}
static class NotPCFactory extends Factory
{
public NotPCFactory()
{
prototype = new NotPCPhrase();
}
public Expression makeCompromise()
{
return new Expression("\"my way, or the highway\"");
}
public Expression makeGrade()
{
return new Expression("\"take test, deal with the results\"");
}
}
public static void main(String[] args)
{
Factory factory;
if (args.length > 0)
factory = new PCFactory();
else
factory = new NotPCFactory();
for (int i = 0; i < 3; i++)
System.out.print(factory.makePhrase() + " ");
System.out.println();
System.out.println(factory.makeCompromise());
System.out.println(factory.makeGrade());
}
static class PCPhrase extends Expression
{
static String[] list =
{
"\"animal companion\"", "\"vertically challenged\"",
"\"factually inaccurate\"", "\"chronologically gifted\""
};
private static int next = 0;
public PCPhrase()
{
super(list[next]);
next = (next + 1) % list.length;
}
public Expression cloan()
{
return new PCPhrase();
}
}
static class NotPCPhrase extends Expression
{
private static String[] list =
{
"\"pet\"", "\"short\"", "\"lie\"", "\"old\""
};
private static int next = 0;
public NotPCPhrase()
{
super(list[next]);
next = (next + 1) % list.length;
}
public Expression cloan()
{
return new NotPCPhrase();
}
}
}
The output ignores the first item in the "list" variable due to the double initialization of the respective classes. Also, there seems quite a few things wrong about this example. How can correct this particular code and pertain it to the pattern standard?
I tried to do something of this sort:
public class TestAbstractFactory {
static abstract class Expression {
protected String[] str;
public Expression(String[] list) {
str = list;
}
public abstract Expression cloan();
public String toString() {
StringBuilder sb = new StringBuilder();
for (String s : str) {
sb.append(s);
}
return sb.toString();
}
}
static abstract class Factory {
protected Expression prototype = null;
public Expression makePhrase() {
return prototype.cloan();
}
public abstract Expression makeCompromise();
public abstract Expression makeGrade();
}
static class PCFactory extends Factory {
public PCFactory() {
String[] list = { "\"animal companion\"", "\"vertically challenged\"", "\"factually inaccurate\"",
"\"chronologically gifted\"" };
prototype = new PCPhrase(list);
}
public Expression makeCompromise() {
final String[] str = { "\"do it your way, any way, or no way\"" };
return new Expression(str);
}
public Expression makeGrade() {
final String[] str = { "\"you pass, self-esteem intact\"" };
return new Expression(str);
}
}
static class NotPCFactory extends Factory {
public NotPCFactory() {
String[] list = { "\"pet\"", "\"short\"", "\"lie\"", "\"old\"" };
prototype = new NotPCPhrase(list);
}
public Expression makeCompromise() {
final String[] str = { "\"my way, or the highway\"" };
return new Expression(str);
}
public Expression makeGrade() {
final String[] str = { "\"take test, deal with the results\"" };
return new Expression(str);
}
}
public static void main(String[] args) {
Factory factory;
if (args.length > 0)
factory = new PCFactory();
else
factory = new NotPCFactory();
System.out.print(factory.makePhrase() + " ");
System.out.println();
System.out.println(factory.makeCompromise());
System.out.println(factory.makeGrade());
}
static class PCPhrase extends Expression {
public PCPhrase(String[] list) {
super(list);
}
public Expression cloan() {
return new PCPhrase(str);
}
}
static class NotPCPhrase extends Expression {
public NotPCPhrase(String[] list) {
super(list);
}
public Expression cloan() {
return new NotPCPhrase(str);
}
}
}
It still doesn't work as it says TestAbstractFactory.Expression cannot be initialized.
Aucun commentaire:
Enregistrer un commentaire