Skip to main content
20 events
when toggle format what by license comment
Jan 10, 2016 at 13:01 comment added Ross Patterson There's a reason this answer is getting 10 times as many votes as the accepted answer: Sometimes the "quick and dirty" solution is the right one, and the pedantic solution is ridiculous.
Jan 4, 2016 at 0:11 comment added Ixrec I should not be getting this many upvotes for writing two if statements and a return.
Jan 3, 2016 at 12:04 comment added Pete TNT @hvd Another similar principle would be KISS, which fits this case nicely. If one wants to name the pattern just for the sake of naming it something just call it "Anti-Arrowhead pattern" and be done with it.
Jan 2, 2016 at 19:35 comment added Dev This style can be adapted easily in to a Chain of Responsibility pattern. The pattern helps avoid "combinatorial explosion". An excellent example of CoR (which uses encryption and compression as possible handlers) is the implementation of ChannelPipeline in the Netty project.
Jan 2, 2016 at 15:32 comment added hvd "I'm not aware of any "design pattern" or "idiom" that this could be considered an example of." -- Not a pattern or idiom, really, but it seems like a straightforward application of the DRY principe to me, although at a much smaller scale than usual. The logic of mapping the compressionEnable flag to the compress function was originally in two places, it's in one now. Ditto for encryption.
Jan 2, 2016 at 7:04 comment added Andy @JonBentley Sadly, those are my own words, the way how I reworded the questions to meet the standards of this site instead of being a cry fo help. I will take the blame for that. The original question said: I'm thinking about the Decorator pattern.Please help me out find good design pattern to handle this.
Jan 2, 2016 at 1:29 comment added Jon Bentley @DavidPacker Actually if you read the question more closely, it doesn't insist on a pattern. It states, "I'm thinking about the Decorator pattern. Is it the right choice, or is there perhaps a better alternative?". You addressed the first sentence in my quote, but not the second. Other people took the approach that no, it is not the right choice. You can't then claim that only yours answers the question.
Jan 1, 2016 at 23:24 comment added user113093 This is definitely the clean way to do this.
Jan 1, 2016 at 19:27 comment added Andy @whatsisname You completely avoided the answer.
Jan 1, 2016 at 19:18 comment added whatsisname @DavidPacker: If I was in the OP's position, and asked that question, Lightness Race in Orbit's comment is what I really need. "Finding a solution using design patterns" is already starting from the wrong foot.
Jan 1, 2016 at 19:09 comment added Andy @whatsisname If you were in OP's position and wanted to find a solution using design patterns and recieved the one of Ixrec's noting that: I'm not aware of any "design pattern" or "idiom" that this could be considered an example of, even though it is a clear example of the decorator/adapter collaboration, which can be implemented in 5 minutes (that is not really a long time), would you be happy with the provided short example instead of the one you seeked?
Jan 1, 2016 at 18:56 comment added whatsisname @DavidPacker: correct is not defined by how well the code adheres to some guideline by some author about some programming ideology. Correct is "does the code do what it's supposed to do and was it implemented in a reasonable amount of time". If it makes sense to do it the "wrong way", then the wrong way is the right way because time is money.
Jan 1, 2016 at 16:05 comment added Andy I would not bother if OP had not asked for a design pattern to help him solve the problem. On top of that, with the decorator he suggested he was already on the right track, interested in the right OO approach, and you showed him the wrong way - with flags and ifs... That saddens me. Just because it makes more sense to do it the wrong way now does not mean you should not try to do it correctly.
Jan 1, 2016 at 15:55 comment added Ixrec @DavidPacker Technically, so does every if statement in every programming language. I went for simplicity, since this looked like a problem where a very simple answer was appropriate. Your solution is valid too, but personally I'd save it for when I have a lot more than two boolean flags to worry about.
Jan 1, 2016 at 15:46 comment added Andy Your solution violates the Open/Closed principle, Ixrec.
Jan 1, 2016 at 15:44 vote accept Damith Ganegoda
Jan 1, 2016 at 16:43
Jan 1, 2016 at 15:44 comment added Damith Ganegoda @lxrec Yeah I got you'r point
Jan 1, 2016 at 15:37 comment added Ixrec @DamithGanegoda Nope, if you read my code carefully you'll see it does exactly the same thing in that case. That's why there's no else between my two if statements, and why I'm assigning to data each time. If both flags are true, then compress() gets executed, then encrypt() gets executed on the result of compress(), just like you want.
Jan 1, 2016 at 15:33 comment added Damith Ganegoda I think you missed my additional condition 'encrypt(compress(data));' I want to data compress and encrypt too.
Jan 1, 2016 at 14:45 history answered Ixrec CC BY-SA 3.0