Design Pattern: What is a Design Pattern and How Do I Use Them

Edit: Updates for OH 3.

What Is a Design Pattern (DP)

image

A DP is a concept borrowed from software engineering that is in turn borrowed from architecture. A DP is a general and reusable solution to a common problem. But the solution is not complete. Instead it is a template that one can apply to a lot of different situations with minor modifications and customizations.

The DPs posted to this forum will usually include a complete solution as an example implementation of the DP. But do not assume that the DP is only applicable in that specific example context. A DP is generic and should apply to a number of situations.

Finally, a DP rarely stands alone. They are often closely related to other DPs and many solutions will include multiple DPs, sometimes within just a few lines of code.

What a Design Pattern Is Not

As mentioned above, a DP is not a comprehensive example. They are a template that one can follow to solve a given class of problems one may encounter while developing Rules.

The example code that is included with the DPs may not be fully explained. The purpose of the example code is to illustrate the DP only. But since the DPs are only templates and not complete in and of themselves, the example code will necessarily include additional code.

What Should I Know to Use a DP

Because a DP is not a complete solution, the reader needs to possess a few skills in order to apply them to their situation:

  • the basics of how to define Items and use them in Rules code
  • the parts of a Rule and how to identify those parts in various contexts
  • the ability to follow a block of code and understand at a basic level what the code does
  • the ability to translate a block of code from one rules language to another where necessary
  • the ability to recognize and deal with code in a block that is not fully explained or defined in the DP text (e.g. reference to Items not defined in the current DP)
  • the ability to generalize an example to multiple situations

Languages

The DPs may include examples in multiple different supported programming languages. They may include UI rules examples, text based rule examples, or both. The purpose of the DP is to show a way of solving a problem, not a copy and paste example to simply use as is.

Where possible, a rule template that implements a DP will be posted to the Marketplace or as a utility in a library. Where that exists it will be noted.

Current Design Patterns

13 Likes
Design Pattern: Working with Groups in Rules
Design Pattern: Gate Keeper
Design Pattern: Event Limit
Design Pattern: Do While
Design Pattern: Motion Sensor Timer
Design Pattern: Switch & Dimmer
Design Pattern: How to Structure a Rule
What are your top 3 automations
Design Patterns: Generic Is Alive
Design Pattern: Sensor Aggregation
Design Pattern: Rule Latching
Design Pattern: Associated Items
Design Pattern: Proxy Item
Design Pattern: DRY, How Not to Repeat Yourself in Rules DSL
Design Pattern: Cascading Timers
Magical light scenes
Design Pattern: Recursive Timers
Design Pattern: Unbound Item (aka Virtual Item)
Design Pattern: Encoding and Accessing Values in Rules
Design Pattern: Manual Trigger Detection
Design Pattern: Time of Last Update
Design Pattern: Human Readable Names in Messages
Design Pattern: Expire Binding Based Timers
Design Pattern: Group Based Persistence
[Deprecated] Design Pattern: Time Of Day
Design Pattern: Looping Timers
Design Pattern - Timer Management
Best practice for small coding (Design Pattern)
Design Pattern: Cancel Activity
Design Pattern: Rule Disable
[SOLVED] My First Rule: Any advice?
Smart Virtual Thermostat (beta version)
GDPR Compliance and NEW WEBSITE!
[SOLVED] In sitemaps, how to create a view having item name and item value
Design Pattern: Using Item Metadata as an Alternative to Several DPs
Design Pattern: Hysteresis
Design Pattern: State Machine Driven Groups
Design Pattern: Separation of Behaviors
Using HABMin rules in HABPanel
[SOLVED]Scene light and persistence rrd4j
Design Pattern: Rule Refresh
[Deprecated] Design Pattern: Debounce
Issues with implicit variables newState and triggeringItem in 3.0.0
Upgrade 2 to 3 strategy/planning
Design Pattern: Working with Groups in Rules
[Deprecated] Design Pattern: Time Of Day