Danh mục

The Automated Factory Scenario

Số trang: 5      Loại file: pdf      Dung lượng: 23.43 KB      Lượt xem: 1      Lượt tải: 0    
thaipvcb

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Các Kịch bản tự động Nhà máy Giả sử bạn đang viết các hệ thống điều khiển cho một nhà máy tự động. Nhà máy có chứa một số lượng lớn các máy khác nhau, mỗi nhiệm vụ thực hiện khác biệt trong sản xuất của các bài viết được sản xuất bởi các-nhà máy tạo hình và tấm kim loại folding
Nội dung trích xuất từ tài liệu:
The Automated Factory Scenario The Automated Factory ScenarioSuppose you are writing the control systems for an automated factory. The factorycontains a large number of different machines, each performing distinct tasks in theproduction of the articles manufactured by the factory—shaping and folding metal sheets,welding sheets together, painting sheets, and so on. Each machine was built and installedby a specialist vendor. The machines are all computer-controlled, and each vendor hasprovided a set of APIs that you can use to control their machine. Your task is to integratethe different systems used by the machines into a single control program. One aspect thatyou have decided to concentrate on is to provide a means of shutting all the machinesdown, quickly if needed!NOTEThe term API means Application Programming Interface. It is a method, or set ofmethods, exposed by a piece of software allowing you to control that software. You canthink of the .NET Framework as a set of APIs, as it provides methods allowing you tocontrol the .NET common language runtime and the Microsoft Windows operatingsystem.Each machine has its own unique computer-controlled process (and API) for shuttingdown safely. These are summarized below:StopFolding(); // Folding and shaping machineFinishWelding(); // Welding machinePaintOff(); // Painting machineImplementing the Factory Without Using DelegatesA simple approach to implementing the shutdown functionality in the control program isshown below:class Controller{ ... public void ShutDown() { folder.StopFolding(); welder.FinishWelding(); painter.PaintOff(); } ... // Fields representing the different machines private FoldingMachine folder; private WeldingMachine welder; private PaintingMachine painter;}Although this approach works, it is not very extensible or flexible. If the factory buys anew machine, you must modify this code; the Controller class and the machines aretightly coupled.Implementing the Factory by Using a DelegateHowever, although the names of each method are different, they all have the same“shape”; they take no parameters, and they do not return a value (we will consider whathappens if this isnt the case later, so bear with me!). The general format of each methodis, therefore:void methodName();This is where a delegate is useful. A delegate that matches this shape can be used to referto any of the machinery shutdown methods. You declare a delegate like this:delegate void stopMachineryDelegate();Note the following points: • Use the delegate keyword when declaring a delegate. • A delegate defines the shape of the methods it can refer to. You specify the return type (void), a name for the delegate (stopMachineryDelegate), and any parameters (there are none in this case).After you have defined the delegate, you can create an instance and make it refer to amatching method by using the += operator. You can do this in the constructor of thecontroller class like this:class Controller{ delegate void stopMachineryDelegate(); ... public Controller() { this.stopMachinery += folder.StopFolding; } ... private stopMachineryDelegate stopMachinery; // Create an instance of the delegate}This syntax takes a bit of getting used to. You add the method to the delegate; you are notactually calling the method at this point. The + operator is overloaded to have this newmeaning when used with delegates (we will talk more about operator overloading inChapter 19, “Operator Overloading”). Notice that you simply specify the method name,and should not include any parentheses or parameters.It is safe to use the += operator on an uninitialized delegate. It will be initializedautomatically. You can also use the new keyword to explicitly initialize a delegate with aspecific method, like this:this.stopMachinery = new stopMachineryDelegate(folder.stopFolding);You can call the method by invoking the delegate, like this:public void ShutDown(){ this.stopMachinery(); ...}Invoking a delegate uses exactly the same syntax as making a method call. If the methodthat the delegate refers to takes any parameters, you should specify them at this time.NOTEIf you attempt to invoke a delegate that is uninitialized, you will get aNullReferenceException.The principal advantage of using a delegate is that it can refer to more than one method;you simply use the += operator to add them to the delegate, like this:public Controller(){ this.stopMachinery += folder.StopFolding; this.stopMachinery += welder.FinishWelding; this.stopMachinery += painter.PaintOff;}Invoking this.stopMachinery() in the Shutdown method of the Controller class willautomatically call each of the methods in turn. The Shutdown method does not need toknow how many machines there are, or what the method names are. You can remove amethod from a delegate by using the –= operator:this.stopMachinery += folder.StopFolding;The current scheme adds the machine methods to the delegate in the Controllerconstructor. To make the Controller class totally independent of the various machines,you need to supply a means of allowing classes outside of Controller to add methods tothe delegate. You have several options: • Make the delegate variable, stopMachinery, public: public stopMachineryDelegate stopMachinery; • Keep the stopMachinery delegate variable private, but provide a read/write property to provide access to it. You also need to make the stopMachineryDelegate type public as well: • public delegate void stopMachineryDelegate(); • ... • public stopMachineryDelegate StopMachinery • { • get • { • return this.stopMachinery; • } • • set • { • this.stopMachinery = value; ...

Tài liệu được xem nhiều: