Jump to content

Design pattern: Difference between revisions

From LabVIEW Wiki
m Articles: Fix article does not link to external page
 
(9 intermediate revisions by 2 users not shown)
Line 10: Line 10:


=== Event Handler ===
=== 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|Event Handler Design Pattern]].
See more on the [[Event Handler|Event Handler Design Pattern]].


=== Master/Slave ===
=== 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.<ref>"Application Design Patterns: Master/Slave" by National Instruments (http://www.ni.com/white-paper/3022/en/)
See more on the [[Master/Slave|Master/Slave Design Pattern]].
</ref>   See more on the [[Master/Slave|Master/Slave Design Pattern]].


=== Producer/Consumer ===
=== 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|Producer/Consumer Design Pattern]].
See more on the [[ Producer/Consumer|Producer/Consumer Design Pattern]].


== Intermediate Design Patterns ==
== Intermediate Design Patterns ==


=== Queued Message Handler (QMH) ===
=== 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|Queued Message Handler (QMH) Design Pattern]].
See more on the [[Queued Message Handler|Queued Message Handler (QMH) Design Pattern]].


=== Queued State Machine (QSM) ===
=== 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|Queued State Machine (QSH) Design Pattern]].
See more on the [[Queued State Machine|Queued State Machine (QSH) Design Pattern]].


=== Action Engine (AE) ===
=== 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.<ref>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)</ref>  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|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 [[Functional global variable|Action Engine (AE) Design Pattern]].
See more on the [[Functional global variable|Action Engine (AE) Design Pattern]] a.k.a. [[Functional global variable]].


== Advanced Design Patterns ==
== Advanced Design Patterns ==


=== Object-Oriented 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]].
See more on [[Object-Oriented Design Patterns]].


=== Actor Oriented Design ===
=== 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.<ref>"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/)
See more on the [[Actor Oriented Design Patterns]].
</ref>  See more on the [[Actor Oriented Design Patterns]].


== Articles ==
== Articles ==
Line 48: Line 46:
*"[[Design Pattern Case Study: A Simple Counter]]" - by Quentin "Q" Alldredge, Q Software Innovations, LLC
*"[[Design Pattern Case Study: A Simple Counter]]" - by Quentin "Q" Alldredge, Q Software Innovations, LLC
*"[[Actor Framework is not as hard as you think and here is why…]]" - by Quentin "Q" Alldredge, Q Software Innovations, LLC
*"[[Actor Framework is not as hard as you think and here is why…]]" - by Quentin "Q" Alldredge, Q Software Innovations, LLC
*[https://forums.ni.com/t5/Example-Code/Applying-Common-Object-Oriented-OO-Design-Patterns-to-LabVIEW/ta-p/3510571 Applying Common Object-Oriented (OO) Design Patterns to LabVIEW] by National Instruments


== References ==
== References ==

Latest revision as of 10:22, 11 January 2025

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

See more on the State Machine Design Pattern.

Event Handler

See more on the Event Handler Design Pattern.

Master/Slave

See more on the Master/Slave Design Pattern.

Producer/Consumer

See more on the Producer/Consumer Design Pattern.

Intermediate Design Patterns

Queued Message Handler (QMH)

See more on the Queued Message Handler (QMH) Design Pattern.

Queued State Machine (QSM)

See more on the Queued State Machine (QSH) Design Pattern.

Action Engine (AE)

See more on the Action Engine (AE) Design Pattern a.k.a. Functional global variable.

Advanced Design Patterns

Object-Oriented Design Patterns

See more on Object-Oriented Design Patterns.

Actor Oriented Design

See more on the Actor Oriented Design Patterns.

Articles

References