Rebuilding notes-refining class, restructuring notes Refining
This article is in the study of the summary, welcome to reprint but please note the Source: http://blog.csdn.net/pistolove/article/details/43059759
In the previous article, we introduced "moving fields ". This article will introduce the refactoring method of "extraction class.
Let's learn about This refactoring method.
Open door
It is found that a class has done something that should be done by two classes.
Solution: create a new class to move related fields and functions from the old class to the new class.
Motivation
We have heard more or less the following teachings: A class should be a clear abstraction to deal with specific responsibilities. However, in actual work, classes will continue to grow and expand. You will add some functions here and some data here. When you add a new responsibility to a class, you will feel that it is not worthwhile to separate a separate class for this responsibility. As a result, this class becomes too complex as the number of responsibilities increases. Soon, the class will become messy, and you will not want to manage it any more.
Such classes often contain a large amount of functions and data, which is too big to be understood. In this case, you need to consider which parts can be separated and separate them into a separate class. If some data and some functions always appear together, and some data often changes at the same time or even depend on each other, this means that they should be separated for processing. A useful test is to ask yourself: if some fields and functions are removed, what will happen? Will other fields and functions become meaningless?
Another important thing to note is the subclass method of classes. If you find that subclass only affects some of the features of the class, or if you find that some features need to be subclass in one way, and other features need to be subclass in another way, it means that the original class needs to be decomposed.
Practice
(1) Determine how to break down the responsibilities of the class. (2) create a new class to show the responsibilities separated from the old class (if the remaining responsibilities of the old class do not match the old class name, rename the old class ).
(3) establish a connection to access the new class from the old class. (A two-way connection may be required, but do not create a connection between the new class and the old class before you actually need it)
(4) For each field to be moved, use "move field" to move it.
(5) Compile and test each time after migration.
(6) use the "moving method" to move necessary functions to the new class. First move the higher-level functions (that is, the functions that are "called by other functions" are redundant "called by other functions"), and then move the higher-level functions.
(7) Compile and test each time after migration.
(8) Check to streamline the interfaces of each class. (If you have established a two-way connection, check whether it should be changed to a one-way connection)
(9) Decide whether to publish a new class. If you decide to publish it, you need to decide whether to make it a reference object or an immutable object.
Example
We start with a simple Person class:
// Extract class Person {private String _ name; private String _ officeAreaCode; private String _ officeNumber; public String get_name () {return _ name;} public String getTelephoneNumber () {return ("(" + _ officeAreaCode + ")" + _ officeNumber);} public String get_officeAreaCode () {return _ officeAreaCode;} public void set_officeAreaCode (String areaCode) {_ officeAreaCode = areaCode;} public String get_officeNumber () {return _ officeNumber;} public void set_officeNumber (String number) {_ officeNumber = number ;}}In this example, the behaviors related to phone numbers can be separated into an independent class. First, you need to define a TelephoneNumber class to represent the concept of "phone number:
class TelephoneNumber {}Then establish a connection from Person to TelephoneNumber:
class Person {//...private TelephoneNumber _officeTelephone = new TelephoneNumber();}Now, you can use the "move field" method to move a field:
class TelephoneNumber {private String _areaCode;public String get_AreaCode() {return _areaCode;}public void set_AreaCode(String areaCode) {_areaCode = areaCode;}}class Person {//......private TelephoneNumber _officeTelephone = new TelephoneNumber();public String getTelephoneNumber() {return ("(" + get_officeAreaCode() + ")" + _officeNumber);}public String get_officeAreaCode() {return _officeTelephone.get_AreaCode();}public void set_officeAreaCode(String areaCode) {_officeTelephone.set_AreaCode(areaCode);}}Then, you can move other fields and use the "moving function" method to move related functions to the TelephoneNumber class:
class Person {//......private TelephoneNumber _officeTelephone = new TelephoneNumber();private String _name;public String get_name() {return _name;}public String getTelephoneNumber() {return _officeTelephone.getTelephoneNumber();}public TelephoneNumber getOfficeTelephone() {return _officeTelephone;}}class TelephoneNumber {private String _areaCode;private String _number;public String get_AreaCode() {return _areaCode;}public String getTelephoneNumber() {return ("(" + _areaCode + ")" + _number);}public void set_AreaCode(String areaCode) {_areaCode = areaCode;}public String getNumber() {return _number;}public void setNumber(String number) {_number = number;}}The next step is to decide whether to publish this new class to the user? You can delegate the phone number-related functions in Person to TelephoneNumber to completely hide this new class. You can also publish it directly to users. It can also be made public to some users (users in the same package), rather than other users.
This article mainly introduces the refactoring method-refining class. This refactoring method is also frequently used during development. Think about moving some features of a class to another new class during development, clearly differentiate their responsibilities, which facilitates development and maintenance. Refining is a common technique for improving concurrent programs. Because of this, the two extracted classes can be locked separately. However, if you do not need to lock two objects, you do not have. Here, we will also face transaction issues when we make sure that two objects are locked at the same time. In this way, there will be risks. Finally, I hope this article will help you. If you have any questions, please leave a message. Thank you. (PS: the next article will introduce refactoring notes-link classes)
Rebuild note articles
Rebuild notes-getting started
Rebuilding notes-bad taste of code (I)
Rebuilding notes-bad taste of Code (Part 2)
Rebuilding notes -- Building a test body
Rebuilding notes -- refining Functions
Rebuilding notes-inline functions
Refactoring notes-inline temporary variables
Rebuilding notes -- replacing temporary variables with queries
Refactoring notes -- Introducing explanatory variables
Rebuilding notes -- breaking down temporary variables
Rebuild notes -- remove value assignment to parameters rebuild notes -- shift function
Rebuilding notes -- moving fields and restructuring notes -- Refining