成本计算器应用程序设计模式?

编程入门 行业动态 更新时间:2024-10-23 07:33:42
本文介绍了成本计算器应用程序设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有我试图得到帮助之前的一个问题,但我不能够解决这个问题的话,所以我想现在简化问题,看看我是否能得到一些更具体的帮助这是因为它是推动我疯了...

基本上,我有这个应用程序,这是一个项目成本计算器的工作(更复杂的)版本。但是因为我在同一时间努力学习,以更好地设计我的应用程序,我想我如何能改善这种设计一些输入。基本上我想最主要的是在条件语句是(在这里)出现在两个地方重复输入。我得到的建议之前是使用策略模式或工厂模式。我也知道关于Martin Fowler的书的建议,有条件的重构与多态。我理解他的简单的例子这一原则。但是,我该怎么做任何这些东西在这里(如果有的话将是合适的)?我看到它的方式,计算依赖于两个条件:1。什么样的服务呢,写或分析? 2.项目是否小型,中型或大型? (请注意,有可能是其他参数,以及,同样不同,比如是产品的新的或以前存在的?所以,这样的参数应该是可以添加的,但我尽量保持例子简单,只有两个参数是能够得到具体的帮助)

因此重构与多态性意味着创造了多项子类,我已经为第一个条件(服务类型),我就真正创造的第二个条件,以及(大小)更多子类?什么会成为那个,AnalysisSmall,AnalysisMedium,AnalysisLarge,WritingSmall等.. ???不,我知道这是不好的,我只是不明白如何使用这个模式反正别的工作?

我基本上看到了同样的问题,使用的建议策略模式(和工厂模式,我看到就只是一个辅助,以实现上述多态性)。所以,请如果任何人有具体建议,以如何设计这些类的最佳方式,我将非常感激!也请考虑我是否选择了正确的对象太多,或者他们需要重新设计。 (如你应该考虑工厂模式显然不会是有益的回应...我已经去过这条道路,我在如何精确在这种情况下难倒)

问候,

安德斯

中的代码(非常简单的,不介意的事实,我使用的,而不是枚举字符串,而不是使用数据等一个配置文件,一旦我得到这些设计问题的窍门,将做必要在实际应用中):

公共抽象类服务 {保护字典<字符串,整数> _小时; 保护const int的小= 2; 保护const int的MEDIUM = 8; 公众诠释NumberOfProducts {搞定;组; } 公共抽象INT调用getHours(); } 公共类写作:服务 {公共写作(INT numberOfProducts) { NumberOfProducts = numberOfProducts; _hours =新词典<字符串,整数> {{小,125},{中等,100},{大,60}}; } 公共覆盖INT调用getHours() {如果(NumberOfProducts< =小)返回_hours [小] * NumberOfProducts; 如果(NumberOfProducts< = MEDIUM)回报率(_hours [小] *小)+(_hours [中等] *(NumberOfProducts - 小)); 回报率(_hours [小] *小)+(_hours [中等] *(中 - 小)) +(_hours [大] *(NumberOfProducts - MEDIUM)); } } 公共类分析:服务 {公共分析(INT numberOfProducts) { NumberOfProducts = numberOfProducts ; _hours =新词典<字符串,整数> {{小,56},{中等,104},{大,200}}; } 公共覆盖INT调用getHours() {如果(NumberOfProducts< =小)返回_hours [小]; 如果(NumberOfProducts< = MEDIUM)返回_hours [中等]; 返回_hours [大]; } } 公共部分Form1类:表格 {公共Form1中() {的InitializeComponent(); 名单,LT; INT>数量=新的List< INT>(); 的for(int i = 0; I< 100;我++) { quantities.Add(I) } comboBoxNumberOfProducts.DataSource =数量; } 私人无效comboBoxNumberOfProducts_SelectedIndexChanged(对象发件人,EventArgs五) {服务的写作=新的写作((INT)comboBoxNumberOfProducts.SelectedItem); 服务分析=新的分析((INT)comboBoxNumberOfProducts.SelectedItem); labelWriterHours.Text = writing.GetHours()的ToString(); labelAnalysisHours.Text = analysis.GetHours()的ToString()。 } }

解决方案

在你的计算有服务类型,服务规模和产品数量之间的紧耦合,它很难,因为它代表,向他们送出分为模块化块应用策略模式。

如果计算系统是固定的,则似乎策略模式是不合适的。如果不是...那么,为什么不简化系统?

例如,从服务规模拉动小时基数,并应用各种折扣或。根据您的其他设置增加

公共类服务 {公共IServiceSize serviceSize {内部搞定;组; } 公共IServiceBulkRate serviceBulkRate {内部搞定;组; } 公共IServiceType的serviceType {内部搞定;组; } 公众诠释numberOfProducts {搞定;组; } ///<总结> ///初始化中的<一个新的实例;见CREF =服务/>类的默认值 ///< /总结> 公共服务() { serviceSize =新小型多用(); serviceBulkRate =新FlatBulkRate(); 的serviceType =新WritingService(); numberOfProducts = 1; } 公共小数CalculateHours() {十进制小时= serviceSize.GetBaseHours(); 小时数=小时* serviceBulkRate.GetMultiplier(numberOfProducts); 小时数=小时* serviceType.GetMultiplier(); 返回小时; } } 公共接口IServiceSize { INT GetBaseHours(); } 公共类小型多用:IServiceSize {公众诠释GetBaseHours() {返回125; } } 公共接口IServiceBulkRate {十进制GetMultiplier(INT numberOfProducts); } 公共类FlatBulkRate:IServiceBulkRate {公共小数GetMultiplier(INT numberOfProducts) {返回numberOfProducts; } } 公共类StaggeredBulkRate:IServiceBulkRate {公共小数GetMultiplier(INT numberOfProducts) {如果( numberOfProducts 2)返回numberOfProducts; ,否则如果(numberOfProducts> = 2及numberOfProducts&下; 8)返回numberOfProducts *0.85米; ,否则返回numberOfProducts *0.8米; } } 公共接口IServiceType {十进制GetMultiplier(); } 公共类WritingService:IServiceType {公共小数GetMultiplier() {返回1.15米; } }

I have a problem that I’ve tried to get help for before, but I wasn’t able to solve it then, so I’m trying to simplify the problem now to see if I can get some more concrete help with this because it is driving me crazy…

Basically, I have a working (more complex) version of this application, which is a project cost calculator. But because I am at the same time trying to learn to design my applications better, I would like some input on how I could improve this design. Basically the main thing I want is input on the conditionals that (here) appear repeated in two places. The suggestions I got before was to use the strategy pattern or factory pattern. I also know about the Martin Fowler book with the suggestion to Refactor conditional with polymorphism. I understand that principle in his simpler example. But how can I do either of these things here (if any would be suitable)? The way I see it, the calculation is dependent on a couple of conditions: 1. What kind of service is it, writing or analysis? 2. Is the project small, medium or large? (Please note that there may be other parameters as well, equally different, such as "are the products new or previously existing?" So such parameters should be possible to add, but I tried to keep the example simple with only two parameters to be able to get concrete help)

So refactoring with polymorphism would imply creating a number of subclasses, which I already have for the first condition (type of service), and should I really create more subclasses for the second condition as well (size)? What would that become, AnalysisSmall, AnalysisMedium, AnalysisLarge, WritingSmall, etc…??? No, I know that’s not good, I just don’t see how to work with that pattern anyway else?

I see the same problem basically for the suggestions of using the strategy pattern (and the factory pattern as I see it would just be a helper to achieve the polymorphism above). So please, if anyone has concrete suggestions as to how to design these classes the best way I would be really grateful! Please also consider whether I have chosen the objects correctly too, or if they need to be redesigned. (Responses like "you should consider the factory pattern" will obviously not be helpful... I've already been down that road and I'm stumped at precisely how in this case)

Regards,

Anders

The code (very simplified, don’t mind the fact that I’m using strings instead of enums, not using a config file for data etc, that will be done as necessary in the real application once I get the hang of these design problems):

public abstract class Service { protected Dictionary<string, int> _hours; protected const int SMALL = 2; protected const int MEDIUM = 8; public int NumberOfProducts { get; set; } public abstract int GetHours(); } public class Writing : Service { public Writing(int numberOfProducts) { NumberOfProducts = numberOfProducts; _hours = new Dictionary<string, int> { { "small", 125 }, { "medium", 100 }, { "large", 60 } }; } public override int GetHours() { if (NumberOfProducts <= SMALL) return _hours["small"] * NumberOfProducts; if (NumberOfProducts <= MEDIUM) return (_hours["small"] * SMALL) + (_hours["medium"] * (NumberOfProducts - SMALL)); return (_hours["small"] * SMALL) + (_hours["medium"] * (MEDIUM - SMALL)) + (_hours["large"] * (NumberOfProducts - MEDIUM)); } } public class Analysis : Service { public Analysis(int numberOfProducts) { NumberOfProducts = numberOfProducts; _hours = new Dictionary<string, int> { { "small", 56 }, { "medium", 104 }, { "large", 200 } }; } public override int GetHours() { if (NumberOfProducts <= SMALL) return _hours["small"]; if (NumberOfProducts <= MEDIUM) return _hours["medium"]; return _hours["large"]; } } public partial class Form1 : Form { public Form1() { InitializeComponent(); List<int> quantities = new List<int>(); for (int i = 0; i < 100; i++) { quantities.Add(i); } comboBoxNumberOfProducts.DataSource = quantities; } private void comboBoxNumberOfProducts_SelectedIndexChanged(object sender, EventArgs e) { Service writing = new Writing((int) comboBoxNumberOfProducts.SelectedItem); Service analysis = new Analysis((int) comboBoxNumberOfProducts.SelectedItem); labelWriterHours.Text = writing.GetHours().ToString(); labelAnalysisHours.Text = analysis.GetHours().ToString(); } }

解决方案

In your calculation there is a tight coupling between service type, service size and the number of products, it's very hard as it stands to separate them out into modular chunks to apply the strategy pattern.

If the calculation system is fixed, then it seems that the strategy pattern is not appropriate. If it isn't... Well, why not simplify the system?

For example, pull the base number of hours from the service size, and apply various discounts or increases depending on your other settings.

public class Service { public IServiceSize serviceSize { internal get; set; } public IServiceBulkRate serviceBulkRate { internal get; set; } public IServiceType serviceType { internal get; set; } public int numberOfProducts { get; set; } /// <summary> /// Initializes a new instance of the <see cref="Service"/> class with default values /// </summary> public Service() { serviceSize = new SmallSize(); serviceBulkRate = new FlatBulkRate(); serviceType = new WritingService(); numberOfProducts = 1; } public decimal CalculateHours() { decimal hours = serviceSize.GetBaseHours(); hours = hours * serviceBulkRate.GetMultiplier(numberOfProducts); hours = hours * serviceType.GetMultiplier(); return hours; } } public interface IServiceSize { int GetBaseHours(); } public class SmallSize : IServiceSize { public int GetBaseHours() { return 125; } } public interface IServiceBulkRate { decimal GetMultiplier(int numberOfProducts); } public class FlatBulkRate : IServiceBulkRate { public decimal GetMultiplier(int numberOfProducts) { return numberOfProducts; } } public class StaggeredBulkRate : IServiceBulkRate { public decimal GetMultiplier(int numberOfProducts) { if (numberOfProducts < 2) return numberOfProducts; else if (numberOfProducts >= 2 & numberOfProducts < 8) return numberOfProducts * 0.85m; else return numberOfProducts * 0.8m; } } public interface IServiceType { decimal GetMultiplier(); } public class WritingService : IServiceType { public decimal GetMultiplier() { return 1.15m; } }

更多推荐

成本计算器应用程序设计模式?

本文发布于:2023-11-22 05:35:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616163.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计算器   程序设计   成本   模式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!