VB.net & Responsibility Chain Model

Source: Internet
Author: User

A few days ago has been talking about design patterns, in the discussion process with brother Siang, found that in the computer room charges personal reconstruction version of the use of several design patterns, first to discuss the responsibility chain model:

First, when you see this pattern at the first glance it is a very simple pattern, but when used to really have to consider a lot, first of all to understand what is the chain? A chain is a collection of nodes that can be flexibly split and re-reorganized. This is also different from the list of links, the user can go to access any point in the node as the start node.

Definition:enables multiple objects to have the opportunity to process requests, thus avoiding the coupling between the sender and the recipient of the request. link the object to a chain and pass the request along the chain until an object is processed by him. Example:

Now is knocking the personal Reconstruction Edition room charge system, in the process of the lower machine, we need and the basic data setting connection, namely the machine time is divided into three paragraphs to consider, the preparation time, at least machine time, unit increment time, in the judgment processing time he is once to carry on, this is not with the responsibility chain pattern coincides with? Referring to the small story of wage rise in the chain of responsibility, we can divide into three classes, prepare the time, at least the machine time and unit time

Look at the relevant code:

First you need an abstract class that handles an interface to the request:

' Bl_timehandler, abstract class, defines an interface for processing requests public MustInherit class Bl_timehandler    Protected calaulate as Bl_timehandler Public    Sub setcalaulate (ByVal calaulate as Bl_timehandler)  ' Set inheritors        me.calaulate = calaulate    End Sub    ' abstract method    for processing requests Public MustOverride Function Handletime (ByVal time as Integer) as Integerend Class

Then there is the inheritance of the three subclasses for the parent class:

 BL_ Preparetimehandler, preparing time processing class, inheriting Bl_timehandlerpublic class Bl_preparetimehandler:inherits Bl_timehandler Dim        Preparetime as Integer ' constructor, with the value of the generic set incoming prep time public Sub New (ByVal enbasicdata as IList (of Entity.basicdataentity)) Me.preparetime = CInt (enbasicdata (0). preparetime) ' Return expression End Sub public Overrides Function Handletime (time as Integer) As Integer if time <= preparetime then ' if the on-machine Times is ' prep time ', return 0 return 0 Else Re Turn calaulate. Handletime (Time) ' Go to next successor ' End If end FunctionEnd Class 
 BL_ Unittimehandler, Unit time processing class public classes Bl_unittimehandler:inherits Bl_timehandler Private unittime as Integer ' constructor, pass Into the increment time public Sub New (ByVal enbasicdata as IList (of entity.basicdataentity)) Me.unittime = CInt (enbasicdata (0). UN Ittime) End Sub ' is greater than at least time, returns the actual consumption time public Overrides Function handletime As Integer return M Ath. Abs (Int (-time/unittime)) * Unittime End Function 
' Bl_leasttimehandler,   at least on-Machine time class public class Bl_leasttimehandler:inherits Bl_timehandler    Private Leasttime as Integer    ' constructor, the value of the minimum incoming time on the public    Sub New (ByVal enbasicdata as IList (of entity.basicdataentity))        Me.leasttime = CInt (enbasicdata (0). leasttime)    End Sub public    Overrides Function handletime (time as Integer) as Integer        If Time <= leasttime then            return leasttime        Else            return calaulate. Handletime (time)        end If    End FunctionEnd Class
This is the end of the responsibility chain mode, in order to facilitate the invocation, you can add an interface, convenient to call, so that only need to pass in two parameters.

Public Class bl_onlinetimecount Public    Function costtime (ByVal enbasicdata as IList (of Entity.basicdataentity), Enlineinfo as Entity.lineentity) as Integer        ' instantiation class, through constructors, the transfer function        Dim bpreparetime as New Bl_preparetimehandler ( Enbasicdata)        Dim bleasttime As New Bl_leasttimehandler (Enbasicdata)        Dim bsteptime As New Bl_unittimehandler ( Enbasicdata)        bpreparetime.setcalaulate (bleasttime)  ' set responsibility chain successor        Bleasttime.setcalaulate (Bsteptime)        Dim time as Integer   ' calculates the time-to-machine difference time        = DateDiff ("n", Enlineinfo.ontime, Enlineinfo.offtime) + DateDiff ("n", Enlineinfo.ondate, Enlineinfo.offdate) return        bpreparetime.handletime (time)   ' responsibility chain processing, returning to the on-machine times    End FunctionEnd Class


The calling code in the U layer:

Dim Enline as New entity.lineentity with        enline            . Ondate = Linere (0). Ondate            . Ontime = Linere (0). Ontime            . Offdate = CStr (Format (now (), "Yyyy-mm-dd"))            . Offtime = CStr (Format (now (), "HH:mm:ss"))        End with        Dim basicdatalist as IList (of entity.basicdataentity)        Dim bllbasicdata As Basicdatabll = new BASICDATABLL ()        Dim enbasicdata as New entity.basicdataentity  ' call returns the basic data function, Returns the Entity collection        basicdatalist = Bllbasicdata.readbasic (enbasicdata)        ' invoke responsibility chain mode, calculate the on-machine time        enline.consumetime = Ontimecount.costtime (Basicdatalist, Enline)
Category:

This in their own personal computer room reconstruction in the integrity of the chain of responsibility model is familiar with, for the responsibility chain of learning, but also only in an initial stage, which is purely a "chain of responsibility model (or assume responsibility, or the responsibility is all pushed to the next one", It also requires that the request be received by a handler object before the end of the process, and that there is a corresponding "impure chain of responsibility pattern (allowing a request to allow a request to be partially processed by a specific processor and then passed down. Or when a specific processor finishes processing a request, the subsequent processor can continue to process the request , and a request can eventually be received by any processor object .

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.