Reconstruction 2. Replace Type Code with Class (Replace Type Code with Class), 2. replacecode

Source: Internet
Author: User

Reconstruction 2. Replace Type Code with Class (Replace Type Code with Class), 2. replacecode
Scenario

In a class, we often define some type codes, such:

public static final int INVALID=0;public static final int VALID=1;

We can convert these values into a class.


Prerequisites:

Only when the type code is pure data (the type code will not cause behavior changes in the Switch statement) Can you replace it with a class


Code before modification:

Student

Package com. demo. refactor. codetype. before; public class Student {private int id; private String name;/*** status: 0, valid, 1 is INVALID */private int status; public static final int INVALID = 0; public static final int VALID = 1; public Student (int id, String name, int status) {super (); this. id = id; this. name = name; this. status = status;} public int getId () {return id;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getStatus () {return status;} public void setStatus (int status) {this. status = status ;}@ Overridepublic String toString () {return "Student [id =" + id + ", name =" + name + ", status = "+ status +"] ";}}

Test:

Package com. demo. refactor. codetype. before; public class StudentTest {/*** @ param args */public static void main (String [] args) {Student student = new Student (1, "James", Student. VALID); System. out. println (student );}}



After modification:

StatusCode

package com.demo.refactor.codetype.after;public class StatusCode {public static final StatusCode invalid=new StatusCode(0);public static final StatusCode valid=new StatusCode(1);public static final StatusCode[] codes={invalid,valid};private int code;private StatusCode(int code) {this.code = code;}/** * @param index  * @return */public static StatusCode create(int index){return codes[index];}public int getCode() {return code;}public void setCode(int code) {this.code = code;}@Overridepublic String toString() {return "StatusCode [code=" + code + "]";}}


Student

package com.demo.refactor.codetype.after;public class Student {private int id;private String name;private StatusCode code;public Student(int id, String name, StatusCode code) {this.id = id;this.name = name;this.code = code;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getCode() {return code.getCode();}public void setCode(int index) {this.code =StatusCode.create(index);}@Overridepublic String toString() {return "Student [id=" + id + ", name=" + name + ", code=" + code + "]";}}

StudentTest

Package com. demo. refactor. codetype. after; public class StudentTest {/*** @ param args */public static void main (String [] args) {Student stu = new Student (1, "Li Si", StatusCode. invalid); System. out. println (stu );}}






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.