Reconstruction Method-Simplified function call [5], reconstruction method-Simplified Function

Source: Internet
Author: User

Reconstruction Method-Simplified function call [5], reconstruction method-Simplified Function

Total returned directory

Directory of this section

  • Hide Method (hidden function)
  • Replace Constructor with Factory Method (Replace constructors with Factory functions)
11 Hide Method (hidden function) Overview

There is a function that has never been used by any other class.Set this function to private.

Motivation

Refactoring often leads you to modify the visibility of a function. It is easy to imagine how to increase the visibility of a function: Another type requires a function, so you must increase the visibility of the function.

When you remove a call to a function from another class, you should check whether it is possible to reduce the visibility of the function (to make it private ).

12 Replace Constructor with Factory Method (Replace constructors with Factory functions) Overview

You want to create an object instead of simply constructing it.Replace the constructor with a factory function..

Motivation

The most obvious motivation for using this refactoring method is to replace the type code with the factory function during the subclass derivation process. You may often need to create an object based on the Type Code. Now, you have to add subclass to the creation list, and the subclass is also created based on the type code. However, since the constructor can only return a single type of objects, You need to replace the constructor with a factory function.

Example: Create an object based on an integer (actually a type code)
class Employee{    public static int ENGINEER = 0;    public static int SALESMAN = 1;    public static int MANAGER = 2;    private int _type;    public Employee(int type)    {        _type = type;    }}

I want to provide different subclasses for the Employee and give them corresponding type codes. Therefore, you need to create a factory function:

public static Employee Create(int type){    return new Employee(type);}

Then, modify all the call points of the constructor, change them to the newly created factory function, and declare the constructor as private:

Employee eng=Employee.Create(Employee.ENGINEER);
private Employee(int type){    _type = type;}
Example: Create a subclass object based on a string

So far, no substantial gains have been made. The advantage is that the "receiver of the object creation request" and "Class of the created object" are separated. After converting the type code to the Child class of Employee, you can use the factory function to hide these child classes to users:

public static Employee Create(int type){    switch (type)    {        case 0:            return new Engineer();        case 1:            return new Salesman();        case 2:            return new Manager();        default:            throw new ArgumentException("Incorrect type code value.");    }}

Unfortunately, there is a switch statement. If you add a new subclass, you must update the switch statement here.

A good way to bypass this switch statement is to use reflection. The first thing to do is to modify the parameter type. First, create a function to receive a string parameter:

public static Employee Create(string name){    try    {        var namespaceStr = MethodBase.GetCurrentMethod().DeclaringType.Namespace;        var t = Type.GetType(namespaceStr + "." + name);        return Activator.CreateInstance(t) as Employee;    }    catch (Exception ex)    {        throw new ArgumentException("unable to instantiate " + name);    }}

Modify the caller and run the following statements:

Employee eng = Employee.Create(Employee.ENGINEER);

To:

Employee eng = Employee.Create("Engineer");

After modification, you can remove the "Create () function int version.

Now, when you need to add a new Employee subclass, you do not need to update the Create () function. However, the compilation period check is lost, so that a small spelling error may cause a runtime error. If it is necessary to prevent runtime errors, use clear functions to create objects (see the next example ). In this way, a new function must be added every time a subclass is added. This is the flexibility sacrificed for type security.

Example: To explicitly create a subclass of a function

If there are only a few child classes and they do not change any more, it is useful to explicitly hide the child classes of a function. Assume there is a Person class, which has two subclasses: Male and Female. First, define a factory function for each subclass in the base class:

class Person{    public static Person CreateMale()    {        return new Male();    }    public static Person CreateFemale()    {        return new Female();    }}

Then, you can call the following:

Person kent = new Male();

Replace:

Person kent = Person.CreateMale();
Summary

The factory function is the core function of the simple factory mode, implementing the separation of object creation and use.

 

To Be Continued ......

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.