哪些设计模式可以应用于配置设置问题? [英] Which design patterns can be applied to the configuration settings problem?

查看:36
本文介绍了哪些设计模式可以应用于配置设置问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大型复杂的软件产品中,管理可配置设置成为一个主要的难题.我见过的两种解决问题的方法是:

In large and complex software products managing configurable settings becomes a major pain. Two approaches I've seen to the problem are:

  • 让系统中的每个组件从配置文件或注册表设置加载自己的配置.
  • 有一个设置加载器类,用于加载所有可配置的系统设置,并让每个组件查询设置加载器以获取其设置.

我觉得这两种方法都不对.

These approaches both feel wrong to me.

是否有任何设计模式可以用来简化问题?也许可以利用依赖注入技术.

Are there any design patterns that could be used to simplify the problem? Maybe something that would take advantage of the dependency injection technique.

推荐答案

我更喜欢创建一个用于设置查询、加载和保存的界面.通过使用依赖注入,我可以将其注入到每个需要它的组件中.

I prefer to create an interface for setting query, loading, and saving. By using dependency injection, I can inject this into each component that requires it.

这在替换配置策略方面提供了灵活性,并为所有工作提供了一个共同的基础.与单个全局设置加载器"(您的选项 2)相比,我更喜欢它,特别是因为如果我绝对需要,我可以覆盖单个组件的配置机制.

This allows flexibility in terms of replacing the configuration strategy, and gives a common base for everything to work from. I prefer this to a single, global "settings loader" (your option 2), especially since I can override the configuration mechanism for a single component if I absolutely need to do so.

这篇关于哪些设计模式可以应用于配置设置问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆