9 Replies Latest reply: Jul 7, 2010 8:04 AM by 800387 RSS

    Re: pls help newbie - which pattern to use for this?

    843853
      I appreciate your attention to the details, saish, but i just want to keep the problem at hand as simple as possible to identify which pattern to use.

      let's say the pizza parlor has only one pizza which costs 10 dollars, and every fee is in whole dollar amounts, and the address is indeed a simple string because the pizza parlor is built in an isolated island if you will. and there are two coupon codes, one is to deduct one dollar, and five is to deduct five dollars off the total price.

      now, which pattern can I use?
        • 1. Re: pls help newbie - which pattern to use for this?
          796262
          Jim_Found wrote:
          now, which pattern can I use?
          What makes you think you need to use a pattern at all? It seems that forcing a pattern onto your design will just needlessly complicate your code, which is the opposite of what you want to be doing.
          • 2. Re: pls help newbie - which pattern to use for this?
            843853
            kevin, i know. this is an oversimplified toy situation for the sake of brevity. ultimately what I am trying to get at is what pattern should/can i use when there are many different configuration options.

            coming from a rather structural programming background, i know many things can be solved easily with a bunch of if's and case statements, and i do have a habit of doing so, but this is not real oop. so i am trying to figure out what patterns work (may not be efficiently at first) for certain situations.
            • 3. Re: pls help newbie - which pattern to use for this?
              800387
              Jim_Found wrote:
              kevin, i know. this is an oversimplified toy situation for the sake of brevity. ultimately what I am trying to get at is what pattern should/can i use when there are many different configuration options.

              coming from a rather structural programming background, i know many things can be solved easily with a bunch of if's and case statements, and i do have a habit of doing so, but this is not real oop. so i am trying to figure out what patterns work (may not be efficiently at first) for certain situations.
              Simply start with polymorphism. You have an Order interface. There is a DeliveryOrder implementation and a CarryOutOrder implementation. Perhaps some common code can be refactored out into either OrderHelper (delegation) or AbstractOrder (inheritance). If you really really want to jam a pattern onto that, use AbstractFactory.

              Don't think in patterns. Think abstraction, encapsulation, inheritance and polymorphism first. Your response earlier makes me think you are oversimplifying the problem only to make it overly complicated with patterns. Which is totally backward. Your problem will normally be complex, your solution should be simple.

              - Saish
              • 4. Re: pls help newbie - which pattern to use for this?
                843853
                saish, thanks for the suggestions. i know you are trying to get me onto the right track, but coming from a structural background, it is sometimes difficult to think in terms of abstractions etc.

                i hear people making fun of "big block of if statements" all the time, but i do that quite frequently. so i try to squeeze myself into the design patterns realm to do away from those laughable big block of if statements. but on the other hand, i think patterns are making things more complicated because all they do is adding a layer of indirection. in fact, with some IDEs, navigation of pattern codes is quite annoying because the IDE only takes the (inter)face value of the code and tends to jump into the abstract level code that is either an abstract class or an interface, so i actually have trouble understanding why many people are saying using patterns make things easier...
                • 5. Re: pls help newbie - which pattern to use for this?
                  jwenting
                  NEVER start with the question "which pattern shall we use today", or even more damning "which pattern is best" (without qualifying and quantifying the very exact conditions applying to the specific case).

                  A pattern is a tool to achieve a goal, not a goal in itself.
                  When you define your software structure, the patterns (note, multiple) that could be of help will flow freely from the design if you're at all capable. You'll likely not even consciously apply them.

                  That said, studying various patterns and design philosophies is a good thing to do, but that's independent of the application of those patterns in real world applications, which should never be forced.
                  In fact, in those cases where I've encountered applications that were forced into patterns, those applications were without exception abominations. Abysmal performance, terribly convoluted code, impossible to improve, maintain, or expand without excessively high effort (and thus cost).
                  Yet those applications were all designed and created by people with the highest degrees in professional education (including authors of books on software design, which says a lot about those books).
                  • 6. Re: pls help newbie - which pattern to use for this?
                    800387
                    Jim_Found wrote:
                    saish, thanks for the suggestions. i know you are trying to get me onto the right track, but coming from a structural background, it is sometimes difficult to think in terms of abstractions etc.

                    i hear people making fun of "big block of if statements" all the time, but i do that quite frequently. so i try to squeeze myself into the design patterns realm to do away from those laughable big block of if statements. but on the other hand, i think patterns are making things more complicated because all they do is adding a layer of indirection. in fact, with some IDEs, navigation of pattern codes is quite annoying because the IDE only takes the (inter)face value of the code and tends to jump into the abstract level code that is either an abstract class or an interface, so i actually have trouble understanding why many people are saying using patterns make things easier...
                    Patterns are, IMO, primarily for communication. I should be able to say, "we need some kind of a bridge between those two systems, or those five different tasks look like a strategy to me, or see if we can use an abstract builder and an interface to hide all those implementations." I don't expect to see someone run off and code FooStrategy and BarStrategy. It's more a useful way of thinking and communicating about problems. Most non-trivial solutions for a component use multiple patterns, mainly subconsciously. But I would never start out by trying to jam a pattern into a problem.

                    WRT to your specific concern, a number of if statements. The ideal "pattern" for that is simple polymorphism. If you really really want a pattern to go along with that, implement Strategy.

                    - Saish
                    • 7. Re: pls help newbie - which pattern to use for this?
                      843853
                      thank you both for sharing your thoughts and insights. the inconvenient truth of education is that i went through a class on design patterns which did exactly what jwenting described. the prof has a project and our tasks were to refactor pretty much every part into pattern. it is one thing to apply the handful of patterns we just learned today to some toy problems; but it is a totally different world to refactor some complex thing that has already been written.

                      but anyhow. are there any good books on applying patterns to problems? we were required to have the Go4 design patterns book, but i didn't think it was that useful. how about the refactoring to patterns book? any good?
                      • 8. Re: pls help newbie - which pattern to use for this?
                        800387
                        The following by Feathers and Fowler are both outstanding.

                        - Saish
                        • 9. Re: pls help newbie - which pattern to use for this?
                          843853
                          nice. thanks a bunch!