1

I'm trying to teach an object oriented design principles course (on SOLID) at a training institute. I also want to teach the students a few OOP design patterns such as factory, singleton and one other. I know there is no 1 to 1 match between SOLID principles and OOP Design patterns, but I want to introduce the students a pattern that is sort of inclusive of all the SOLID design principles at play.

Any ideas?

I'm really trying to fit in the observer pattern but want to keep it conforming with all the SOLID principles.

Dan Pichelman
  • 13,773
  • 8
  • 42
  • 73
Tazo Man
  • 159
  • 1
  • 5
  • No. There is not any. Also, please. Don't teach them antipatterns like (plain)Factory and Singleton. Teach them Command, Visitor or Observer. – Euphoric Sep 05 '14 at 06:04
  • How is factory an anti pattern? – Tazo Man Sep 05 '14 at 06:05
  • See http://programmers.stackexchange.com/questions/253254/why-do-people-nowadays-use-factory-classes-so-often/253264#253264 – Euphoric Sep 05 '14 at 06:07
  • Well okay just because some people consider it an anti pattern doesnt mean it should not me learned and understood by most – Tazo Man Sep 05 '14 at 06:10
  • @Euphoric: "No. There is not any." so you think my answer is flawed? Please enlighten me, what did I overlook? – Doc Brown Sep 05 '14 at 06:20
  • http://meta.programmers.stackexchange.com/a/6488/40980 – gnat Sep 05 '14 at 06:54
  • 4
    Gnat this doesnt really have anything to do with career or education. Its a question on OOP patterns. Just because there is the term teaching in there doesnt make it fall in that list. That was just background info. – Tazo Man Sep 05 '14 at 07:04
  • 4
    @gnat: IMHO we should not close question as "asking for educational advice" just because the OP tells us the motivation for his (on-topic, programming) question is for educational purposes. – Doc Brown Sep 05 '14 at 07:56
  • @DocBrown upon re-checking the question, your reasoning seems to make sense (retracted close vote) – gnat Sep 05 '14 at 08:26

1 Answers1

15

One of the most popular design pattern among the community here is the strategy pattern. And yes, if you build some example code around this pattern, you can demonstrate all the SOLID principles:

  • S = is fulfilled when each strategy subclass is only responsible for exactly one task, and the "context" class does not take responsibilities which belong into the strategy classes
  • O = you can add new strategies afterwards ("open for extentions") without the need to change the internals of the context ("closed for modifications")
  • L = this means to implement the strategy subclasses correctly with the semantics defined by the strategy interface, not changing the semantics in any of the subclasses. LSP would not be fulfilled when a subclass implements the strategy interface in a manner which breaks the parts of the context using the strategy.
  • I = is fulfilled when the strategy base class offers only a small, single-purpose interface (like the one "execute" method)
  • D = the context relies completely on the abstract strategy interface, it gets the concrete strategy "injected" from outside (for example, at construction time) and does not make any assumptions about, for example, a fixed set of subclasses, or the availability of specific subclasses.

Note that the pattern itself does not guarantee your code to be SOLID, it is more that the SOLID principles help you to implement the pattern correctly. You may consider to show your students not only correct examples for applying the SOLID principles, but also counter-examples in context of a strategy pattern, showing code which breaks each of the five principles. Or even better: make this an exercise for them.

Doc Brown
  • 199,015
  • 33
  • 367
  • 565