labview利用簇模拟汽车控制_在LabVIEW应用程序中何时应采用面向对象技术 (翻译)...

本文探讨了在LabVIEW中何时应该使用面向对象编程技术,介绍了类的概念和优势,如继承、动态调度,并通过硬件抽象层和被测设备等应用场景举例。面向对象编程可以帮助提高代码的组织性和可维护性。
摘要由CSDN通过智能技术生成

b5f60b0b92c97ac047cf7d5ca2fa0bb1.png

在LabVIEW中应用面向对象编程技术并不是一件特别容易的事情,尤其是先期的学习成本很高,但是在开发大型、复杂的、长期维护的程序时,使用面向对象技术会带来较好的回报。

自从LabVIEW8.2版本引入面向对象编程范型后,至今已经有近十多个年头了,在面向对象的基石上,陆续积累很多好的开发框架(如Actor Framework)和测试样板例程(HAL),以及两个优秀的开源面向对象实现工具包(G#和GOOP),并且在NI美国的英文社区论坛上有大量的面向对象的相关资料,而相应的在国内的讨论中,相关内容研讨的很少,如果你咨询NI的技术工程师为啥面向对象的中文资料如此匮乏的时候,他会建议你去上《面向对像编程》的三天付费培训课程,因此在此我来充当一下知识的搬运工,将一些内容翻译后供大家一起学习与讨论!

这篇博文原作者写作的大约时间是在8.2版本刚刚引入面向对象范型后不久写的,其作者Elijah_K是NI公司资深的开发专家,这篇博文中主要简单介绍的面向对象的技术要素,数据和行为混装一起提供整体封装,通过继承可以形成层次类型系统以便更具编程表达力,并且结合多态(动态分配)完成归一化处理调用,从而达到上层框架的复用。原文中给出了很好的面向对象的出发点:硬件抽象层及被测试设备的泛化场景。

本篇资料文章为NI开发经理Elijah_K在2009年撰写的博客,讨论在应用程序中何时应采用面向对象技术!
When Should You Use LabVIEW Classes? Author Elijah_K

原文链接​decibel.ni.com


你应该在什么时候应用LabVIEW类呢?
I often have customers ask me if they should be using classes in in their LabVIEW application. Many of the people asking this question are long-term LabVIEW users who are new to the concept of an object-oriented development approach and they're struggling to identify how and where classes would benefit their application. If you've ever asked this question, or if you've ever wondered where in your application you would use classes, I'll do my best to shed some light on the topic and explain some of the primary benefits…
经常会碰到有客户询问我:“在他们的LabVIEW应用程序中是否应该使用类(面向对象编程)?”,在咨询此问题的人当中有些人已是长期的LabVIEW开发者,只是对于面向对象编程概念感到陌生,另外他们已经开始努力识别如何及在何处使用类,从而使得他们的应用程序从中受益。或者你已经在考虑在应用程序的哪里使用类,那么我将尽我所能的分享一些该主题的启发,并解释其主要的好处...
To put it simply, classes aim to make it easier to represent collections of items within software. Consider that real-world objects have attributes (colors, weight, size, etc..), and they also have things they can do (open, close, move, etc...). As you might expect, object-oriented programming allows you to define a class of objects, all of which have attributes (known as properties) and things they can do (known as methods).
简单来说,类的目标是使它更容易的表示软件中的事项集合,考虑到现实世界中的对象,它们拥有属性(如颜色,重量,尺寸,等等),它们也有相应的事情可做(如开启,关闭,移动,等等)。 如你所期望的,面向对象编程范型允许你定义一个代表着所有的对象的类,它具备所有的特性(也称之为属性)和能够有能力做的事情(也称之为方法
Consider some contrived examples:

  1. Vehicles - properties: color, size, weight, occupancy, fuel efficiency, methods: accelerate, brake, roll down windows
  2. Cell Phones - properties: battery size, call time, weight, size, methods: place call, end call, send text message
  3. Network Packet - properties: port, IP Address methods: send, print string

举一些具体的例子来说:
1.车辆 属性:颜色、大小 颜色,尺寸,重量,体积,燃油效率;
方法:加速,制动,升降门窗
2.手机 属性:电池尺寸,通话时长,重量,尺寸;
方法:拨通电话,结束通话,发送短信
3.网络包 属性:端口,IP地址
方法:发送,打印字符串
Now, you might be thinking to yourself, "Can't I just use a cluster?" The answer is generally 'yes.' In fact, if you've recently begun developing an application that relies upon a large, complex cluster that you're passing around your application, it may be an excellent candidate for replacing with a class. This is especially true if you're passing around an array of clusters that are used to represent items that your program needs to be able to communicate and interact with. Making the switch from a cluster to a class offers several benefits - I want to highlight the following (although there are many more):
现在,你可能会想“难道不能使用簇来解决吗?”,答案当然是肯定的。实际上,如果你已经开始开发个应用程序,并且依赖一个大型复杂簇在整个程序中传递使用,这种情形该簇很可能(注意只是可能,并不是决定性的!)是类的很好的候选者,这种方式特别适合当你在整个程序传递簇数组的时候,每个簇都是用来表示一组特定的事物并且程序各个部分都需要与之通信及交互,(这里面的每个簇都是分类好的事物,已经具备类的雏形),驱动从簇转化成类能够提供一些好处,虽然有很多但是我想重点突出一下几点优点:
1.Inheritance - so far, I've described explained that you can define a class of objects and give it properties and methods. What makes this even more powerful is the ability to define children of that class, which have the same properties and methods, as well as some of their own. They may also want to override the methods or properties of their parent class. In my list of examples, I mentioned 'vehicles.' As you can imagine, there are many different types of vehicles, some of which have very unique properties and things that they can do. If you consider 'pickup truck,' as an example, one of it's properties might be 'bed size,' which wouldn't make any sense when applied to a car or a motorcycle.
1.继承,到目前为止,我已解释可以定义一个代表对象的类,并赋予该类属性和方法,从而进一步定义该类的子类得以提供更为强大的编程能力,子类通过继承获得同样的属性和方法,这些属性和方法与它们父类完全相同,子类也有可能覆写父类的属性和方法,在我前述的例子中,我提到了“车辆”,大家可以会想到有多种不同类型的车辆,其中的一些车辆具有非常独特的属性和特别能力方法,如果你考虑皮卡车型的时候,例如它的一个可能的属性是“后货仓尺寸”,这个属性如果应用赋予汽车或者摩托车肯定是没有意义的!

2.Dynamic dispatching - If we have an array of similar objects, they likely share methods and properties. In LabVIEW, we create wrappers using VIs to access and modify these values. If we want the wrapper VI for a specific child class to do something different, we can create a VI to override the parent VI automatically. LabVIEW will automatically run the version of the VI that is appropriate for the current class. To put it simply, LabVIEW dynamically runs the VI based upon the class of the object supplied to it - this assumes that the object is a child of the generic class.
2 .动态分配,如果我们有一个包含着相似的对象数组,它们可能共享一些方法和属性。在LabVIEW中,我们使用VIs创建包装器来获取和修改这些值,如果我们需要这些包装器VI来针对一个特殊的子类做一些不同的事情时,我们可以创造个VI来自动的覆写原先父类VI,LabVIEW将会自动的运行与之当前类相匹配对应的VI。简而言之,LabVIEW动态运行VI时是依据这个类的实际对象来匹配(并且还需要确保该对象是通用泛型类的子类)
As a software engineer, it's important to be able to recognize the potential benefits and when the use of classes may make more sense. LabVIEW is almost always used to interface with hardware, so the I/O in your application may be the perfect place to start. Consider these examples:
作为一名软件工程师,重要的是何时正确、有意义的使用类并且有能力识别出这样做(即使用类)所带来潜在的好处,LabVIEW(的开发使用场景)几乎总是用来与硬件设备进行操控,所以在你的程序里有关I/O(输入输出)部分的也许是完美的开始(采用面向对象),考虑以下这些例子,
1.Hardware Abstraction Layers - This white paper on hardware abstraction layers illustrates some excellent examples of the benefits of classes and how they can be used to mitigate the risk of hardware obsolescence. Classes have been defined for certain subsets of functionality (ie: a generic Scope). When a specific instrument is connected or added, it inherits the properties and methods of the parent class, but may add additional functionality (ie: an Agilent Scope could be replaced with an NI Scope, or visa versa).
1.硬件抽象层,关于硬件抽象层的白皮书展示了一些优秀的例程,通过采用类(面向对象)技术获取益处并能够适用于减轻由于硬件设备废型停产而带来的风险,类可以用来定义一些特定功能函数子集(例如:一个通用的示波器类)。当一个特殊的设备驱动被使用或新增时,它将从父类继承属性和方法,子类也有可能增加特殊的功能函数(例如:一台安捷伦示波器可以容易被NI公司的示波器替代,或者通用的VISA驱动)。(备注:这就是接口继承,或者说是契约继承)
2.Devices Under Test - Consider the task of testing a large number of very similar, yet slightly different devices such as cell phones. One production line may be responsible for testing a variety of different types of phones, but they almost certainly all have similar properties. The code can be written using a generic cell-phone class, but the method to execute a specific operation may require slightly different commands be sent to the test executive. This can easily be added later on without major modifications to the code through the creation of a child-class.
2.被测设备—考虑测试大量非常相似的,但略有不同的设备如手机。一条生产线也许是负责测试各种不同类型的手机,但是几乎可以肯定的是各种类型手机都有类似的属性,这段类似代码可以用更为通用(泛化)的手机类来编写,但是一个特定的方法可能需要略有不同的命令用来发送到设备来执行测试。这可以很容易的后期创建一个子类来增加代码,并且不会对原先代码较大的修改。(备注:这就是实现继承的应用)
Personally, I recently adopted the use of classes in my projects - I went back in one of my largest projects and replaced an array of clusters with an array of classes. In my experience, the use of classes forced me to write code that has clearly defined 'ownership' - in other words, my code is cleaner and more modular because I've forced myself to say, 'this function is a member of this class - therefore, it is only responsible for operating upon the data available in this object' - this benefit is often referred to as encapsulation. This helps me avoid unnecessary coupling between sections of your application that make reuse difficult later on.
就我个人而言 ,我最近采用了类(面向对象)技术在我自己的项目中,我重构了一个自己最大的项目,将簇数组用类数组所代替。在我的经验中使用类迫使我在写代码的时候需要清晰的明确“所有权”(及这代码属于谁)-换句话说:这样的代码更清楚和模块化,因为我强迫自己“这个函数是该类的成员”,因此它相应的只能操作该类中的数据成员。-这样的好处常常被认为是封装技术所带来的,这样能够帮助我避免程序区段间的耦合并且降低以后重用的困难
The concept of a class is not unique to LabVIEW. In fact, the use of classes is probably most commonly associated with C++, which was basically C with classes added. However, most modern programming languages, including Java and C#, heavily emphasize the use of classes. Classes were introduced to LabVIEW in 8.2, but have been continuously improved and refined since then.
类概念(面向对象)并不是LabVIEW编程语言所独有的,事实上,使用类(面向对象)往往更加常见的与C++相关联上,C++是基本由C语法并增加了类功能的编程语言。但是,现代大多数编程语言(包括java和c#),非常强调类(面向对象)的使用。类(面向对象)是从LabVIEW8.2中引入的,自此还在不断的改进和逐渐的完善中。
For examples and illustrations of object-orientation at work, check out the following:

  • Creating a class in LabVIEW
  • Changing the inheritance of a LabVIEW Class
  • Object Oriented FAQ for LabVIEW
  • LabVIEW OO: The Decisions Behind the Design
  • Applying Common C Reference Architectures to LabVIEW Using Classes


如果需要面向对象如何使用的例程和说明,可以查看以下的链接

  • 如何在LabVIEW中创建一个类
  • 在LabVIEW类中改变继承
  • LabVIEW面向对象编程常见问题集
  • LabVIEW面向对象编程:设计背后的抉择
  • 在LabVIEW中应用通用的面向对象设计模式

There is a lot more to be covered and discussed on the topic of classes in LabVIEW, including by-reference implementations and Endevo's GOOP toolkit. Look for more in future entries.
上述链接中覆盖了诸多的类(面向对象编程)主题,诸如 by-reference 实现,另外还可以Endevo's GOOP toolkit(工具包)中查找到更多的相关内容。
A final tip: there are a number of settings and options when creating and managing classes, some of which are intended for advanced use-cases. If you're having trouble figuring out how to configure something, I highly recommend consulting the LabVIEW documentation. Also, feel free to post your questions below.
最后的小贴士:当在程序中创建和管理类的时候有大量的设置和管理选项,有一些配置是只适用于较为高级的用户场景。当你碰到如何配置及使用面向对象技术的时候,我强烈建议你查询LabVIEW随机文档。当然也可以在这篇博文下面张贴你的问题。

(全文完)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值