labview利用簇模拟汽车控制_在LabVIEW应用程序中何时应采用面向对象技术 (翻译)...
在LabVIEW應(yīng)用程序中何時(shí)應(yīng)采用面向?qū)ο蠹夹g(shù) (翻譯)
在LabVIEW中應(yīng)用面向?qū)ο缶幊碳夹g(shù)并不是一件特別容易的事情,尤其是先期的學(xué)習(xí)成本很高,但是在開發(fā)大型、復(fù)雜的、長期維護(hù)的程序時(shí),使用面向?qū)ο蠹夹g(shù)會帶來較好的回報(bào)。
自從LabVIEW8.2版本引入面向?qū)ο缶幊谭缎秃?#xff0c;至今已經(jīng)有近十多個(gè)年頭了,在面向?qū)ο蟮幕?#xff0c;陸續(xù)積累很多好的開發(fā)框架(如Actor Framework)和測試樣板例程(HAL),以及兩個(gè)優(yōu)秀的開源面向?qū)ο髮?shí)現(xiàn)工具包(G#和GOOP),并且在NI美國的英文社區(qū)論壇上有大量的面向?qū)ο蟮南嚓P(guān)資料,而相應(yīng)的在國內(nèi)的討論中,相關(guān)內(nèi)容研討的很少,如果你咨詢NI的技術(shù)工程師為啥面向?qū)ο蟮闹形馁Y料如此匱乏的時(shí)候,他會建議你去上《面向?qū)ο窬幊獭返娜旄顿M(fèi)培訓(xùn)課程,因此在此我來充當(dāng)一下知識的搬運(yùn)工,將一些內(nèi)容翻譯后供大家一起學(xué)習(xí)與討論!
這篇博文原作者寫作的大約時(shí)間是在8.2版本剛剛引入面向?qū)ο蠓缎秃蟛痪脤懙?#xff0c;其作者Elijah_K是NI公司資深的開發(fā)專家,這篇博文中主要簡單介紹的面向?qū)ο蟮募夹g(shù)要素,數(shù)據(jù)和行為混裝一起提供整體封裝,通過繼承可以形成層次類型系統(tǒng)以便更具編程表達(dá)力,并且結(jié)合多態(tài)(動態(tài)分配)完成歸一化處理調(diào)用,從而達(dá)到上層框架的復(fù)用。原文中給出了很好的面向?qū)ο蟮某霭l(fā)點(diǎn):硬件抽象層及被測試設(shè)備的泛化場景。
本篇資料文章為NI開發(fā)經(jīng)理Elijah_K在2009年撰寫的博客,討論在應(yīng)用程序中何時(shí)應(yīng)采用面向?qū)ο蠹夹g(shù)!
When Should You Use LabVIEW Classes? Author Elijah_K
原文鏈接decibel.ni.com
你應(yīng)該在什么時(shí)候應(yīng)用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…
經(jīng)常會碰到有客戶詢問我:“在他們的LabVIEW應(yīng)用程序中是否應(yīng)該使用類(面向?qū)ο缶幊??”,在咨詢此問題的人當(dāng)中有些人已是長期的LabVIEW開發(fā)者,只是對于面向?qū)ο缶幊谈拍罡械侥吧?#xff0c;另外他們已經(jīng)開始努力識別如何及在何處使用類,從而使得他們的應(yīng)用程序從中受益。或者你已經(jīng)在考慮在應(yīng)用程序的哪里使用類,那么我將盡我所能的分享一些該主題的啟發(fā),并解釋其主要的好處...
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).
簡單來說,類的目標(biāo)是使它更容易的表示軟件中的事項(xiàng)集合,考慮到現(xiàn)實(shí)世界中的對象,它們擁有屬性(如顏色,重量,尺寸,等等),它們也有相應(yīng)的事情可做(如開啟,關(guān)閉,移動,等等)。如你所期望的,面向?qū)ο缶幊谭缎驮试S你定義一個(gè)代表著所有的對象的類,它具備所有的特性(也稱之為屬性)和能夠有能力做的事情(也稱之為方法
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.NetworkPacket?-?properties:?port, IP Address?methods:?send, print string
舉一些具體的例子來說:
1.車輛 屬性:顏色、大小 顏色,尺寸,重量,體積,燃油效率;
方法:加速,制動,升降門窗
2.手機(jī) 屬性:電池尺寸,通話時(shí)長,重量,尺寸;
方法:撥通電話,結(jié)束通話,發(fā)送短信
3.網(wǎng)絡(luò)包 屬性:端口,IP地址
方法:發(fā)送,打印字符串
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):
現(xiàn)在,你可能會想“難道不能使用簇來解決嗎?”,答案當(dāng)然是肯定的。實(shí)際上,如果你已經(jīng)開始開發(fā)個(gè)應(yīng)用程序,并且依賴一個(gè)大型復(fù)雜簇在整個(gè)程序中傳遞使用,這種情形該簇很可能(注意只是可能,并不是決定性的!)是類的很好的候選者,這種方式特別適合當(dāng)你在整個(gè)程序傳遞簇?cái)?shù)組的時(shí)候,每個(gè)簇都是用來表示一組特定的事物并且程序各個(gè)部分都需要與之通信及交互,(這里面的每個(gè)簇都是分類好的事物,已經(jīng)具備類的雛形),驅(qū)動從簇轉(zhuǎn)化成類能夠提供一些好處,雖然有很多但是我想重點(diǎn)突出一下幾點(diǎn)優(yōu)點(diǎn):
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.繼承,到目前為止,我已解釋可以定義一個(gè)代表對象的類,并賦予該類屬性和方法,從而進(jìn)一步定義該類的子類得以提供更為強(qiáng)大的編程能力,子類通過繼承獲得同樣的屬性和方法,這些屬性和方法與它們父類完全相同,子類也有可能覆寫父類的屬性和方法,在我前述的例子中,我提到了“車輛”,大家可以會想到有多種不同類型的車輛,其中的一些車輛具有非常獨(dú)特的屬性和特別能力方法,如果你考慮皮卡車型的時(shí)候,例如它的一個(gè)可能的屬性是“后貨倉尺寸”,這個(gè)屬性如果應(yīng)用賦予汽車或者摩托車肯定是沒有意義的!
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 .動態(tài)分配,如果我們有一個(gè)包含著相似的對象數(shù)組,它們可能共享一些方法和屬性。在LabVIEW中,我們使用VIs創(chuàng)建包裝器來獲取和修改這些值,如果我們需要這些包裝器VI來針對一個(gè)特殊的子類做一些不同的事情時(shí),我們可以創(chuàng)造個(gè)VI來自動的覆寫原先父類VI,LabVIEW將會自動的運(yùn)行與之當(dāng)前類相匹配對應(yīng)的VI。簡而言之,LabVIEW動態(tài)運(yùn)行VI時(shí)是依據(jù)這個(gè)類的實(shí)際對象來匹配(并且還需要確保該對象是通用泛型類的子類)
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:
作為一名軟件工程師,重要的是何時(shí)正確、有意義的使用類并且有能力識別出這樣做(即使用類)所帶來潛在的好處,LabVIEW(的開發(fā)使用場景)幾乎總是用來與硬件設(shè)備進(jìn)行操控,所以在你的程序里有關(guān)I/O(輸入輸出)部分的也許是完美的開始(采用面向?qū)ο?,考慮以下這些例子,
1.Hardware Abstraction Layers?-?This white paper on hardware abstraction layersillustrates 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.硬件抽象層,關(guān)于硬件抽象層的白皮書展示了一些優(yōu)秀的例程,通過采用類(面向?qū)ο?技術(shù)獲取益處并能夠適用于減輕由于硬件設(shè)備廢型停產(chǎn)而帶來的風(fēng)險(xiǎn),類可以用來定義一些特定功能函數(shù)子集(例如:一個(gè)通用的示波器類)。當(dāng)一個(gè)特殊的設(shè)備驅(qū)動被使用或新增時(shí),它將從父類繼承屬性和方法,子類也有可能增加特殊的功能函數(shù)(例如:一臺安捷倫示波器可以容易被NI公司的示波器替代,或者通用的VISA驅(qū)動)。(備注:這就是接口繼承,或者說是契約繼承)
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.被測設(shè)備—考慮測試大量非常相似的,但略有不同的設(shè)備如手機(jī)。一條生產(chǎn)線也許是負(fù)責(zé)測試各種不同類型的手機(jī),但是幾乎可以肯定的是各種類型手機(jī)都有類似的屬性,這段類似代碼可以用更為通用(泛化)的手機(jī)類來編寫,但是一個(gè)特定的方法可能需要略有不同的命令用來發(fā)送到設(shè)備來執(zhí)行測試。這可以很容易的后期創(chuàng)建一個(gè)子類來增加代碼,并且不會對原先代碼較大的修改。(備注:這就是實(shí)現(xiàn)繼承的應(yīng)用)
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.
就我個(gè)人而言 ,我最近采用了類(面向?qū)ο?技術(shù)在我自己的項(xiàng)目中,我重構(gòu)了一個(gè)自己最大的項(xiàng)目,將簇?cái)?shù)組用類數(shù)組所代替。在我的經(jīng)驗(yàn)中使用類迫使我在寫代碼的時(shí)候需要清晰的明確“所有權(quán)”(及這代碼屬于誰)-換句話說:這樣的代碼更清楚和模塊化,因?yàn)槲覐?qiáng)迫自己“這個(gè)函數(shù)是該類的成員”,因此它相應(yīng)的只能操作該類中的數(shù)據(jù)成員。-這樣的好處常常被認(rèn)為是封裝技術(shù)所帶來的,這樣能夠幫助我避免程序區(qū)段間的耦合并且降低以后重用的困難
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.
類概念(面向?qū)ο?并不是LabVIEW編程語言所獨(dú)有的,事實(shí)上,使用類(面向?qū)ο?往往更加常見的與C++相關(guān)聯(lián)上,C++是基本由C語法并增加了類功能的編程語言。但是,現(xiàn)代大多數(shù)編程語言(包括java和c#),非常強(qiáng)調(diào)類(面向?qū)ο?的使用。類(面向?qū)ο?是從LabVIEW8.2中引入的,自此還在不斷的改進(jìn)和逐漸的完善中。
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
如果需要面向?qū)ο笕绾问褂玫睦毯驼f明,可以查看以下的鏈接
如何在LabVIEW中創(chuàng)建一個(gè)類
在LabVIEW類中改變繼承
LabVIEW面向?qū)ο缶幊坛R妴栴}集
LabVIEW面向?qū)ο缶幊?#xff1a;設(shè)計(jì)背后的抉擇
在LabVIEW中應(yīng)用通用的面向?qū)ο笤O(shè)計(jì)模式
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.
上述鏈接中覆蓋了諸多的類(面向?qū)ο缶幊?主題,諸如 by-reference 實(shí)現(xiàn),另外還可以Endevo's GOOP toolkit(工具包)中查找到更多的相關(guān)內(nèi)容。
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.
最后的小貼士:當(dāng)在程序中創(chuàng)建和管理類的時(shí)候有大量的設(shè)置和管理選項(xiàng),有一些配置是只適用于較為高級的用戶場景。當(dāng)你碰到如何配置及使用面向?qū)ο蠹夹g(shù)的時(shí)候,我強(qiáng)烈建議你查詢LabVIEW隨機(jī)文檔。當(dāng)然也可以在這篇博文下面張貼你的問題。
(全文完)
本文轉(zhuǎn)載知乎大神“李時(shí)珍”?
更多內(nèi)容歡迎關(guān)注公眾號
喜歡就點(diǎn)個(gè)在看再走吧總結(jié)
以上是生活随笔為你收集整理的labview利用簇模拟汽车控制_在LabVIEW应用程序中何时应采用面向对象技术 (翻译)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言char转cstring,CStr
- 下一篇: 编程语言基础 c语言同步教案,C语言程序