2015-11-03 107 views
-4

我有一個實用程序類,其中保存了對象和其他類,並且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);

+1

考慮使用單或正常類與單實例化,而不是? – Bathsheba

+0

沒有必要一切都是靜態的。也許這個班級應該是一個單身人士。也不要公開你的類變量。他們應該是私人的。 – Qwerky

+0

@Bathsheba - 如果這只是一個實用工具類,那麼將構造函數私有就足夠了嗎? – TheLostMind

回答

1

嘗試,如果可能,以避免任何一成不變的東西。在面向對象的情況下,我們傾向於使用對象來處理所有事情,並且只有在絕對要求或者有充分理由的情況下才能使用對象。

如果您有一些方法來解決不屬於任何類的簡單任務(很少是這種情況),則使用util類。不是你想要的。

我刪除了你的一些varriables和方法,以使代碼shoter ...

Varriante 1 - 構造

您可以創建該對象多次,並有它的多個實例。但你也可以創建一個 - 這很好。 有點像,你可以多次

信息持有者

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()); 
    } 

} 

Varriante 2打開Windows資源管理器 - 辛格爾頓

釷ere將只是其中之一,無法多次創建這個。這可能會在某些情況下導致問題,如果您沒有完全理解這種模式。

信息保持器

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()); 
    } 

} 
+0

我已更新我的問題。請教我如何通過不編寫靜態變量和方法來編寫代碼。這個單獨的類可以多次訪問多個類 例如: MainStageController.java可以使用getMainScreen();在一個地方 setUser();在其他地方 LoginController.java可以使用getUser();在一個地方和setMainStackPane在其他地方 – oldvipera

+0

好吧,現在我所做的是我已經刪除了'靜態'的單詞,並且它是一個普通的類。現在當我想訪問這個類時,我只需擴展它並訪問它的方法。但現在的問題是子類A訪問此父類的一個對象,子類B訪問父類的另一個對象。我希望父類的一個對象可以被所有的子類訪問。我該怎麼做 – oldvipera

+0

不,那也不好。如果孩子與參加者有點相同,那麼繼承應該真的被使用,而不是你的情況。我會再次編輯我的答案,以顯示您將如何訪問它 – arcs