Jump to content

Object-oriented programming: Difference between revisions

From LabVIEW Wiki
Added G Interfaces Section
m Add link to Template pattern
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
Object oriented software designs in LabVIEW follow many of the same programming principals as object oriented software in [https://en.wikipedia.org/wiki/Object-oriented_programming other programming languages]. In general object oriented software is done in LabVIEW by first creating a class.  This class is an object that has has the ability to add functionality to data. This allows for abstraction of the underlining data and allows operations to happen on a more segmented part of the applicationThis often leads to better reusability and scalability.
'''Object-Oriented Programming''' (OOP) is a [[Wikipedia:programming paradigm|programming paradigm]] that enforces [[Wikipedia:object-oriented design|object-oriented design]] principles to solve software problems. In object-oriented programming, a [[class (object-oriented programming)|class]] defines data and [[method (object-oriented programming)|method]]s to work with that data. A client then creates an [[object (object-oriented programming)|object]] - a specific instance of a class - to perform actions using methods that are exposed by the classImplementation details are hidden from the client.


== Access Scope ==
Because implementation details are hidden from the client, methods and data of a class can change without affecting any of the client's code. This can even happen at runtime by replacing one class with another of the same kind. Classes can [[inheritance|inherit]] methods and data from a parent class to [[overriding|override]] methods of the parent class. The client can then use the child class as if it was the parent class, without changing any of the existing code. Methods called by the client will seamlessly execute the methods defined by the child class. If the child class doesn't override a method, the parent's method is called. This behavior is called [[dynamic dispatching]] in LabVIEW.


Object oriented software allows for specific data, or functions to have scope set to them which restricts access from other parts of the application.  A common use for this is with internal references, or sessions, which if given to the rest of the application could be inadvertently closed.  By keeping the internal references from being accessed externally, an object can have more control over who has access, and how its internals are accessed.
[[File:OOP Example.png]]


== Inheritance ==
==Object-Oriented Programming in LabVIEW==
As of [[LabVIEW 8.20]], [[G]] is an object-oriented programming language that enforces object-oriented design principles.


Using object oriented software in LabVIEW allows for a class to inherit from another class. This child class has the ability to call all the parents functions, but then also can optionally override a parents call if the VI is setup to be dynamic dispatch.
A class is defined by a [[class library]] that serves as a user-defined data type. Each class consists of a private data control and methods (member VIs) to access that data. A class can change the [[access scope]] of its methods to public (anyone), protected (this class and its descendants), community (this class and its friends) or private (this class).


== Dynamic Dispatch ==
An object is a specific instance of a class. It is represented by data on a wire and behaves like a [[cluster]]. Objects in G are by-value. When a wire forks, the object may be duplicated, as decided by the [[LabVIEW compiler]]. A wire of a specific class can carry an object of that class or any of its descendants without loss of data.


Calling a parents initialize function might not be sufficient for all children, and so a dynamic dispatch call can override the parents call with the child's specific implementation.  This child implementation can also call the parent's depending on how the VI is written.
G supports single inheritance in which a class can have only one parent. Children inherit public and protected methods from their parent and can override inherited dynamic dispatch methods. A parent class can require a descendant to override specific dynamic dispatch methods and to call the parent method from inside the override.


== Interfaces ==
Many common object-oriented design patterns have been adapted into the by-value dataflow in G.
* [[Aggregation pattern]]
* [[Command pattern]]
* [[Decorator pattern]] (aka. Wrapper pattern)
* [[Delegation (object-oriented programming)|Delegation]] (aka. Delegation pattern)
* [[Factory pattern]]
* Hierarchy composition pattern
* Singleton pattern
* Specification pattern
* Strategy pattern (aka. Policy pattern)
* [[Template pattern]] (aka. Channeling pattern)
* Visitor pattern
See also: [[:Category:Object-Oriented Design Pattern]]s


* See [[Americas CLA Summit 2019#StephenGInterfaces|Stephen Loftus-Mercer ACLA Summit 2019 Presentation on G Interfaces]].
==Benefits of OOP==
===Reusability===
===Scalability===


== See also ==
==See also==
* [[LabVIEW Class]]
* [[LabVIEW Object]]
* [[LabVIEW object-oriented programming FAQ]]
* [[Americas CLA Summit 2019#StephenGInterfaces|Americas CLA Summit 2019: G Interfaces]] by Stephen Loftus-Mercer
* [[Graphical Object-Oriented Programming]] (GOOP)
* [[G-sharp Framework|G# Framework]]


* [[LVClass]]
==External links==
* [[G-sharp Framework]]
*[https://www.ni.com/en-us/support/documentation/supplemental/06/labview-object-oriented-programming-faq.html LabVIEW Object-Oriented Programming FAQ] by National Instruments
* [[GOOP]]
*[https://www.ni.com/en-us/support/documentation/supplemental/06/labview-object-oriented-programming--the-decisions-behind-the-de.html LabVIEW Object-Oriented Programming: The Decisions Behind the Design] by National Instruments
*[http://www.eyesonvis.com/blog/2006/08/code-reuse-through-classes.html Code Reuse through Classes] by Christina Rogers
*[https://forums.ni.com/t5/LabVIEW-Development-Best/Introduction-to-Object-Oriented-Programming-and-Hardware/ta-p/3519580 Introduction to Object Oriented Programming] by Elijah Kerry
*[https://forums.ni.com/t5/LabVIEW-Development-Best/When-Should-You-Use-LabVIEW-Classes/ba-p/3478257 When should you use classes] by Elijah Kerry


== External Links  ==
{{stub}}
*[http://www.ni.com/white-paper/3573/en/ NI FAQ on Object Oriented Programming].
 
*[https://forums.ni.com/t5/LabVIEW-Development-Best/Introduction-to-Object-Oriented-Programming-and-Hardware/ta-p/3519580 Introduction to Object Oriented Programming] by Elijah Kerry
[[Category:Object-Oriented Programming]]
*[http://zone.ni.com/reference/en-XX/help/371361P-01/lvconcepts/creating_classes/ Creating Classes].
[[Category:Programming Paradigm]]
*[https://forums.ni.com/t5/LabVIEW-Development-Best/When-Should-You-Use-LabVIEW-Classes/ba-p/3478257 When should you use classes].

Latest revision as of 14:14, 8 August 2024

Object-Oriented Programming (OOP) is a programming paradigm that enforces object-oriented design principles to solve software problems. In object-oriented programming, a class defines data and methods to work with that data. A client then creates an object - a specific instance of a class - to perform actions using methods that are exposed by the class. Implementation details are hidden from the client.

Because implementation details are hidden from the client, methods and data of a class can change without affecting any of the client's code. This can even happen at runtime by replacing one class with another of the same kind. Classes can inherit methods and data from a parent class to override methods of the parent class. The client can then use the child class as if it was the parent class, without changing any of the existing code. Methods called by the client will seamlessly execute the methods defined by the child class. If the child class doesn't override a method, the parent's method is called. This behavior is called dynamic dispatching in LabVIEW.

Object-Oriented Programming in LabVIEW

As of LabVIEW 8.20, G is an object-oriented programming language that enforces object-oriented design principles.

A class is defined by a class library that serves as a user-defined data type. Each class consists of a private data control and methods (member VIs) to access that data. A class can change the access scope of its methods to public (anyone), protected (this class and its descendants), community (this class and its friends) or private (this class).

An object is a specific instance of a class. It is represented by data on a wire and behaves like a cluster. Objects in G are by-value. When a wire forks, the object may be duplicated, as decided by the LabVIEW compiler. A wire of a specific class can carry an object of that class or any of its descendants without loss of data.

G supports single inheritance in which a class can have only one parent. Children inherit public and protected methods from their parent and can override inherited dynamic dispatch methods. A parent class can require a descendant to override specific dynamic dispatch methods and to call the parent method from inside the override.

Many common object-oriented design patterns have been adapted into the by-value dataflow in G.

See also: Category:Object-Oriented Design Patterns

Benefits of OOP

Reusability

Scalability

See also

External links