Flexible .NET architecture unified factory service (2)

xiaoxiao2021-03-06  73

Now, the function you own can implement universal targets that provide you with an object corresponding to the specified symbolic name. This utility is called factory services. However, this is just a simple framework for plant services. We will make further expansion in this way so that we can cover more situations, and strive to achieve maximum use of formalized factory services, we will modify demand into a factory service interface. Public interface ifactory {public object getObject (String SymbolicName):} Therefore, the factory service will return the object corresponding to the specified symbol name. Use the cache enhance factory service assumptions: Now there are two clients that are trying to log in to the application using the factory service. Specifically the following situations: // client1IFactory fact; LoggingInterface log = (LoggingInterface) fact.getObject ( "logger"); log.logMessage ( "Message from client1"); // client2IFactory fact; LoggingInterface log = (LoggingInterface) fact. GetObject ("Logger"); Log.LogMessage ("Message from Client2"); also assumes that Filelogger is used as our implementation tool. So now the question is, what many Filelogger objects do you expect in the app? This is an ideal result if the two clients can share the same object. Therefore, the factory service should take this requirement to buffer the requested object. However, how can factory services know to buffer the object? This requires the necessary clues to provide factory services to make it clear that the class has only one instance in the entire application. To do this, you can use the following two ways: a method is to change the configuration, as shown below: ·

Public interface SingleInstanceInterface {} public interface {} If the class we mentioned does not implement any of the above interfaces, the default is the MultiPleInstanceInterface interface. Using initialization to improve the factory so far, I have never mentioned a very important detail of Filelogger. Below, let's take a look at the configuration file: fileger ABC The following interface is used to handle Filelogger Class: Public Interface InitializableInterface {public void initialize (string requestname, IConfig cfg);} public class FileLogger: LoggingInterface // main job, SingleInstaceInterface // only one copy exists, InitializableInterface // we will see now what this is {private string filename; FileLogger (); public void Initialize (String Requestname, Iconfig CFG) {filename = cfg.getValue ("/ Request /" RequestName "/ filename", null); .. other Initialization stuff}} We are now serving factory services The interface covered is a simple summary. Please note: For convenience, "I" will be used to represent the interface. Public interface ifactory {// instantiates an Object and returns itpublic Object getObject (String SymbolicName); .. More Methods To be covered in subsequent articles} public interface ISingleInstance {} // Indicates to the factory that the intantiated object // needs to be cachedpublic interface IMultiInstance {} // Object can not be cachedpublic interface IIniti Alizable {// buy to read further unified configuration information; Public interface IComponent1 {ReturnType1 method1 (. Arg1, arg2, etc); ReturnType2 method2 (arg1, arg2, etc ..)} implemented method for this component are as follows: Public class MyComponentImplementation: IComponent1, ISingleInstance {// any local variables you May Havereturntype1 Method1 (arg1, arg2, etc.) {.. method1 importation} Returntype2 Method2 (arg1, arg2, etc ..) {.. mathod2 information}} You can use the following configuration file to specify the implementation of the runtime.

转载请注明原文地址:https://www.9cbs.com/read-91775.html

New Post(0)