How to develop a reusable software system

Source: Internet
Author: User
Tags abstract continue execution log requires
How to create a reusable software system

This article is an extension of the design pattern "Template method" template, applying the template to the whole software development product. First of all, the SOFTWARE product core unchanged business logic part of the abstract, for different parts of different products, the core products through the hook call Hook component of the specific implementation of the heavy, so the development of different systems, as long as the content of the change hook component to achieve a different product. The key to development is to abstract the function of the core product. Of course, this development thinking is also limited, not suitable for all development projects (abstract all the core of the project is meaningless), this kind of development thinking more suitable for a field of product development, the same area of abstraction of the core products have practical value.



Creating reusable components is the first step in learning how to create a reusable program ――――by Mike Cahn

If you develop several software projects in a row, you will find yourself writing a lot of repetitive code, and when you realize this, you will definitely generate the idea of reusing code. In fact, many development teams have built their own code libraries and build libraries to easily apply them to new projects. I'll tell you how to raise the reuse to a higher level: reusing the entire application

If you develop a particular business function or specific vertical market, you will find that there is a lot of overlap between the customer's requirements and the components that reuse the core code, But the difference between requirements requires that you have to develop a new system for each client.

Unfortunately, rewriting will increase design and development time and more complex maintenance, for example, if you have multiple versions, you will face the problem: Spread a bug fix, product add function requirements for each customer to specify changes and testing, reuse build help, etc.!!!! But if the core logic of all applications is the same, why not reuse this part of the core logic better?

The answer is, of course, you can do it, develop a core product, and then you can use it between different functional layers without having to change the core code.

What is a hook?

A hook is where you set up your client instantiation code, which is a way for a bundle of core product calls to custom components, a client layer (hook component) that needs to know what is happening, or where you need to modify the core product behavior, your core product can be invoked using hooks

A typical application is: A hook method is invoked by the current context of the data and hook component, and the hook component processes the call to return the correct information

When designing core products and hook components, you have to keep them as loosely coupled as possible in the future if the core product changes will not affect the already configured hook components. You also have to make sure that when configuring the hook component, the core product increases beyond the scope of the new hook call after the hook component can also perform the correct event

Identify core features:

When you use hooks to create a core application, the first step is to determine what is going to be the core, and if the core product is set up with too little functionality, you will have to repeat common functionality for each project, adding a lot of work! Repetitive code operations undermine the purpose of core application development. But if you include a common feature that is not available to all customers, all development projects will have a lot of problems using the core code, and you will have to modify the core product

It is much more useful to add portable functionality later in the core product than to remove features that should not have been added, but it may be too conservative to make a mistake! The functionality that fits the core product can be created as a shared component, and you can also transplant it when you're ready to do it.

The key to determining core application functionality (functionality) is to define a program flow that is common to all customers, let's take a look at an example where you are developing three systems that have a workflow based on a mobile work item, where users can perform "next" in the three systems , from one work project to the next, the following are the detailed differences between the three systems:

System 1: This system is just moving work items to the next step, but nothing is done

System 2: The system records the details of work items and operational steps in an authorization (Legacy) system, while writing a reference key from an authorized (Legacy) system to a work item

System 3: The system displays a check box for user updates before the work item moves to the next step, and the system undo "next" action if all the required actions of the user flag have not been successfully completed




Figure 1 shows how the functionality is divided between the core product and the three different client tiers, in this case

The customer layer requires the following capabilities:

Use and update core product objects (work items in this example)

Interacting with the consumer

Change the workflow of a core product (for example, undo the current operation)

Connecting to other applications or components

These requirements are typical and you have to take into account all of the functions in your core product

Build Hook Interface:

Parameters that are set within the hook component depend on context changes, which are much stronger than maintaining a bunch of hook interfaces, so creating a generic hook interface is a good solution. Use a set of variables or a parameter array to accommodate the different data types you will be adding, to store the hook's marker in the first argument, the first thing the hook component does is to interpret the marker and then call the corresponding function or component, and the following code uses the Select ... the case structure implements this functionality:

Listing 1: The core product calls the hook component (a very small hook component)

' Constants would normally is defined in a module or class
Const Nextstep_start = "Ns-start"
Const nextstep_selected = "ns-selected"
Const HOOK_OK = "OK"
Const Hook_cancel = "Cancel"
'...

' User just selected Next step operation
Shookresponse = Mobjhookcomponent.callin (Nextstep_start, _
Mobjcurrentitem, Mobjcurrentuser, "")
...
' User just selected the ' Step ' to '
Shookresponse = Mobjhookcomponent.callin (nextstep_selected, _
Mobjcurrentitem, Mobjcurrentuser, Sselectedstep)

Hook Component Code:
' Note:hook constants file would need to be included

Function Callin (ByVal shookid as String, vParam1 as Variant, _
VParam2 As Variant, vParam3 as Variant) as String

Dim Sreturnvalue as String
Dim Objworkitem as Clsworkitem
Dim objuser as Clsuser
Dim Sselectedstep as String

On Error Goto Callin_handler

Select Case Shookid

Case Nextstep_start
' Processing to be-NextStep is-a-invoked
Set Objworkitem = vParam1
Set objuser = vParam2
Call Writelog (Shookid, "Item ID:" & _
Objworkitem.workitemid & ", User ID:" & _
Objuser.userid)
Shookid = Hook_ok
Case nextstep_selected
Set Objworkitem = vParam1
Set objuser = vParam2
Sselectedstep = VParam3
Call Writelog (Shookid, "Item ID:" & _
Objworkitem.workitemid & ", User ID:" & _
Objuser.userid & ", Step:" & VPARAM3)

' ... do whatever processing are required ...
Shookid = Hook_ok

Case Else
' Unknown hook, possibly introduced to product
' After this component was written.
' Just allow it, but does nothing.
Call Writelog (Shookid, "unrecognised Hook")
Sreturnvalue = Hook_ok
End Select

Callin = Sreturnvalue

Exit Function

Callin_handler:
' Handle the error ...

End Function

Sub Writelog (Shookid As String, stext as String)
Dim Sfulltext as String

Sfulltext = time$ & "" & Shookid & "& Stext

Debug.Print Sfulltext

' Log to file used mainly for testing
' and for diagnosing production systems
If Isfileloggingon (Shookid) Then
' Output log info to text file ...
End If
End Sub (to is continue)

You can also use your familiar COM to implement this feature, they are very similar to the technique of assigning call methods



You must ensure that you do not need to recompile the existing hook component when you release the new version of the core product, so the hook component should ignore all the not-recognized hook markers and simply return a simple "success" code (refer to the Case Else section of the code list), and by this method, The hook component only needs to perform a recognized hook call for functional requirements, and if you need to add a new hook call point for the core product, the existing hook component will add new hook markers without changing any behavior.

In the "Next" example, the core product might call the hook at several execution points, for example: when the user requests the next action and then selects the step name again, these two examples will be:

Hook
Param1
Param2
Param3
Param4

Next Step started
"Ns-start"
objuser
Objworkitem
""

Next Step Step Selected
"Ns-selected"
objuser
Objworkitem
Sstepname




As a simple example, three variables (plus the hook identifier) may suffice, but to avoid the

To interface with the changes brought about by the trouble, you always have to add more functions, listing 1 shows a core product simple

Code: A hook component, an invocation instance of a hook

To perform the function of the customer's three check boxes, you should add a check box form to the Hook component project and

The following code calls:

Case nextstep_selected

'... Cast variables and call Writelog () ...

' Show the checklist
Frmchecklist.show vbmodal
If frmchecklist.allrequireditemsticked Then
Sreturnvalue = Hook_ok
Else
MsgBox "You have don't ticked all" & _
"Required Items." & _
"Next Step cannot Continue", vbexclamation
Call Writelog (Shookid, "Cancel sent back-" & _
"All required items not ticked")
Sreturnvalue = Hook_cancel
End If
' ... more code
Hook components affect core products:

The core product sets the point for the potentially changing requirements, and then delegates their control to the hook component, then responds to the hook

The state that the child component returns,

Data Object reference

When the hook component receives a reference to a data object, the core product needs to capture what the hook component is causing.

If you don't want the hook component to modify an object's parameters, you can use a propaganda object or a lock object, or a

An object that sets a part of the attribute before invoking the method, and if you allow the change, the object can authenticate them, or record them,

Validate them with the core product when the method returns, for example, users need to update a job with a reference keyword with two hooks

As a project

Case nextstep_selected
' ... cast variables and call Writelog () ...
Srefkey = Getmainframekey (Objworkitem.workitemid)
Objworkitem.reference = Srefkey
Sreturnvalue = Hook_ok
The core product can then query the returned work item object:

Mobjcurrentitem.resetchangedflag

Shookresponse = _
Mobjhookcomponent.callin (nextstep_selected, _
Mobjcurrentuser, Mobjcurrentitem, Sselectedstep)

If Mobjcurrentitem.changed Then
' Take appropriate action, such as validating
' and/or saving the changes ...
End If
Logical control

The hook component can return values that affect the core product logic, which is very limited, such as: you can specify

A set of hook components returns the flag (cancels the current operation, displays the standard dialog box), or your core product and Hook Group

Will create a tight coupling hazard. By predefined a group of customers without the need to change the table logo, you can for the core product

Capture any values that might be returned

Using interactive

In a typical Windows Forms application, the hook component can be published in VB form, and when the user needs to

Interaction calls

For Web applications, the core product and hook components need to run on the server, so the VB form does not apply.

But you can use the following several methods:

To invoke a new window browser by returning a URL for the core product, a call to the

URLs are returned to the core product, and all subsequent requests from this window can be called Non-core. Because this window is modeless

, so the user is clearly nested between the core product screen and the hook window.

The first step of the operation of a variable with a hook URL content to temporarily replace the core product browser, which is a very

An effective mode interface through which the request can be captured by custom components, and when control is returned to the core product, control

The system can store its own screen

Returns an HTML or XML to the core product that can be merged into a page being created, which allows the hook interface to pass

It's better to use the core product, but the core product has to convert the request from the hook part of the screen to the corresponding set

System components

Practice:

Binary compatibility ensures that your core product and hook components always use the same interface and can be used in office-specific

The computer records the core product you created, and the project team often creates hook components on the site so that any computer

Become available. This can sometimes cause the binary to be difficult to fix the problem, if you run like this, try to sync

These two create environments that include service components as much as possible. If you still do not work properly, you can discard binary compatibility,

Use slower late binding as a last resort

Test:

The hook component provides useful debugging and testing help, as demonstrated, your basic hook component should record all calls and parameters, and the typical way to respond to each hook is to customize the code for each customer, but even an internal use, it is worthwhile to write a configurable hook component. The behavior of the component is not hard-coded, and its response is controlled through the List2 configuration file (the XML file is used because he can contain a scalable information). You have written a configurable hook component that you have to change to test for different situations, and you will only need to modify this configuration file. Such modifications are easy for developers and have significant usefulness for testers, who do not have to modify the code.

Your configurable hooks allow you to specify a different return value for each hook marker, with a bit of precision for each basic user-defined behavior example. I also added a pause setting and a message box for display, which is useful for analog latency.

Finally, I added a way to explain how the hook component changes incoming parameters (see Code Listing 2)

Release:

If there are no features other than logging, be sure to remember that you must submit at least the same Hook component branch as the core product because your core product needs to be able to invoke execution of it

For more information:

This hook concept is based on the "template" design pattern, and if you have not heard of this design pattern, you should read the "design pattern" book, and they assume that you are using a language with full inheritance, although this is not necessary. Through vb.net we can achieve, vb60 through a little effort can also achieve this goal if you feel this article is a bit difficult, you can write from the "VB design mode" Stamatakis, although it does not cover all the patterns, but he is a good start for VB developers





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.