Flexible factory service of the flexible .NET architecture (1)

xiaoxiao2021-03-06  75

This article is an article on how to build a .NET flexible architecture. Previous article introduces a method that meets the most configuration needs of the application. In that article, it is assumed that the specific content of a configuration file is as follows: value1 value_for_x Value1 // Reading A key with reserved name attributecfg.getValue ( "/ section2 / key / x") -> value_for_x // reading a key with reserved name attribute and its // attribute / child nodescfg.getValue ( "/ section2 / key / y / a1" ) -> 10cfg.getValue ("/ section2 / key / y / a3") -> 16 Use iConfig Many advantages include: • Provides the ability to access the information hierarchy with simple API; · Don't replace XPath, just use Some feasible agreements are supplemented and improved; • Allow the use of defaults; Use similar way to process properties and sub-elements. Factory service: The key to building large structures in the field of starting architecture is to start with a simple idea. That is to say, complexity does not construct a complexity basis. Next, we will describe how to expand and form a factory-level factory service on this simple configuration service. What is factory service? Language Fair produces such a tendency: forms a popular program habit or practice, and then introduces it into the language of language. For example, Java introduces keyword Interface, while this is just a formal concept in C before Java. C # also has keyword interface. At the same time, C # also forms a formal process of event processing by keyword delegate. In order to better understand the meaning of factory services, you need to first accurately understand the interface. The reason why this is because the interface is the core concept of the architecture. Understanding the interface interface is like a class with a set of methods. Therefore, if you get an object and clarify the interface it implemented, you can call these methods in a reliable way.

Consider the following code, you will find that it is actually very simple: // Let us define an interfaceinterface LoggingInterface {public void logMessage (String message);} // Let's see how we can use this interfacepublic void myLoggingFunction (Object o) {if (o is LoggingInterface) {LoggingInterface log = (LoggingInterface) o; log.message ( "test message");} else {// sorry not sure of the type of this objectthrow new Exception ( "Unexpected type") }} Now the problem is when you call the logMessage function, who will record this news? The above interface does not have the corresponding code to be responsible for logging this message, what should I do? In this case, it is necessary to achieve the following: public class FileLogger: LoggingInterface {private FileOutputStream fs = null; public FileLogger (string filename) {fs = new FileOutputstream (filename);} // implement the following method to complete the // contract required by LoggingInterfacepublic void logMessage (String message) {fs.println (message);} public close () {fs.close ();}} well, you can now add the following code: FileLogger fileLogger = new FileLogger ( "myfile "); MyLogGingFunction (filelogger); now, this requirement: You need to write all logging to the event log supported by the operating system.

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

New Post(0)