Atitit. Meta programming summary o99, atitit programming summary o99
Atitit. Meta programming summary o99.doc
1. Metaprogramming 1
2. History and Development of metaprogramming 1
3. Metadata type and 1
4. Implementation of metaprogramming 2
4.1. Code Generation 2
4.2. lex and yacc analyzer 2
4.3. Generic programming 2
4.4. annotation 2
4.5. interpreted framework 2
4.6. Object factory concept, a program that can write programs! 3
4.7. Aop 3
4.8. Data Object triggers and configurable plug-in services 3
5. Application Scenario 4
6. Refer to 4
1. Meta programming)
Metaprogramming refers to the compilation of a certain type of computer program. Such computer programs write or manipulate other programs (or themselves) as their data, or complete part of the work that should have been completed during compilation at runtime. In many cases, it is more efficient than writing all the code manually. The language used to write the metacharacter program is called the metacharacter language, and the language used to write the metacharacter program is called the target language. A language is also called reflection.
The business logic is separated from the implementation code. Only descriptive languages such as XML are used to describe the ing relationships between businesses. Programming is completed without writing implementation code.
Author: old wow's paw Attilax iron, EMAIL: 1466519819@qq.com
Reprinted please indicate Source: http://blog.csdn.net/attilax
2. History and Development of metaprogramming
In early 1994, a person named Erwin Unruh first discovered this. Erwin Unruh presented a special piece of code during a meeting of the C ++ Standards Committee in San Diego on 1994. The special feature of this Code is that the functions of the program are implemented during the compilation period rather than the runtime period. The Compiler generates all the prime numbers from 2 to a given value in the form of error information. In the summer of the same year, Todd Veldhuizen was inspired by Erwin's example and found that he could use the C ++ template for metaprogramming and published a technical report.
3. Metadata and metadata
Type (generic ???) The data is metadata (anno/attr)
4. Implementation of metaprogramming
4.1. Code Generation
"Metaprogramming" is actually a alias for "code generation ".
You can give it a short piece of code to let it return an executable program or a parsing tree that can be identified or overwritten.
The most commonly used metaprogramming tool is a compiler that converts advanced languages into assembly languages or machine languages. A more flexible method is to embed an interpreter in a program to directly process program data. There are some implementations such as the RemObject's Pascal Script written for Object Pascal.
4.2. lex and yacc Analyzer
Another common example of metaprogramming is lex and yacc, which are used to generate lexical analyzer and syntax analyzer. Yacc is usually used as a compiler to generate a tool that converts advanced languages into machine languages.
Therefore, the so-called template metaprogramming can be understood as: it treats the compiler as a higher level interpreter and runtime. template programming is Generative programming (such as generic programming)
4.3. Generic programming 4.4. Annotation
Annotations play a core role. The idea is to use annotations to tell the tool how to generate new code, convert code, or determine runtime behavior. Taking Java Persistence API (JPA) as an example, this is also a function introduced by Java 1.5. It allows developers to specify the relationship between Java objects and database entities in a declared manner such as @ Entity. Then, tools such as Hibernate can use these annotations to generate ing files and SQL queries at runtime.
4.5. interpreted framework
In particular, the Openbiz framework is an interpreted framework, which is equivalent to a "compiler" role. When other development environments and frameworks are designed to reduce the amount of code written by developers, Rocky said, don't let them write code. simply describe the ing relationship in XML to complete programming.
4.6. Object factory concept, a program that can write programs!
Every time I mention this concept, I am excited, as if we are just a little away from intelligent programming. This idea is the first one I know. net introspection (this Chinese translation is very strange), that is, the main program dynamically creates another independent subroutine, dynamic compilation, then load and destroy as needed (like Transformers). At that time, I was very excited. Since then, this concept was basically no longer mentioned.
Later, I read and analyzed the underlying source code of Openbiz and found the idea of object factory based on PHP. Take the data object as an example:
The XML-based Metadata File is considered as an assembly ticket sent to the "Factory". The above describes how to "Assemble" the object and the ing relationship between the object and the formation database, mappings with other objects at the same level (for example, one-to-many ORM)
After receiving the production instructions for creating such an object, the object factory creates and assembles the desired object according to the description, and caches the Object Body and status in the system in a serialized manner to trigger the call again, and optimized performance. Until the metadata configuration file changes, the object only needs to be dynamically produced once, that is, unlimited use.
4.7. Aop
4.8. Data Object triggers and configurable plug-in services
The trigger of this email and text message must not be implemented at the UI Layer, because we should consider that the logic of sending mail should be triggered no matter where the order is generated. Therefore, the business logic should be coupled with the data object, that is, the logic should be triggered as long as an order is generated.
Some common reusability logics of sending and sending messages can be defined as pluginService. For example, in the Service of sending an email, the recipient, title, and content should be API parameters, the SMTP server information of the mail account usually does not change much compared to the entire business system. It should be used as a metadata interface, how to send emails through a link to the server is the logic of the reusable object. The subtlety of this design will be analyzed in the next article
5. application scenarios
Based on this programming logic, we solve a common problem of modification and expansion.
For example, the customer often proposes to modify the bottom layer data field during project acceptance. "You can add a birthday and preference field to the contact management module, otherwise, I'm afraid this final payment... ".
What should I do?
Change it. CRUD, List, and Search must be modified.
Who changed it?
You must change it because you write a program.
Openbiz metadata is different. Now I only modify one data description file, and then the object factory will detect that the metadata configuration file has changed, then he automatically re-writes the object and all its associated ORM ing calls (ORM ).
When you are dealing with a system with extremely complex business coupling, you will find that these upper-layer objects "you have me, I have you" are extremely complicated (disgusting) stack calls ). For example, you have also called the contact fields in the view of the Document Modification record. Are you sure you can modify them all the way to the data structure?
This is not what human resources can do! But the object factory can, because it is created in On-demand production.
6. Reference
Yuan programming _baidu Baike .htm
Yuan programming _interactive Baike .htm
Java 8 Type annotation: Tools and opportunities _ Linux Eden open source community-24-hour rolling update of open source information, all year round !. Htm
Openbizframework, which is based on Java programming, implements PHP metadata programming _php_itpower .htm.