Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Các phương pháp tiếp cận phụ trách lớp học là một biến thể trên các mẫu thiết kế Nhà máy, trong đó một phương pháp "nhà máy" là chịu trách nhiệm cho việc tạo và quản lý một đối tượng. Trong nhiều trường hợp, các phương pháp này nhà máy được chia sẻ các phương pháp có thể được đặt trực tiếp | 46 CHAPTER 2 FRAMEWORK DESIGN Note The class-in-charge approach is a variation on the Factory design pattern in which a factory method is responsible for creating and managing an object. In many cases these factory methods are Shared methods that may be placed directly into a business class hence the class-in-charge moniker.1 In this model I ll make use of the concept of Shared factory methods on a class. A Shared method can be called directly without requiring an instance of the class to be created first. For instance suppose that a Customer class contains the following code Serializable _ Public Class Customer Public Shared Function NewCustomer As Customer Dim svr As AppServer _ CType Activator.GetObject GetType AppServer _ http myserver myroot appserver.rem AppServer Return svr.CreateCustomer End Function End Class Then the UI code could use this method without first creating a Customer object as follows Dim cust As Customer Customer.NewCustomer A common example of this tactic within the .NET Framework itself is the Guid class whereby a Shared method is used to create new Guid values as follows Dim myGuid As Guid Guid.NewGuid This accomplishes the goal of making the UI code reasonably simple but what about the Shared method and passing objects by value Well the NewCustomer method contacts the application server and asks it to create a new Customer object with default values. The object is created on the server and then returned back to the NewCustomer code which is running on the client. Now that the object has been passed back to the client by value the method simply returns it to the UI for use. Likewise you can create a Shared method in the class in order to load an object with data from the data store as shown Public Shared Function GetCustomer ByVal criteria As String As Customer Dim svr As AppServer _ CType Activator.GetObject GetType AppServer _ http myserver myroot appserver.rem AppServer Return svr.GetCustomer criteria End Function Again the code .