简体   繁体   English

PHP中的接口抽象类? 尊重SRP

[英]Abstract Class of Interface in PHP? Respecting SRP

I have a need to design what, in my head, seems to be a simple system that performs some calculations on data passed from a webform where data is entered by a user. 我需要设计一个似乎很简单的系统,该系统可以对从Webform(用户输入数据)传递的数据进行一些计算。 The webform itself is throwaway but the back end code should go on to be used in a new project I will start soon that handles these calculations on data that it goes out and finds by itself. Webform本身是可丢弃的,但后端代码应继续用于新项目中,我将很快开始,该项目将处理并根据自己发现的数据进行这些计算。

I originally thought that the Strategy pattern was a good fit for these back-end classes but as I started writing them Friday it really hit me that I have probably 95% duplication in my code between my 4 classes. 我最初以为策略模式非常适合这些后端课程,但是当我星期五开始编写它们时,我的确感到很惊讶,我的4个课程之间的代码可能有95%的重复。 While I feel like my current solution would work well if it was ever decided that the formulas or how the data interacts were to change, right now that code duplication makes me uneasy. 虽然我觉得如果决定改变公式或数据交互方式,我当前的解决方案会很好用,但是现在,代码重复使我感到不安。

The basics are like this: 基本内容如下:

We have 4 sales programs and each of these programs has 5 properties that vary between each program but the formula to calculate what I need to know is all the same. 我们有4个销售程序,每个程序都有5个属性,每个程序之间都有不同的属性,但是计算所需知道的公式是相同的。

Currently I have an interface with a single function calculate and 4 classes (one for each program) that implement this function. 目前,我有一个接口,其中包含一个单独的函数calculate和四个实现该功能的类(每个程序一个)。 That was all well and good until I quickly realized that calculate and the other functions I implemented in my classes are all 100% to the letter the same. 一切都很好,直到我很快意识到,我的课程中实现的calculate和其他功能都完全相同。 The only thing that is different in my 4 files are the 5 values. 我的4个文件中唯一不同的是5个值。

My original design plan was to build so that I could key from a dropdown on the webform and to pass in a class that had my values in it and perform the calculation. 我最初的设计计划是进行构建,以便可以从Web窗体的下拉菜单中键入内容,并传递一个包含我的值的类并执行计算。 Since I've stepped back from that I see that I've missed that mark and need to re-evaluate my structure. 自从我退后一步,我发现我错过了那个标记,需要重新评估我的结构。

I've done some research on Abstract classes and even the Decorator pattern but nothing really seems to be clicking right away. 我已经对Abstract类甚至Decorator模式进行了一些研究,但似乎并没有立即单击。 I like the idea of building it so that in the future other sales programs could be easily brought into it but I don't like requiring someone to basically go into my files and copy the file and adjust the variables at the top, that seems very sloppy. 我喜欢构建它的想法,以便将来可以轻松引入其他销售程序,但是我不喜欢要求某人基本上进入我的文件并复制该文件并在顶部调整变量,这看起来非常lop

The easy solution seems to be just to write a single Calculator class and pass in an array of these variables based on what the user selected. 简单的解决方案似乎只是编写一个单个的Calculator类,并根据用户选择的内容传入这些变量的数组。 I may be trying to over-engineer this but going with the easy solution doesn't feel right either. 我可能正试图对此进行过度设计,但是采用简单的解决方案也感觉不合适。

似乎您可能正在寻找策略模式;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM