
开发一个支持多种主题风格的桌面应用程序,例如浅色主题和深色主题。每种主题风格都有其特定的按钮(Button)、窗口(Window)和滑块(Slider)等界面组件。我们希望应用程序能够根据用户选择的主题动态加载相应的界面组件。
肖哥弹架构 跟大家“弹弹” 设计模式,需要代码关注
欢迎 点赞,点赞,点赞。
关注公号Solomon肖哥弹架构获取更多精彩内容
2、为什么要使用抽象工厂模式
抽象工厂模式允许我们创建一系列相关或依赖的对象族,而不需要指定它们具体的类。这样,当添加新的主题风格时,我们只需增加新的具体工厂和组件类,无需修改现有代码。
4. 业务抽象工厂模式图

4. 业务抽象工厂模式图

5. 业务代码参考
浅色主题工厂
interface GUIComponent {
void paint();
}
class LightButton implements GUIComponent {
public void paint() {
System.out.println("Painting Light Theme Button");
}
}
class LightWindow implements GUIComponent {
public void paint() {
System.out.println("Painting Light Theme Window");
}
}
class LightSlider implements GUIComponent {
public void paint() {
System.out.println("Painting Light Theme Slider");
}
}
class LightThemeFactory implements ThemeFactory {
public GUIComponent createButton() {
return new LightButton();
}
public GUIComponent createWindow() {
return new LightWindow();
}
public GUIComponent createSlider() {
return new LightSlider();
}
}
深色主题工厂
class DarkButton implements GUIComponent {
public void paint() {
System.out.println("Painting Dark Theme Button");
}
}
class DarkWindow implements GUIComponent {
public void paint() {
System.out.println("Painting Dark Theme Window");
}
}
class DarkSlider implements GUIComponent {
public void paint() {
System.out.println("Painting Dark Theme Slider");
}
}
class DarkThemeFactory implements ThemeFactory {
public GUIComponent createButton() {
return new DarkButton();
}
public GUIComponent createWindow() {
return new DarkWindow();
}
public GUIComponent createSlider() {
return new DarkSlider();
}
}
应用程序使用抽象工厂
interface ThemeFactory {
GUIComponent createButton();
GUIComponent createWindow();
GUIComponent createSlider();
}
public class DesktopApplication {
private GUIComponent button, window, slider;
public DesktopApplication(ThemeFactory themeFactory) {
this.button = themeFactory.createButton();
this.window = themeFactory.createWindow();
this.slider = themeFactory.createSlider();
}
public void render() {
button.paint();
window.paint();
slider.paint();
}
public static void main(String[] args) {
ThemeFactory lightThemeFactory = new LightThemeFactory();
new DesktopApplication(lightThemeFactory).render();
ThemeFactory darkThemeFactory = new DarkThemeFactory();
new DesktopApplication(darkThemeFactory).render();
}
}
6. 使用抽象工厂的好处
抽象工厂模式解决了以下问题并带来了以下好处:
- 解耦:客户端不需要知道具体的组件类,只需要知道组件的接口。
- 扩展性:添加新的主题风格时,只需增加相应的具体工厂和组件类。
- 一致性:确保应用程序使用相同主题下的所有组件。
7. 其他使用抽象工厂场景参考
抽象工厂模式适用于以下业务场景:
- 多产品系列:如电子产品的不同型号。
- 不同操作系统的GUI组件:如Windows、macOS、Linux下的界面组件。
- 开源Bean创建框架:如BeanFactory 与 ApplicationContext。
8. 可参考开源框架
- Java Swing:提供了不同外观的组件集合。
- Spring Bean:ApplicationContext 提供了Bean创建。
总结
抽象工厂模式是一种强大的创建型模式,它允许系统在不修改现有代码的情况下引入新的产品族。通过将创建逻辑封装在不同的工厂类中,抽象工厂模式提高了系统的灵活性和可扩展性。
历史热点文章
- 外观模式(Facade Pattern):微服务架构中的数据库访问实战案例分析
- 代理模式(Proxy Pattern):权限校验API调用实战案例分析
- 桥接模式(Bridge Pattern):多样式用户界面组件实战案例分析
- 组合模式(Composite Pattern): 在线教育平台课程管理实战案例分析
- 享元模式(Flyweight Pattern):网页游戏中的角色对象管理实战案例分析
- 观察者模式(Observer Pattern):股票交易系统实战案例分析
- 策略模式(Strategy Pattern):电商平台的优惠券系统实战案例分析
- 模板方法模式(Template Method Pattern):视频播放应用实战案例分析
- 命令模式(Command Pattern):网络爬虫任务队列实战案例分析
- 迭代器模式(Iterator Pattern):电商平台商品分类浏览实战案例分析
- 中介者模式(Mediator Pattern):即时通讯软件实战案例分析
- 备忘录模式(Memento Pattern):游戏存档系统实战案例分析
- 状态模式(State Pattern):电商平台订单状态管理实战案例分析
- 责任链模式(Chain of Responsibility Pattern):电商平台的订单审批流程实战案例分析
- 访问者模式(Visitor Pattern):电商平台商品访问统计实战案例分析
- 工厂方法模式(Factory Method Pattern): 电商多种支付实战案例分析
- 抽象工厂模式(Abstract Factory Pattern):多风格桌面应用实战案例分析
- 建造者模式(Builder Pattern): 在线订单系统实战案例分析
- 原型模式(Prototype Pattern): 云服务环境配置实战案例分析
- 适配器模式(Adapter Pattern):第三方支付集成实战案例分析
- 装饰器模式(Decorator Pattern):电商平台商品价格策略实战案例分析
- 单例模式(Singleton Pattern):购物车实战案例分析
网友评论