2013-11-14 222 views
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(); 

顯示了一個具體的工廠的名稱。這是一個錯誤嗎?謝謝你們

回答

0

那麼,我肯定不會使用這種設計!它至少因以下原因而聞起來:

  1. ShapeFactory.getShape()使用一種開關,它不是面向對象的設計。
  2. ShapeFactory是編譯時依賴於所有的Shape對象。
  3. 沒有人不能擴展你的圖書館!試着想象一下,不是有人控制你的ShapeFactory想要添加一個新的形狀......不可能!
  4. 我看不出有什麼理由爲什麼AbstractFactory應該是一個抽象類而不是接口。在我看來,它似乎來自C++而不是Java設計。

看看java.sql.DriverManager.getConnection(connectionString)方法的實現方式。最好的方法是觀看源代碼。

非常粗略的想法總結(它隱藏在很多私人方法裏面)。這或多或少是責任鏈的實現,儘管沒有鏈接的驅動程序列表。

DriverManager管理驅動程序列表。 每個驅動程序都必須通過調用其方法registerDriver()將其自身註冊到DriverManager。 在請求連接時,getConnection(connectionString)方法會依次調用傳遞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"); 
    } 
}