labview利用簇模拟汽车控制_在LabVIEW应用程序中何时应采用面向对象技术 (翻译)...
在LabVIEW應用程序中何時應采用面向對象技術 (翻譯)
在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.NetworkPacket?-?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 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.硬件抽象層,關于硬件抽象層的白皮書展示了一些優秀的例程,通過采用類(面向對象)技術獲取益處并能夠適用于減輕由于硬件設備廢型停產而帶來的風險,類可以用來定義一些特定功能函數子集(例如:一個通用的示波器類)。當一個特殊的設備驅動被使用或新增時,它將從父類繼承屬性和方法,子類也有可能增加特殊的功能函數(例如:一臺安捷倫示波器可以容易被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隨機文檔。當然也可以在這篇博文下面張貼你的問題。
(全文完)
本文轉載知乎大神“李時珍”?
更多內容歡迎關注公眾號
喜歡就點個在看再走吧總結
以上是生活随笔為你收集整理的labview利用簇模拟汽车控制_在LabVIEW应用程序中何时应采用面向对象技术 (翻译)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言char转cstring,CStr
- 下一篇: list steam_在 Steam 中