0

I am learning Design Pattern and I have a question.

For example, here is the first design.
Shape
Shape->Red Circle
Shape->Blue Circle
Shape->Red Square
Shape->Blue Square
...

Second design
Shape
Shape->Circle
Shape->Square
Shape->Circle->Red Circle
Shape->Circle->Blue Circle
Shape->Square->Red Square
Shape->Square->BlueSquare
...

Thanks to Tony, I come up with a situation. In first design, the Shape class is abstract, and now I have a function for "Red Square" and "Blue Square" only. That means I cannot define the function in the Shape class but I need to do it one by one in "Red Square" and "Blue Square". The same situation for second design, I can put the new function in "Square" class.
Now, my question is am I correct and is there any other design I can use?

sflee
  • 1,659
  • 5
  • 32
  • 63
  • 2
    There is not really enough information to guide you. It is hard to understand the context or the purpose of the objects. IShape -> Abstract Circle/Square -> Concrete Red/Blue Circle/Square. Could be one setup, That would allow you to pass any shape around, have common implementations in your abstract model, and individual implementations on your concretes.. but without knowing the purpose and expected functionality we could come up with tons of variations. Perhaps edit the question to be more specific. – Tony Apr 29 '15 at 13:20
  • Yes I know there will be many variations, so can you just pick one situation that is good to use the first design and a situation that is good to use the second design? Since I am learning design pattern by myself recently and I read from a book that in normal situation I should avoid too many levels during designing classes but I am not sure the reason and I cannot find the paragraph again/.\ – sflee Apr 29 '15 at 13:49
  • 1
    Reducing the number of unneeded levels of abstractions is correct but it all depends on what you are trying to achieve. Check this site out http://www.dofactory.com/net/design-patterns they have good explanations and examples. Also check out the book "Head First Design Patterns" it helps understand the patterns. The most important advice I can give you about patterns is.."Don't force a pattern if it doesn't fit". Have an idea of what you are trying to achieve then you can go to the doFactory site and see if there is a pattern that will help achieve your goal. – Tony Apr 29 '15 at 13:55
  • Thank you, and I added a situation, if you don't mind, could you give me some advice? – sflee Apr 29 '15 at 14:06

2 Answers2

1

You should follow the "Bridge" pattern.

The Bridge Pattern is used to separate out the interface from its implementation. Doing this gives the flexibility so that both can vary independently.

Answer taken from : When do you use the Bridge Pattern? How is it different from Adapter pattern?

The Bridge pattern is an application of the old advice, "prefer composition over inheritance". It becomes handy when you must subclass different times in ways that are orthogonal with one another. Say you must implement a hierarchy of colored shapes. You wouldn't subclass Shape with Rectangle and Circle and then subclass Rectangle with RedRectangle, BlueRectangle and GreenRectangle and the same for Circle, would you? You would prefer to say that each Shape has a Color and to implement a hierarchy of colors, and that is the Bridge Pattern. Well, I wouldn't implement a "hierarchy of colors", but you get the idea...

Community
  • 1
  • 1
CreativeMind
  • 897
  • 6
  • 19
1

Your question is not specific enough to give a solid answer. After discussing in the comments it is clear you are actively trying to learn design patterns so it may not be clear to you the best way to ask the question. I am providing this as an answer so I have room to type.

Resources

dofactory - I use this as my reference source when looking for a pattern that matches my need.

Head First Design Patterns - I used this book to get a good introduction into design patterns and how to use them. Learning design patterns was a pivotal point for my understanding development. It shows you how to truly use interfaces and abstractions to achieve common problems in software development.

Beginner Guide to Design Patterns - Nice article to get you started with design patterns.

Javascript Design Patterns - (BONUS) This site was incredible for learning javascript design patterns. I am including to illustrate a point I make below about looking up design patterns for any language you use.

When looking at dofactory you can see there are three areas the patterns fall into; Creational, Structural, Behavioral. When looking for a pattern know what you are trying to achieve.

Few Tips

  • Don't force a pattern if it does not naturally fit the problem.
  • Don't over engineer the solution by trying to force patterns into every line of code you write, they are useful but not always needed.
  • Do use multiple patterns when necessary, it is perfectly 'Ok' to have multiple patterns playing close together, again.. if it is a natural fit.
  • Do learn a few common patterns, this small list is my own but a google search will reveal others personal list of commonly used patterns;
    • Strategy
    • [Abstract] Factory
    • Singleton
    • Command
    • Decorator
    • Observer
  • Do look up design patterns for any language you attempt to learn, it will give you a better understanding how to better utilize the language. (see bonus link above)
  • Do spread your wings after you get a decent understanding of the GoF patterns and look into the Enterprise Design Patterns.

I hope this answer helps guide you on the path to learning the design patterns. I know it does not exactly answer your question but hopefully it will help you understand a little better and enable you to return with a more specific question about design patterns and the usage for your code.

Tony
  • 3,269
  • 1
  • 27
  • 48