2013-05-12 53 views
2

EDIT(2013年5月16日)调用一个方法:良好的MVC设计由许多类

我会用我的第三个解决方案。谢谢大家的帮助。编辑

END(2013年5月16日)

我有更新方法的图(控制器内)。该方法必须由许多类调用。所以我想出了两个解决方案:

  1. 在每个需要调用update方法的类中为View/Controller创建一个字段。
  2. 使更新方法为静态。

什么是更好的(M)VC设计,如果两个或多个类同时调用更新方法会发生什么?

简化实施例1:

public class View{ 
    public void update(){} 
} 

// Many classes: 
public class AClass{ 
    private View view; 

    private void aMethod(){ 
     view.update(); 
    } 
} 

简化实施例2:

public class View{ 
    public static void update(){} 
} 

// Many classes: 
public class AClass{ 

    private void aMethod(){ 
     View.update(); 
    } 
} 

编辑(2013年5月12日):我同意, “静态溶液” 是不好的,尤其是为OOP。

我过于简单化豆蔻位,所以这里是实际的设计:

比方说,你有一些MVC设计的方案,并希望把他们一起在一个“大”的计划,但现在的程序必须从互动时常。

public class AView{ 
    public void update(){} 
} 

public class AModel{} 

//Many of these: 
public class AController{ 
    private AView aview; 
    private AModel amodel; 

    public AController(AView aview, AModel amodel){ 
     this.aview=aview; 
     this.amodel=amodel; 
    } 
} 

public class MainController{ 
    public MainController(){ 
     AView view1 = new AView(); 
     AModel model1 = new AModel(); 
     AController controller1 = new AController(view1, model1); 

     AView view2 = new AView(); 
     AModel model2 = new AModel(); 
     AController controller2 = new AController(view2, model2); 

     AView view3 = new AView(); 
     AModel model3 = new AModel(); 
     AController controller3 = new AController(view3, model3); 
    } 
} 

现在我们假设controller1和controller2需要调用view3的更新方法。 我目前的解决方案是在controller1和controller2中创建一个view3(如你所建议的)。

AController controller1 = new AController(view1, view3, model1); 
AController controller2 = new AController(view2, view3, model2); 

或者我应该重构整个设计还是在控制器之间创建一个“桥梁”?那么这座“桥”会是怎样的?

编辑结束(2013年5月12日)

编辑(2013年5月14日): 我想出了一个第三个解决方案: 每个控制器触发它自己的事件。例如:

Controller1和Controller2触发事件,Controller3侦听它们,然后调用View3的更新方法。 您对这个解决方案有什么看法?

编辑结束(2013年5月14日)

+1

这一切取决于你是否需要'View'的内部状态。如果你使它成为静态的,你需要使整个状态成为静态 - 这显然是代码异味。如果你不需要一个状态,那么每个类都应该有它自己的'View'实例。如果您需要共享状态,则需要在决定如何共享课程实例之前回答自己的问题“在两个或更多类同时调用更新方法时会发生什么情况”。 – 2013-05-12 00:18:02

回答

0

我会用下面的设计,因为这将是模块化(感谢@Hovercraft全部鳗鱼您指出紧耦合):

public class MainController{ 

    private Controller1 controller1; 
    private Controller2 controller2; 
    private Controller3 controller3; 

    public MainController(){ 
     controller1=new Controller1();    
     controller2=new Controller2();   
     controller3=new Controller3(); 

     addListeners(); 
    } 

    private class Ctrl1Listener implements Controller1Listener(){   
     public void controller1ActionPerformed(Controller1Event e){ 
      //Calls the method of view3  
      controller3.update(); 
     } 
    } 

    private class Ctrl2Listener implements Controller2Listener(){   
     public void controller2ActionPerformed(Controller2Event e){ 
      //Calls the method of view3  
      controller3.update(); 
     } 
    } 

    private void addListeners(){ 
     controller1.addListener(new Ctrl1Listener()); 
     controller2.addListener(new Ctrl2Listener()); 
    } 
} 

的意见是高度赞赏。

5

扔静态解掉,只是甚至不考虑这一点,因为这样做你扔掉所有OOP窗外。如果需要,可以向控件公开update()方法调用视图的方法。

说了这话之后,你的问题让我担心紧密耦合。

这些其他需要调用View更新的类 - 它们是Control类/库的一部分吗?只有控件应该在视图上调用update。


编辑,你的状态:

能否请您介绍一些关于“静态方法抛出所有OOP窗外”我渴望得知。

当您使静态方法不再是可继承的,更重要的是,它不再是类的实例的方法,而是类的对象的方法。所以单独的实例将不再有自己独特的方法来调用,它不能从实例中获取状态数据,也不能改变实例的状态。

+0

您能否详细解释一下“静态方法将所有OOP抛出窗口”我渴望了解这一点。所以请。 – 2013-05-12 01:00:19

+0

@KanagaveluSugumar:请参阅编辑以回答。 – 2013-05-12 01:03:41

+0

+1谢谢。现在我懂了。 – 2013-05-12 01:06:18

0

我希望整个模型将更新相同的视图,并保持更新各种数据。

public class View{ 
    private View(){} 
    public void update(){} 
    private static class singleTonFactory{ 
      private Static View obj = new View(); 
    } 

    public static View getViewInstance(){ 
      return singleTonFactory.obj; 
    } 
} 


// Many classes: 
public class AClass{ 
    private final View view = View.getViewInstance(); 

    private void aMethod(){ 
      view.update(); 
    } 
}