Step by step. NET code refactoring study note 10

Source: Internet
Author: User
Tags class manager

Step by step. NET code refactoring learning notes Series

Html "> step by step. NET code refactoring Study Notes 1. Why code refactoring?

Step by step. NET code refactoring Study Notes II. Extraction Method (Extract Method)

Step by step. NET code refactoring study note 3. Inline Method)

Step by step. NET code refactoring Study Notes 4. Temporary variables (Temporary Variable)

Step by step. NET code refactoring study note 5. decomposition functions And replacement algorithms (Replace Method And Substitute Algorithm)

Step by step. NET code refactoring study note 6. Move Method And Move Field)

Step by step. NET code refactoring study note 7

Step by step. NET code refactoring learning notes 8

Step by step. NET code refactoring study note 9

1. Replace Type Code with Subclasses (Replace Type Code with subclass)

Motivation)

Replace this type code with a subclass. If the type code it faces does not affect the behavior of the host Class, you can use Replace Type Code with Class to process them. However, if the type code affects the behavior of the host class, the best way is to use the polymorphism industry to handle changing behavior.

Example

View sourceprint? 01 public class Employee

02 {

03 private int _ type;

04 public static int ENGINEER = 0;

05 public static int SALEMAN = 1;

06 public static int MANAGER = 2;

07

08 public Employee (int type)

09 {

10 _ type = type;

11}

12}

 

Change

View sourceprint? 01 public class Employee

02 {

03 private int _ type;

04 public static int ENGINEER = 0;

05 public static int SALEMAN = 1;

06 public static int MANAGER = 2;

07

08 public Employee (int type)

09 {

10 _ type = type;

11}

12

13 public int Type

14 {

15 get {return _ type ;}

16 set {_ type = value ;}

17}

18

19

20}

21

22 public class ENGINEER: Employee

23 {

24 public int GetType ()

25 {

26 return Employee. ENGINEER;

27}

28}

29

30 public class SALEMAN: Employee

31 {

32 public int GetType ()

33 {

34 return Employee. SALEMAN;

35}

36}

37

38 public class MANAGER: Employee

39 {

40 public int GetType ()

41 {

42 return Employee. MANAGER;

43}

44}

45

46 public class Factory: Employee

47 {

48 public Employee Create (int type)

49 {

50 switch (type)

51 {

52 case ENGINEER:

53 return new ENGINEER ();

54 case SALEMAN:

55 return new SALEMAN ();

56 case MANAGER:

57 return new MANAGER ();

58 default:

59 throw new ExecutionEngineException ("Incorrect type code value ");

60}

61}

62}

 

2. Replace Type Code with State/Strategy (Replace Type Code with State/Strategy)

Motivation)

Replace type code with State object (an object specifically used to describe the State.

Example

View sourceprint? 01 public class Employee

02 {

03 private int _ type;

04 public static int ENGINEER = 0;

05 public static int SALEMAN = 1;

06 public static int MANAGER = 2;

07

08 public Employee (int type)

09 {

10 _ type = type;

11}

12

13 public int Type

14 {

15 get {return _ type ;}

16 set {_ type = value ;}

17}

18

19 public int PayAmount ()

20 {

21 switch (_ type)

22 {

23 case ENGINEER:

24 return 100;

25 case SALEMAN:

26 return 1000;

27 & n

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.