0

这是我的代码

public class FactoryPatternDemo {
public static void main(String[]args)
{
    AbstractFactory shapeFactory=new ShapeFactory();

    //tramite la fabbrica di figura geometrica disegno un rettangolo..
    Shape shape1=shapeFactory.getShape("rEcTaNgLe");
    shape1.draw();

    System.out.println();

    //..e un triangolo
    Shape shape2=shapeFactory.getShape("triangle");
    shape2.draw();
}

形状工厂:

public class ShapeFactory extends AbstractFactory{

public ShapeFactory(){

}

@Override
public Shape getShape(String shapeType)
{
    if (shapeType==null)
            return null;
    if (shapeType.equalsIgnoreCase("RECTANGLE"))
            return new Rectangle();
    if (shapeType.equalsIgnoreCase("TRIANGLE"))
            return new Triangle();
    return null;
}

抽象工厂:

public abstract class AbstractFactory {
public abstract Shape getShape(String shapeType);}

抽象产品

public interface Shape {
void draw();}

混凝土产品#1

public class Rectangle implements Shape {

@Override
public void draw() {
    for(int i=0; i<5; i++)
    {
        if(i==0 || i==4)
        {
            for(int j=0; j<10; j++)
            {
                System.out.print("*");
            }
        }
        else
        {
            for(int j=0; j<10; j++)
            {
                if(j==0||j==9)
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
        }
        System.out.print("\n");
    }

}

我的问题是:这是实现抽象工厂模式的正确方法吗?客户端应该只能看到 FactoryPatternDemo 类中的抽象事物或接口,但是这行代码:

 AbstractFactory shapeFactory=new ShapeFactory();

显示混凝土工厂的名称。这是一个错误吗?多谢你们

4

1 回答 1

0

好吧,我肯定不会使用这种设计!它之所以有气味,至少有以下几个原因:

  1. ShapeFactory.getShape()使用一种开关,它不是面向对象的设计。
  2. ShapeFactory编译时取决于所有 Shape 对象。
  3. 没有人不能扩展你的图书馆!试着想象一下,一个无法控制你的人如何ShapeFactory想要添加一个新形状......不可能!
  4. 我看不出为什么AbstractFactory应该是抽象类而不是接口的任何理由。在我看来,这更像是 C++ 的遗产,而不是 Java 设计。

看看java.sql.DriverManager.getConnection(connectionString)方法是如何实现的。最好的方法是观看源代码。

这个想法的非常粗略的总结(它隐藏在很多私有方法中)。它或多或少是一种责任链的实现,尽管没有驱动程序的链表。

DriverManager管理驱动程序列表。每个驱动程序必须DriverManager通过调用其方法将自己注册到registerDriver()。在请求连接时,该getConnection(connectionString)方法会依次调用驱动程序并传递连接字符串。每个驱动程序都知道给定的连接字符串是否在其权限范围内。如果是,它会创建连接并返回它。否则,控制权将传递给下一个驱动程序。比喻:

  • 司机:你的混凝土ShapeFactories
  • 连接字符串:形状的类型
  • 连接:形状的一个实例

我会尝试这样的事情:

public class ShapeManager {
    public void registerFactory(ShapeFactory factory) {
        // store the factory to the internal list
    }
    public shape getShape(String shapeType) {
        // go through the list of registered factories until one of them returns non-null
    }
}

public interface ShapeFactory {
    /**
     * Returns an instance of a shape, if shapeType is supported
     */
    public shape getShape(String shapeType);
}

public class TriangleFactory implements ShapeFactory {
    public static final String SHAPE_TYPE = "Triangle";
    @Override 
    public shape getShape(String shapeType) {
        if (SHAPE_TYPE.equals(shapeType) {
            return new Triangle();
        }
    }
}

public class RectangleFactory implements ShapeFactory {
    public static final String SHAPE_TYPE = "Rectangle";
    @Override 
    public shape getShape(String shapeType) {
        if (SHAPE_TYPE.equals(shapeType) {
            return new Triangle();
        }
    }
}

public class Client {
    ShapeFactory factory;

    // This is performed somehow on a global level, maybe by Spring configuration or so
    // However it is *not* part of the ShapeFactory so anyone may add their own shapes,
    // apart from those provided by default by your shape library.
    public void setUp() {
       factory = new ShapeFactory();
       factory.registerFactory(new TriangleFactory());
       factory.registerFactory(new RectangleFactory());
    }

    public void use() {
        final Shape triangle = factory.getShape("Triangle");
        final Shape rectangle = factory.getShape("Rectangle");
    }
}
于 2013-11-17T17:15:13.343 回答