C # exploring the nature of delegation 8. introducing strategic models

Source: Internet
Author: User

// Introduce the rule mode to separate work
// Apply dependency injection to allow the "execution" to be completed by the injected instance.
// In this way, the new work is completed independently, where to use and where to modify
Using System;
Using System. Collections. Generic;
Using System. Text;

Namespace ConsoleApplication1.EighthTest
{
Class EighthTest
{
Public void DoTest ()
{
Employee Wei = new employee ();

Wei. Work = new operations World of Warcraft ();
Wei. Execute the work ();
Console. ReadLine ();

Wei. Work = new handling industry competition ();
Wei. Execute the work ();
}
}

Class employees
{
Public work policy work;

Public void ()
{
Console. WriteLine (work. Execute ());
}

}

Interface Working Policy
{
String execution ();
}

Class operation World of Warcraft: Work Strategy
{
String leleapplication1.eighthtest. Work policy. Execute ()
{
StringBuilder work content = new StringBuilder ();
Work content. AppendLine ("arrange device procurement ");
Work content. AppendLine ("Recruitment customer service, induction training ");
Work content. AppendLine ("advertising ");
Work content. AppendLine ("game market ");
Work content. AppendLine ("launch activity ");
Work content. AppendLine ("............ ");
Return work content. ToString ();
}
}

Class processing industry competition: Work Strategy
{
String leleapplication1.eighthtest. Work policy. Execute ()
{

StringBuilder work content = new StringBuilder ();
Work content. AppendLine ("investigating competitors ");
Work content. AppendLine ("fight ");
Return work content. ToString ();
}
}

// This design seems very clever, but one thing that happened once caused me to dislike derivative things for years.
// Go to the site at a time to maintain projects developed by others. If an error occurs in the program, you need to modify it.
// The trigger error is very simple. Click a button to report an error. I will find it from the Click event of this button.
// In this example, right-click "Wei. Work" in the test code and select to go to the definition. The actual execution is not redirected.
// The program can hardly be maintained by others. A simple form is accompanied by several. cs and an operation for adding records to the table,
// You cannot find the Insert into statement.
// Therefore, if the derived technology is used, you must write the design instructions.
// Otherwise, I would rather see a long if... else, which at least gives me the chance to find the place to execute.
// The Problem of Difficult Derivative maintenance has always plagued me. If not, do not derive.

// There is no delegate here, but another technology-interface is derived. Some people call this application a policy, and others call it a service.
}

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.