Pattern-engineering implementation and expansion Visitor pattern Visitor-self-test reference answer

Source: Internet
Author: User

Below we will further complicate the HR system example in the text of this chapter:

1. The personnel types of system management include three categories and four sub-categories:

A. Employees: including General employees and managers)

B. Temporary employee (Temporary)

C. External Expert (Expert)

2. management content includes three categories and eight sub-categories, and is expected to continue to increase

A. Salary: Includes base salary, seniority salary (related to the company's working life), irregular job salary increase, and employment of external experts

B. Insurance and Provident Fund: Pension Insurance and housing provident fund

C. Vacation: annual vacation, job increase vacation

3. Existing functional requirements

A. Salary:

I. Seniority salary: 50 RMB per year

Ii. Irregular job increase: employees and managers are expected to be given a salary increase of 15% and 10% respectively.

B. Insurance and Provident Fund: In order to simplify the example, the pension insurance and housing provident fund are deducted Based on 5% and 15% of the total payable income respectively.

C. holidays:

I. Annual Vacation: all employees (general employees and managers) are on a 5-day basis each year, increasing by 5 days every 5-year period.

Ii. Job increase vacation: it is planned to increase the number of common employees by three days each year based on the existing vacation, and the number of managers increases by five days each year.

Use any visitor mode ("Classic mode", "Dynamic mode", and "LINQ + delegation mode") described in this chapter to complete the above functions, and passes unit test verification.

 

Requirements:

1. The dependency should be as concise as possible

2. Implementation methods should be as concise and practical as possible

3. Easy Extension from "personnel type" and "management content"

Analysis

1. To adjust the management content and personnel types at the same time, all the two-factor dependency relationships that need to be decoupled using the. NET platform


2. To simplify implementation, use the LINQ + delegation Method

3. To facilitate modifications to the maintenance of the application project, all personnel types and management content are dynamically loaded in configuration mode.

4. In order to simplify a large number of content, a unified Facade interface is provided to coordinate behavior matching between various personnel types and management content.

 

 

Reference implementation

1. Personnel Type Definition

# Region Staff

Abstract class Person
{
Public string Name {get; set ;}
Public virtual double Income {get; set ;}
Public virtual object Clone () {return MemberwiseClone ();}
}

/// <Summary>
/// External expert
/// </Summary>
Class Expert: Person
{
Public Expert (double cost)
{
Cost = cost;
Income = Cost;
}
Public double Cost {get; set ;}
}

Abstract class Employee: Person {}

Class GeneralEmployee: Employee
{
Public GeneralEmployee (double basicSalary, int workingYears)
{
BasicSalary = basicSalary;
WorkingYears = workingYears;
}
Public double BasicSalary {get; set ;}
Public double ExtraSalary {get; set ;}
Public int WorkingYears {get; set ;}
Public int VacationDays {get; set ;}

Public override double Income {get {return BasicSalary + ExtraSalary ;}}
}

Class Temporary: Employee
{
Public double Wage {get; set ;}
Public override double Income {get {return Wage ;}}
}

Class Mananger: GeneralEmployee
{
Public Mananger (double basicSalary, int workingYears, string department)
: Base (basicSalary, workingYears)
{
Department = department;
}
Public string Department {get; private set ;}
}

# Endregion
 

2. access interface and access process definition

Class HrRuntime
{
/// <Summary>
/// All elements accept the unified entry of the Visitor
/// </Summary>
/// <Remarks>
/// Type indicates the applicable personnel category
/// Action indicates the extended functions of the Visitor
/// </Remarks>
Public IEnumerable <KeyValuePair <Type, Action <Person> Registry {get; set ;}

/// <Summary>
/// All elements
/// </Summary>
Public List <Person> Staff {get; set ;}

/// <Summary>
/// Call each applicable Visitor
/// </Summary>
Public void Visit ()
{
If (Registry. Count () = 0) return;
If (Staff. Count () = 0) return;

// Dynamically generate interfaces of traditional Visit
// VisitGeneralExpert ()
// VisitGeneralTemporary ()
// VisitGeneralGeneralEmployee ()
// VisitGeneralManager ()
Var visitorsByType = Registry. GroupBy (x => x. Key );

// Dynamically execute the Visit () Process
Staff. forEach (x => Registry. where (a =>. key = x. getType ()). toList (). forEach (a =>. value (x )));
}

Public T FindClone <T> (string name)
Where T: Person
{
Return (T) (Staff. FirstOrDefault (x => string. Equals (x. Name, name). Clone ());
}
}
 

3. unit tests to verify the access effectiveness between various management measures and various personnel

 

HrRuntime runtime;
Const double MaxMistake = 0.1;

[TestInitialize]

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.