【问题标题】:Is the coding style following good practice [closed]编码风格是否遵循良好实践[关闭]
【发布时间】:2015-11-03 09:23:54
【问题描述】:

我有一个实用程序类,我在其中保存了对象和其他对象,java 类调用了这个对象。所有的变量和方法在这里都是静态的。我想知道这是否是一个好习惯。我的应用程序是否存在任何安全威胁。

public class ObjectHolderUtil {
/**
* Object of the main stage of the application
*/
public static Stage mainStage;
public static HibernateSession hibernateSession;
public static String dashboard="/fxml/Dashboard.fxml"; //dashboard fxml file
public static String mainScreen="/fxml/MainScreen.fxml";
public static String addBill="/fxml/AddBill.fxml";
public static StackPane mainStackPane;
public static User user;

public static Helper helper;

  public static String getMainScreen() {
    return mainScreen;
  }

  public static void setMainScreen(String mainScreen) {
    ObjectHolderUtil.mainScreen = mainScreen;
  }

  public static Helper getHelper() {
    return helper;
  }

  public static void setHelper(Helper helper) {
    ObjectHolderUtil.helper = helper;
  }

  public static User getUser() {
    return user;
  }

  public static void setUser(User user) {
    ObjectHolderUtil.user = user;
  }

 public static StackPane getMainStackPane() {
    return mainStackPane;
  }

  public static void setMainStackPane(StackPane mainStackPane) {
    ObjectHolderUtil.mainStackPane = mainStackPane;

  }

  public static Stage getMainStage() {
    return mainStage;
  }

  public static void setMainStage(Stage mainStage) {
    ObjectHolderUtil.mainStage = mainStage;
  }

  public static String getDashboard() {
    return dashboard;
  }

  public static void setDashboard(String dashboard) {
    ObjectHolderUtil.dashboard = dashboard;
  }

  public static HibernateSession getHibernateSession() {
    return hibernateSession;
  }

  public static void setHibernateSession(HibernateSession hibernateSession) {
    ObjectHolderUtil.hibernateSession = hibernateSession;
  }
}

我会把这个类的对象称为 ObjectHolderUtil.setMainStackPane(mainStackPane);

【问题讨论】:

  • 考虑使用单例,还是使用带有单个实例化的普通类?
  • 没有必要让一切都是静态的。也许这个类应该是一个单例。也不要将您的类变量公开。它们应该是私有的。
  • @Bathsheba - 如果这只是一个实用程序类,那么将构造函数设为私有就足够了,对吧?
  • 这个问题在 CodeReview 上可能更合适。
  • @VinodMadyalkar 是的,我同意,将一个类命名为“Helper”太抽象了。

标签: java oop design-patterns


【解决方案1】:

如果可能的话,尽量避免任何静态的事情。在 OO 中,我们倾向于将对象用于所有事情,并且只有在绝对必要或有充分理由的情况下才做静态的事情。

如果您有一些方法可以解决不属于任何类的简单任务(很少出现这种情况),则使用 util 类。不是你想要的。

我删除了您的一些变量和方法以使代码更加出色...

变体 1 - 构造函数

您可以多次创建此对象并拥有多个实例。但是你也可以只创建一个 - 这很好。 有点像你可以多次打开 Windows 资源管理器

信息持有者

public class BillingServrice {

  private Stage mainStage;
  private StackPane mainStackPane;
  private User user;
  private HibernateSession hibernateSession;

  public BillingServrice() {
    // create all the other objects here
    // pass this as parameter
    mainStage = new Stage(this);
    mainStackPane= new StackPane (this);
  }

  // ... all the getters and setters
}

对象

public class Stage {

  private BillingServrice billingServrice;

  public Stage (BillingServrice billingServrice) {
    this.billingServrice = billingServrice;

  }

  void doSomething() {
     System.out.println( billingServrice.getUser() );
  }

}

变种 2 - 单例

只有一个,没有办法多次创建这个。如果您不完全理解这种模式,这在某些情况下可能会导致问题。

信息持有者

public class BillingServrice {

  private static BillingServrice billingServrice;

  public static BillingServrice getInstance() {

    if(billingServrice == null) billingServrice = new BillingServrice();
    return billingServrice;

  }

  private Stage mainStage;
  private StackPane mainStackPane;
  private User user;
  private HibernateSession hibernateSession;

  private BillingServrice() {
    // You need to define the constuctor
  }


  // ... all the getters and setters


}

对象

public class Stage {

  private BillingServrice billingServrice;

  public Stage () {
    this.billingServrice = BillingServrice.getInstance();

  }

  void doSomething() {
     System.out.println( billingServrice.getUser() );
  }

}

【讨论】:

  • 我已经更新了我的问题。请建议我如何通过不制作静态变量和方法来编写代码。这个类被多个类多次访问 例如: MainStageController.java 可以使用 getMainScreen();在一个地方和 setUser();在其他地方 LoginController.java 可以使用 getUser();在一个地方,在另一个地方 setMainStackPane
  • 好的,现在我已经删除了 static 字,并将它变成了一个普通的类。现在,当我想访问这个类时,我只需扩展它并访问它的方法。但是现在的问题是子类 A 访问这个父类的一个对象,而子类 B 访问父类的另一个对象。我希望所有子类都可以访问父类的一个对象。我该怎么做
  • 不,这也不好。如果孩子与父母有些相同,则应该真正使用继承-而不是您的情况。我将再次编辑我的答案,以显示您将如何访问它
  • 谢谢。我的问题解决了。这是堆栈溢出社区中有人给我的最佳答案。你真的是来解决我的问题的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多