简体   繁体   中英

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. 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.

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. 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.

Currently I have an interface with a single function calculate and 4 classes (one for each program) that implement this function. 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. The only thing that is different in my 4 files are the 5 values.

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. 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. 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.

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. I may be trying to over-engineer this but going with the easy solution doesn't feel right either.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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