Write-start function


Despite the system architecture does not say, based on the code architecture is sure to be mastered, good code architecture program is maintainable foundation.

You might have a month to write the code for this month do not understand, do not know at the time how to write, so I recorded the code architecture recent study.


Write about boot function.


First look at a layered architecture.

Refer to "Software Architecture and Design Mode" on page 159 Figure 7-3


1, the application hierarchy orthogonally divided into multiple layers, each layer only addresses part of the problem, to provide a complete solution cooperating layers. The first step is a big problem into a series of relatively independent sub-problems, localized in each layer, thus effectively reducing the size and complexity of a single issue, to achieve a complex system is the most critical step in the decomposition .

2, a layered structure with good scalability, provides a flexible framework for the evolution of the growth of application, has good scalability. When adding new features without making changes to existing code, business logic to maximize reuse. At the same time, it can be easily inserted between the layers new extended application layer.

3, layered architecture is easy to maintain. After the decomposition of the system, different functionalities are encapsulated in different layers, the coupling between the layers is significantly reduced. Therefore, when a layer of code changes, they do not involve the interface between the layers, other layers would not have a serious impact.


1, it is not easy to divide levels.

2, find the right, the right level of abstraction method.



 Cons Pros bit more than true.


The following is an example of a layered:



Logical Architecture

User interface layer apis

  • http server
  • PRC Framework

Application Interface Layer service

  • Definition of software to complete all the tasks
  • Business process logic control
  • Model of mutual cooperation in various fields
  • Transaction Log Security
  • Usually refers to service

Domain layer

  • Express business concepts, status information, rules
  • Core business
  • Consisting essentially of, entity value object, the object field

Infrastructure Layer

  • Provide a unified technical capacity
  • Providing an application layer message, layer provides the field persistence mechanisms, to provide a user interface component
  • Unified algorithm
  • Communication between the layers
  • And other server interaction

Physical Architecture

  • PO continued to object 
  • DAO database operation
  • VO (View Object): a view object for display layer, it is the role of a given page (or component) of all of the data package together.
  • DTO (Data Transfer Object): data transfer object, this concept comes J2EE design patterns, the original purpose is to provide a distributed application EJB data entity coarse-grained, in order to reduce the number of the distributed calls, thereby improving call distributed performance and reduced network load, but here, I refers to data transfer objects between the presentation layer and the service layer.
  • DO (Domain Object): domain objects, is abstracted from the real world, tangible or intangible business entity.
  • PO (Persistent Object): persistent object, with which the persistence layer (usually a relational database) data structure is formed one to one mapping relationship, if the persistence is a relational database, the data for each field in the table ( or several) on the PO corresponding to one (or several) properties.


Now a basic understanding of what is the code architecture. Said the following about programming ideas boot function. I look at examples of pseudo-code


1, a boot class requires



class bootApplication { 

      // ... 

    constructor () 
    pubilc the Start () { 
        // Initialization. 1 Starter. 
        the init () 
	// 2 mounted Starter. 
        Setup () 
	// Start. 3 Starter. 
        Start () 
    // .. . // rest of the threads waiting for the end of the wait () } Private the init () {} Private Setup () Private Start ()
Private the wait ()


2, requires a basic starter interfaces, this is the life cycle of each subsystem.

// resource starters, each application and ultimately rely on other resources, such as databases, caching, messaging middleware, etc. Services 
// starters implementation class, do not need to implement all the methods, only need to implement the corresponding stage of the process can be embedded @BaseStarter
// start registration start by implementing the interface resource and resource initialization friendly management of these resources, installation, start and stop.
// Starter object registration is all that is required needs to be instantiated at system startup and operation logic, can implement this interface
// Note that only the Start method can be blocked if it is blocked Start () , while StartBlocking () to return true

the Starter type interface { // resource initialization and usually some resources in this preparation operation the Init (StarterContext) // installed resources, all required to start with the conditions, such resources may be initiated to achieve the standby state on the Setup (StarterContext) / / start the resource, can be used to achieve the status of start (StarterContext) //Description of the resource starter started the service, whether it will clog // If there is more congestion starters, only the last obstruction, will come before the asynchronous start by goroutine // So, you need to plan your starter registration order StartBlocking ( ) BOOL // resources stop: // normally when an exception is encountered when starting or enable remote management, and release resources for termination of the use of resources, // usually elegant release, waiting to continue an ongoing task, but not accept new tasks Stop (StarterContext) PriorityGroup () PriorityGroup Priority () int }


3, a registrar and a function of container

  // Interface Type

    starting register [] starts  

  // add an interface to the list array

    Register (intrerface starter) {

  append(starterRegister  , starter)


4, the class needs to implement the interface. Then call registration function added to starterRegister.


Oh, and then asked them to write back to boot function

E.g. Initi () function.

Traversing  starterRegister  the init function of each class implementation structure.

 func init(){

    for starterRegister {



The remaining start () .... all the same.


Then this is what I wrote Golang code. In fact, it consequently is not important, the most important good-looking. This is the entire project, we will be able to locate each point by pressing Ctrl structure is. Ye Hao modifications.







, Find the right, the right level of abstraction method.
[, Offset dào Zhǎo Heshi de, zhengque de cengci chouxiang fangfǎ.]
, Find the right, the right level of abstraction method.

Guess you like

Origin www.cnblogs.com/Addoil/p/11645755.html