论坛首页 Java版 OO

遇到了一个抽象的问题 (Factory Pattern ??)

浏览 537 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
最后更新时间:2008-06-27 关键字: factory pattern
编俄罗斯方块的时候遇到的问题。
方块有很多种如: Left Dog, Right Dog, Stick, Right L, Left L等等,并且每个方块都有一个旋转方法。
我定义了一个接口Piece,所有的方块都继承这个接口:
public interface Piece {   

    public Piece rotate();//Returns a piece representing    
                          //the next rotation of this piece.    
 
}


其中rotate()方法会返回一个旋转的方块。
但是问题来了:
//Represents the piece in the shape of left dog   
public class LeftDog implements Piece {   
  
    private int index;   
    private static LeftDog leftDogs[] = new LeftDog[2];   
  
    static {   
        //initializes leftDogs[0]
        LeftDog leftDog_0 = new LeftDog(0);
        leftDogs[0] = leftDog_0;   
        ......   

        //initializes leftDogs[1]  
        LeftDog leftDog_1 = new LeftDog(1);
        leftDogs[1] = leftDog_1;   
        ......      
        ......   
    }   
  
    private LeftDog(int index) {
        this.index = index;
    };   
  
    public Piece rotate() {   
        int nextRotation = index + 1; // This line of code also has to be copied to other type of    
                 // pieces. Which will lead to a "boilerplate code".   
        return leftDogs[nextRotation % leftDogs.length];    
    }   
  
}  



我想把上面那行 " int nextRotation = index + 1; " 给抽象出去,有什么办法吗?
   
最后更新时间:2008-06-27
不要犹豫,该抽象类的时候就用抽象类
   
0 请登录后投票
最后更新时间:2008-06-28
我觉得你的想法有问题啊,你的工厂方法会导致只有两个Lef,真的只有两个吗,不是吧。而且我认为一个LeftDog的横着还是竖着应该是他的状态,而不是两个"LeftDog"。我会定义这么几个接口。
一个接口是有横竖不同的(比如木棍),还有是不分横竖的(比如方块)
然后我会定义几个形态,比如dog,stick,L等,他们实现了上面那接口
而且我觉得不要用你那种单例工厂(多个单例),因为每个dog有他自己的位置和生存期,而不是如你表示的LeftDog只有分两个实例
   
0 请登录后投票
最后更新时间:2008-06-28
实在是不好意思啊,代码打错啦。实在太对不起各位了。。。。。。。。。。。那个LeftDog类应该是这样的 :

//Represents the piece in the shape of left dog   
public class LeftDog implements Piece {   
  
    private int index;   
    private static LeftDog leftDogs[] = new LeftDog[2];   
  
    static {   
        //initializes leftDogs[0]
        LeftDog leftDog_0 = new LeftDog(0);
        leftDogs[0] = leftDog_0;   
        ......   

        //initializes leftDogs[1]  
        LeftDog leftDog_1 = new LeftDog(1);
        leftDogs[1] = leftDog_1;   
        ......      
        ......   
    }   
  
    private LeftDog(int index) {
        this.index = index;
    };   
  
    public Piece rotate() {   
        int nextRotation = index + 1; // This line of code also has to be copied to other type of    
                 // pieces. Which will lead to a "boilerplate code".   
        return leftDogs[nextRotation % leftDogs.length];    
    }   
  
}  


我觉得一个LeftDog只有两种旋转出来的形状:

XX
--XX



--X
XX
X

所以我想着把LeftDog做成一个工厂,一调用rotate()就返回其中一个形状。Board类里用到Piece的时候只关心当前的Piece是什么形状的,然后在Board.placePiece()被调用的时候会根据当前的Piece的形状来改变Board的状态。其实LeftDog里的index就是记录当前的LeftDog是横着还是竖着的了。
   
0 请登录后投票
最后更新时间:2008-06-29
你的意思我明白,可我的意思你不明白,人只有黄皮肤,白皮肤,棕皮肤和黑皮肤,难道说只有四个人吗?人有饥饿和不饥饿的时候,难道说只有两个人吗?你的工厂永远都是返回两个LeftDog对象中的一个,难道的游戏中同时只有一个横的LeftDog和竖的LeftDog
   
0 请登录后投票
最后更新时间:2008-06-30
是的.
我确实是想让游戏中只有一个竖的LeftDog,一个横的LeftDog. 因为如果把rotate看成是一个工厂的方法,那么LeftDog就是immutable的了,所以我觉得没有必要每次调用rotate的时候都new一个LeftDog.
   
0 请登录后投票
最后更新时间:2008-06-30
首先我觉得不符合工厂模式的场景,这只是方块状态的改变,而不是制造一个新的实例。
我觉得使用抽象类表达方块的实体,接口来表达行为比较合适。
   
0 请登录后投票
最后更新时间:2008-06-30
应该有一个类叫Direction,然后它有一个方法叫rotate嘛.一个方块包含一个属性叫Direction.
   
0 请登录后投票
论坛首页 Java版 OO

跳转论坛:
JavaEye推荐