gpt4 book ai didi

java - 设计一个主题工厂

转载 作者:行者123 更新时间:2023-12-02 04:19:57 25 4
gpt4 key购买 nike

我的应用程序可能有多个主题,其中只包含一堆常量。我正在考虑使用工厂来生成主题并控制工厂类中不同主题的常量。这对于主题设计来说是一个很好的做法吗?

class Theme {
public static final float LINE_WIDTH = 1;
public static final awt.Color SELECTED_LINE_COLOR = Color.YELLOW;
public static final awt.Color LINE_COLOR = Color.WHITE;
// some other constant variables or const objects
}

class ThemeFactory {

public static Theme getWhiteTheme() {
whiteTheme = new Theme();
whiteTheme.LINE_WIDTH = 2;
whiteTheme.SELECTED_LINE_COLOR = Color.RED;
whiteTheme.LINE_COLOR = Color.BLACK;

return whiteTheme;
}

public static Theme getBlackTheme() {
return new Theme(); // Theme class default settings are for black theme.
}

}


class MyApplication {

Theme currentTheme;

MyApplication() {

// get theme settings from preference store
if (themeSettingInPrefStore.isWhiteTheme) {
currentTheme = ThemeFactory.getWhiteTheme();
} else if (themeSettingInPrefStore.isBlackTheme) {
currentTheme = ThemeFactory.getBlackTheme();
}

}

}

最佳答案

不,不是。

主题缺乏抽象,成员是公共(public)的和静态的。如果某个普通程序员在应用程序运行时更改了某些内容怎么办?事实上,您返回 new Theme() 是没有用的。

可扩展性也很糟糕^H^H^H^H^H 在这里处于危险之中。如果明天有人想要黑白主题怎么办?

使用 setter/getter 保护主题。消除那个 ThemeFactory 并将其转换为单例或其他东西(我知道,有些人一听到 sigleton 这个词就会发疯)。如果主题变得复杂,请使用 Builder (GoF) 从您的偏好存储中加载它。

关于java - 设计一个主题工厂,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32903300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com