Forums

Example of a system using design patterns

Started by kammutierspule 4 months ago3 replieslatest reply 4 months ago68 views

Hello all,

I'm looking for an example implementation of a system using design patterns.

The best example would be something like a wash machine with button interface, a display, etc and then an approach how to apply design patterns to implement the system.

Do you know where can I find such simple examples?


[ - ]
Reply by natersozApril 18, 2020

It is unlikely that you will find a design pattern that fits a washing machine or any other fully completed device.

Design patterns are more commonly used to model interfaces and workings of small components.

I personally think that the Observer Pattern is highly illustrative of how a pattern can effectively be used in embedded systems.

First the pattern:

https://en.wikipedia.org/wiki/Observer_pattern#Str...

And here is an implementation:

https://github.com/natersoz/patterns/tree/master/o...

So where might the Observer Pattern (aka Publish/Subscribe) be used? Anywhere an asynchronous event is to be handled from the oversvable (publisher) to the observer (subscriber). These might include:

  • Interrupt triggers in general
  • Timer events
  • Input notifications from serial ports from all kinds: UART, SPI, I2C
  • Even a Washing Machine: Rinse Cycle Complete Event notification.

Why use this or any other pattern?

The point of a pattern, or any abstraction for that matter, is the reduce component coupling. The Observer and Observable agree on a standard interface. Now each component can change their implementation without breaking code in the other component.

If a pattern does not provide this reduction in code coupling then it is possible that the pattern is not being used properly.

Patterns should fit their usage intuitively. In other words they should make sense. They should improve code readability and maintainability. It should also make testing easier since the unit can be encapsulated with interfaces and removed from its embedded environment.

The potential downside to code decoupling is reduced performance (maybe).

[ - ]
Reply by kammutierspuleApril 18, 2020

Thanks! The Observer Pattern looks easy to understand and with great use!

[ - ]
Reply by rmilneApril 19, 2020

Design patterns haven't figured much into the simple embedded systems I've worked on - a wash machine would fall into that category.  Simple embedded devices are event driven architectures where memory is statically allocated and the relationships between event producers and consumers is fixed and well understood.  A pub-sub model between internal layers isn't something I see.  At most I have seen dependency injection techniques where a device can be configured at build time to use one peripheral rather than another via a common interface.  This kind of abstraction is useful for testing and code reuse but a waste of time unless you have to support a family of products that can use a variety of similar hardwares (ie: substituting a different display, camera, memory chip, serial byte stream from various media, etc.).