Design Patterns Overview

From LabVIEW Wiki
Jump to: navigation, search

A design pattern, also know as a software design pattern, is a reusable solution to a software engineering problem. Design patterns give the developer a starting point and can help improve efficiency, readability, scalability, and maintainability. Using a design pattern can help you easily expand your application and reuse your own development efforts when you want to add new features. Once you create a good architecture for your company, you can create templates that you and others can reuse for future projects.

Described below some useful design patterns that a developer can use in their application architecture. By no means is this a comprehensive list, and no one architecture should be considered as the "best", however it may be helpful to start with one of these design patterns, or variations on them. A good architecture is one that fits best for the application and can be a combination of the design patterns below.[1]

Basic Design Patterns

State Machine

The state machine is one of the fundamental design patterns LabVIEW developers frequently use to build applications quickly. You can use the state machine architecture to implement complex decision-making algorithms represented by state diagrams or flow charts. State machines are used in applications where distinguishable states exist. Each state can lead to one or multiple states, and can also end the process flow. A State Machine relies on user input or in-state calculation to determine which state to transition to next. See more on the State Machine Design Pattern.

Event Handler

The event handler design pattern provides a powerful and efficient architecture for handling user interaction with LabVIEW. Use the event handler for detecting when events occur such as a user changing the value of a control, moving or clicking the mouse, or pressing a key, etc. The standard event handler template consists of an Event structure contained in a While Loop. Configure the Event structure to have one case for each category of event you want to detect. Each event case contains the handling code that executes immediately after an event occurs. See more on the Event Handler Design Pattern.

Master/Slave

The Master/Slave design pattern is another fundamental architecture LabVIEW developers use. It consists of multiple parallel loops where each of the loops may execute tasks at different rates. Of these parallel loops, one loop acts as the master and the others act as slaves. The master loop controls all of the slave loops, and communicates with them using messaging architectures.[2]  See more on the Master/Slave Design Pattern.

Producer/Consumer

Use the Producer/Consumer design pattern for data sharing between multiple loops running at different rates. The Producer/Consumer pattern’s parallel loops break down into two categories; those that produce data, and those that consume the data produced. Data queues communicate data between loops in the Producer/Consumer design pattern. These queues offer the advantage of data buffering between producer and consumer loops. See more on the Producer/Consumer Design Pattern.

Intermediate Design Patterns

Queued Message Handler (QMH)

The Queued Message Handler (QMH) design pattern is a combination of producer/consumer, and event handler architectures together.  The producer loop, called the Event Handler Loop (EHL), contains an event structure that sends messages to the consumer loop, called the Message Handler Loop (MHL).  The MHL receives and processes the messages.  A message is enqueued when a UI event is triggered.  The QMH can also be designed to provide feedback from the consumer to the producer using User Events.  See more on the Queued Message Handler (QMH) Design Pattern.

Queued State Machine (QSM)

The Queued State Machine (QSM) design pattern is a combination of producer/consumer, event handler and state machine architectures together.  The producer loop, called the Event Handler Loop (EHL), contains an event structure that sends messages to the consumer loop, called the State Machine Loop (SML).  A message can be enqueued by UI events or from other states in the state machine.  The QSM can also be designed to provide feedback from the SML to the EHL using User Events.  See more on the Queued State Machine (QSH) Design Pattern.

Action Engine (AE)

The Action Engine (AE) design pattern can be thought of as a machine (Engine) that performs some useful task (Action) often on some data.[3] The data is stored by using a loop that runs only once with an uninitialized shift register or an uninitialized feedback node. Actions are defined by a case structure with code for each action and input as a string or enum commanding which action to execute. A Functional Global Variable (FGV) is a specialization of an AE that contains only two actions (Get or Read and Set or Write) and contains no other logic in the actions other than the reading or writing of the stored data. See more on the Action Engine (AE) Design Pattern.

Advanced Design Patterns

Object-Oriented Design Patterns

LabVIEW Object-Oriented Programming (LVOOP) uses concepts from other object-oriented programming languages which include class structure, encapsulation, and inheritance. You can use these concepts to create code that is easier to maintain and modify without affecting other sections of code within the application. There are many common Object-Oriented design patterns that can be applied to LVOOP.  See more on Object-Oriented Design Patterns.

Actor Oriented Design

Transition from thinking in dataflow to thinking in message flow. In larger applications with parallel modules in multiple threads, dataflow is insufficient to describe the interaction.  Instead, think of the flow of the messages (methods plus data) exchanged between those modules.  Dataflow makes sense for single processes with shorter lifetimes inside self-contained components.  In large complex applications, the messaging may be such that a dedicated message broker would be needed.  Dataflow will still govern the self-contained code pieces inside the components.[4] See more on the Actor Oriented Design Patterns.

Articles

"Design Pattern Case Study: A Simple Counter" - by Quentin "Q" Alldredge, Q Software Innovations, LLC

Which Design Pattern Should I Choose? The design pattern you choose is highly dependent on the project requirements. Here is a case study implementing a simple counter program in 10 different design patterns.

"Actor Framework is not as hard as you think and here is why…" - by Quentin "Q" Alldredge, Q Software Innovations, LLC

If you are familiar with the Queued Message Handler (QMH) Design Pattern and Object-Oriented Programming (OOP) then you already know everything you need to start with Actor Framework.  The purpose of this article is to show the parts of the Actor Framework that are comparable to the QMH Design Pattern and provide a step-by-step procedure for converting a QMH project to an Actor Framework project.

References

  1. "Scalable Design Patterns in LabVIEW" by David Ladolcetta (https://forums.ni.com/t5/Developer-Center-Resources/Scalable-Design-Patterns-in-LabVIEW/ta-p/3534309)
  2. "Application Design Patterns: Master/Slave" by National Instruments (http://www.ni.com/white-paper/3022/en/)
  3. Community Nugget 4/08/2007 Action Engines on NI Discussion Forums (https://forums.ni.com/t5/LabVIEW/Community-Nugget-4-08-2007-Action-Engines/td-p/503801)
  4. "NI Architect Summit: Object Oriented Programming In LabVIEW" by Nate Chandler-Smith. (https://www.distek.com/blog/national-instruments-architect-summit-object-oriented-programming-labview/)