Software Design Knowledge

Source: Internet
Author: User
Tags json serialization wrapper

This is I from the author of Dubbo to see an article, think very good turn around everybody study:

1. API and SPI separation Framework or component there are usually two types of customers, one is the user, one is the Extender, the API (application programming Interface) is for the user, and the SPI (Service provide Interface is for the Extender, in the design, as far as possible to isolate them, and not to mix together, that is, the user can not see the implementation of the extension write, such as: A Web framework, it has an API interface called action, there is an execute () method, is used to write business logic, The web framework then has an SPI interface for the Extender to control the output, such as using the velocity template output or JSON output, if the web framework uses a velocityaction and a jsonaction that inherit the action as an extension, To use the velocity template output to inherit the velocityaction, to use the JSON output inherited jsonaction, this is the API and SPI does not separate the reverse example, the SPI interface is mixed in the API interface, the reasonable way is, There is a separate renderer interface with Velocityrenderer and Jsonrenderer implementations, and the web framework gives the output of the action to the renderer interface for rendering output.

2. service Domain/entity domain/session domain separate any framework or component, there will always be core domain models, such as: Spring's bean,struts Action,dubbo's Service,napoli queue, etc. This core domain model and its constituent parts are called entity domains, which represent the target itself that we want to manipulate, the entity domain is usually thread-safe, whether through invariant class, synchronous state, or replication, service domain is the behavior domain, it is the function set of the component, and also responsible for the life cycle management of entity domain and session domain. For example, Spring's Applicationcontext,dubbo ServiceManager and so on, the service domain object is usually heavy, and is thread-safe, and with a single instance to serve all calls, what is the session. is an interactive process in which the important concept in a conversation is context and what is context. For example, we said: "See the Old Place," where the "old Place" is the contextual information, why say "old Place" the other side will know, because we have defined the "old place" specific content, so that the context usually holds the interaction process of state variables, etc., the session object is usually lighter, each request to recreate the instance, Destroy after the request is finished. Simply put: the meta-information is held by the entity domain, and the temporary state in a request is held by the session domain, and the service domain runs through the whole process.  

3. Set up an interception interface in an important process if you want to write a remote call frame, the remote call process should have a unified interception interface, if you want to write an ORM framework, that at least the SQL execution process, the mapping process to have the interception interface, if you want to write a web framework, That request execution process should have interception interface, and so on, there is no common framework can cover residence there is demand, allow external behavior, is the framework of the basic extension way, so that if someone wants to authenticate before remote call, verify the order, verify the black and white list, statistics under the log, If someone wants to add a page wrapper before the SQL execution, do the data permission control, statistics under the SQL execution time, if someone wants to check the role before the request execution, packaging input and output stream, statistics requests, and so on, can be done by themselves, without intrusion inside the framework, interception interface, Usually, the process itself is encapsulated with an object, passed to the interceptor chain, for example: The remote call main process is invoke (), the Interceptor interface is usually invoke (invocation), the invocation object encapsulates the context of the procedure to be executed, and invocation has an invoke () method, which is determined by the interceptor when it executes, and invocation also represents the interceptor behavior itself, so the invocation of the last interceptor is actually the process of wrapping the next interceptor, Until the invocation of the last interceptor is the final invoke () process of the wrapper, similarly, the SQL main process is execute (), the Interceptor interface is usually execute (execution), the same principle, of course, the implementation can be arbitrary, above is just an example.

4. Important status change Send event and set aside listener interface here first, the difference between an event and the above interceptor, the interceptor is the intervention process, it is part of the process, is based on the process behavior, and the event is based on the state data, any behavior changes in the same state, the event should be consistent, Events are usually post-event notifications, which is a callback interface, where the method name is usually past, such as onchanged (), such as a remote call frame, when the network disconnects or the connection should emit an event, and when an error occurs, consider issuing an event, This makes it possible to observe changes within the framework and adapt accordingly.

5. Extension interface responsibilities as simple as possible, with composable for example, the remote call framework its protocol is replaceable, if only provide a total extension interface, of course, can do the switch protocol, but the protocol support can be subdivided into the underlying communication, serialization, dynamic proxy mode, etc., if the interface is split, orthogonal decomposition, will make it easier for the Extender to reuse the existing logic, instead of just replacing a part of the implementation strategy, of course, the granularity of this decomposition needs to be grasped well.

6. Micro-core plug-in, equal treatment of the third party general development of the better framework, all adhere to the concept of micro-core, eclipse of the micro-core is OSGi, Spring's micronucleus is the beanfactory,maven nucleus is plexus, usually the core should not be functional, Instead, it is a lifecycle and an integration container, so that functions can interact and expand in the same way, and any function can be replaced, if the micro-core is not done, at least equal to the third party, that is, the ability of the original author to achieve, the extension should be able to do all of the way, the original author to himself as an extender This will ensure the sustainability of the framework and the stability from the inside out.

7. Do not control the life cycle of an external object such as the action used by the interface and the renderer extension interface, the framework allows the user or the Extender to report the class name or the class meta information of the action or renderer implementation class, and then internally through the Reflection newinstance () Create an instance so that the framework controls the life cycle of the action or renderer implementation class, the action or renderer, the framework has done its own, the external expansion or integration is powerless, A good idea is to have the user or extender bring up an instance of the action or renderer implementation class, and the framework simply uses these instances, how the objects are created, destroyed, and not framework-independent, and the framework provides tool-class-assisted management, rather than absolute control.

8. Configurable programmable, and maintain a friendly COC convention because of the many uncertainties in the use of the environment, the framework always has some configuration, usually to classpath directly to the configuration of a given name, or to allow the configuration path to be specified at startup, as a common framework, Should do anything can be configured to do the file must be done programmatically, or when users need to integrate your framework with another framework will bring a lot of unnecessary trouble, in addition, as far as possible a standard convention, if the user to do something in accordance with a contract, it does not need the configuration item. For example: Configure the template location, you can contract, if you put in the Templates directory do not match, if you want to change the directory, the configuration.

9. Distinguish between command and query, clear preconditions and post conditions this is part of the contract design, as far as possible to comply with the method of return value is the Query method, void return method is the command, the query method is usually idempotent, no side effects, that is, do not change any state, tune n times the result is the same, For example, get a property value, or query a database record, the command refers to a side effect, that is, modify the state, such as set a value, or update a database record, if your method is done to modify the state of the operation, and do a query return, if possible, split it into a write-read separation of two methods, For example: User DeleteUser (id), delete users and return deleted users, consider changing to GetUser () and void DeleteUser (). In addition, each method tries to assert the legitimacy of the incoming arguments, and the subsequent assertion returns the legitimacy of the result and documents it.

10. Incrementally expand without expanding the original core concept. For example: The remote call framework, there is no doubt that the serialization function, the function is very simple, is to transfer into objects, objects into a stream,
However, because OSGi may be used in some places, when serializing, the ClassLoader of IO may be isolated from the classloader of the Business party,
You need to convert the flow into a byte[] array, and then pass it to the business party's ClassLoader for serialization,
To accommodate OSGi requirements, the original non-OSGi and OSGi scenarios were expanded,
In this way, whether it is an OSGI environment or not, the flow is first byte[] array, copy once,
However, most scenarios do not use OSGi, but they pay the price for OSGi,
In the case of incremental expansion, non-OSGi code is intact,
Add an OSGi implementation that is directly dependent on OSGi when using OSGi.

Again, for example: In the beginning, the remote service is based on the interface method, making transparent calls,
So, the extension interface is, invoke (method, object[] args),
Later, there is no need for interface calls, that is, no interface method can be called, and the Pojo objects are converted to map representations,
Because the method object cannot be directly new, we do not consciously choose an extended extension,
The extension interface was changed to invoke (String MethodName, string[] parametertypes, String returntypes, object[] args),
Causes the parametertypes to be transferred from class[] to string[, whether or not there is no interface call,
If you choose an incremental extension, you should keep the original interface unchanged,
Add a Generalservice interface with a generic invoke () method,
The same way as other normal business interfaces, the expansion interface does not have to change,
Just the Invoke () implementation of Generalserviceimpl will pass the received call to the target interface,
This makes it possible to increment the new functionality to the old functionality and keep the original structure simple.

Again, such as: Stateless message sending, very simple, serialization of an object sent past the line,
Then there is the need for synchronous message sending, which requires a request/response to be paired,
With extended expansion, it's natural to think that stateless messages are actually a request without response,
So add a Boolean state to the request to indicate that you want to return to response,
If there is another session message to send the demand, then add another sessions interaction,
Then found that the original synchronous message sending is a special case of session messages,
All scenes are passed to the session without the need to ignore the session.
If an incremental extension is used, the stateless message is sent intact,
A synchronous message is sent, and a request/response processing is added on the basis of the stateless message.
The session message is sent, plus a sessionrequest/sessionresponse processing.


-----------------------------------------------------------------------------------

Reprinted from: http://javatar.iteye.com/blog/690845

http://blog.csdn.net/pi9nc/article/details/17336651

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.