Refactoring to patterns -- Creation Method

Source: Internet
Author: User

Understanding:

In a class, constructors are often overloaded. if a class has 10 fields, it may have many different constructors which provide convenience to some extent, for example, you can choose the constructors you need to construct objects. It is not appropriate to create an example at Will :)

 

Public class people
{
Private string name;
Private string sex;
Private int age;
Private string address;
Private double weight;

Public people (string _ name, string _ sex, int _ age, string _ address, double _ weight)
{
This. Name = _ name;
This. Sex = _ sex;
This. Age = _ age;
This. Address = _ address;
This. Weight = _ weight;
}

Public people (string _ name)

{

This. Name = _ name;

}

Public people (string _ name, int _ age)

{

This. Name = _ name;

This. Age = _ age;

}

.......
}

 

There is such a pile of constructor for the customerProgramPersonnel, it is difficult to determine which method to use. Maybe this is not bad. The most important thing is:

Public people (string _ name, string _ sex) and Public people (string _ name, string address) cannot coexist.

This is not a good idea. I use the two constructors most. Why can't they coexist? ---- You may be angry!

For the above reasons, I want refactoring!

Method: extract all constructors and use them as a Stati method. The method can be easily understood!

Public static people createpeoplebysex (string _ name, string _ sex );

Public static people createpeoplebyaddress (string _ name, string _ address)

......

Note that you must provide a method that includes all filed:

Public static people createpeople (string _ name, string _ sex, int _ age, string _ address, double _ weight );

Basically completed.

There is a skip, which actually contains all filed constructors and should not be deleted. It changes its access level to private or protected.

We can see the new people class:

Public class people
{
Private string name;
Private string sex;
Private int age;
Private string address;
Private double weight;

Public people (string _ name, string _ sex, int _ age, string _ address, double _ weight)
{
This. Name = _ name;
This. Sex = _ sex;
This. Age = _ age;
This. Address = _ address;
This. Weight = _ weight;
}

Public static people createpeople (string _ name, string _ sex, int _ age, string _ address, double _ weight)
{
Return new people (_ name, _ sex, _ age, _ address, _ weight );
}
Public static people createpeoplebyname (string _ name)
{
Return new people (_ name, "boy", 0, "", 0.0 );
}

Public static people createpeopelbysex (string _ name, string _ sex)
{
Return new people (_ name, _ sex, 0, "", 0.0 );
}
Public static people createpeoplebyweight (string _ name, string _ sex, double _ weight)
{
Return new people (_ name, _ sex, 0, "", _ weight );
}

Public void dowork ()

{

// Do the work

}
}

/// Remind you to think about the question: what is the core work of this class?

It should be a job to do. But the static constructor of Ia replaces its star position!

Therefore, we need to transfer this create behavior again! Encapsulate such a group of creation behaviors through a factory. However, this factory is different from the gof factory. Very, very simple factory.

Public class peoplefactory
{
Public static people createpeople (string _ name, string _ sex, int _ age, string _ address, double _ weight)
{
Return new people (_ name, _ sex, _ age, _ address, _ weight );
}
Public static people createpeoplebyname (string _ name)
{
Return new people (_ name, "boy", 0, "", 0.0 );
}

Public static people createpeopelbysex (string _ name, string _ sex)
{
Return new people (_ name, _ sex, 0, "", 0.0 );
}
Public static people createpeoplebyweight (string _ name, string _ sex, double _ weight)
{
Return new people (_ name, _ sex, 0, "", _ weight );
}
}

That's all! CustomerCodeCall me like this:

Public static void main ()
{
People me = lelefactory. createpeoplebyname ("flyingchen ");

Me. dowork ();
}

 

// 07/01/15

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.