Scenario:
If the content of the business layer is embedded in the interface layer, we need to separate it.
Bad code taste
Myframe
/***** @ Author wumingkun * @ version 1.0.0 * @ Description */package COM. demo. refactor; import Java. util. observable; import Java. util. observer;/*** @ author wumingkun **/public class myframe {private mytext beginfield; private mytext endfield; private mytext lengthfield; Public myframe (mytext beginfield, mytext endfield, mytext lengthfield) {This. beginfield = beginfield; this. endfield = endfield; this. lengthfield = lengthfield;}/*** start value changed */private void beginchange () {calculatelength ();} /*** end value changed */private void endchange () {calculatelength ();}/*** length changed */private void lengthchange () {calculateend ();} public void onchange (INT type) {If (type = 1) {beginchange ();} else if (type = 2) {endchange () ;}else {lengthchange () ;}/ *** calculated end value */private void calculateend () {int begin = integer. parseint (this. beginfield. gettext (); int length = integer. parseint (this. lengthfield. gettext (); int end = Length + begin; this. endfield. settext (string. valueof (end);}/*** calculate the length value */private void calculatelength () {int begin = integer. parseint (this. beginfield. gettext (); int end = integer. parseint (this. endfield. gettext (); int length = end-begin; lengthfield. settext (string. valueof (length ));}}
The calculateend and calculatelength methods in this class belong to the business logic layer and should not be in the interface class.
Reconstruction means:
1. Introduce the observer Mode
2. Use the move method to move calculateend and calculatelength to the new class.
3. Establish a delegation relationship
Code after reconstruction:
Myframe
/** * * @author wumingkun * @version 1.0.0 * @Description */package com.demo.refactor;import java.util.Observable;import java.util.Observer;/** * @author wumingkun * */public class MyFrame implements Observer {@Overridepublic void update(Observable o, Object arg) {this.beginField.setText(subject.getBegin());this.endField.setText(subject.getEnd());this.lengthField.setText(subject.getLength());}private MyText beginField;private MyText endField;private MyText lengthField;Interval subject;public MyFrame(MyText beginField, MyText endField, MyText lengthField) {super();this.beginField = beginField;this.endField = endField;this.lengthField = lengthField;subject = new Interval(this.beginField.getText(),this.endField.getText(), this.lengthField.getText());subject.addObserver(this);}/** * */private void baginLost() {setBegin(beginField.getText());}/** * */private void endLost() {setEnd(endField.getText());}/** * */private void lengthLost() {setLength(lengthField.getText());}public void onchange(int type) {if (type == 1) {baginLost();} else if (type == 2) {endLost();} else {lengthLost();}}public String getBegin() {return this.subject.getBegin();}public void setBegin(String begin) {this.subject.setBegin(begin);}public String getEnd() {return this.subject.getEnd();}public void setEnd(String end) {this.subject.setEnd(end);}public String getLength() {return this.subject.getLength();}public void setLength(String length) {this.subject.setLength(length);}}
Interval
/** * @author wumingkun * @version 1.0.0 * @Description */package com.demo.refactor;import java.util.Observable;import java.util.Observer;/** * @author wumingkun * */public class Interval extends Observable {private String begin;private String end;private String length;public Interval(String begin, String end, String length) {super();this.begin = begin;this.end = end;this.length = length;}@Overridepublic synchronized void addObserver(Observer o) {super.addObserver(o);}public String getBegin() {return begin;}public void setBegin(String begin) {this.begin = begin;change(1);}public String getEnd() {return end;}public void setEnd(String end) {this.end = end;change(1);}public String getLength() {return length;}public void setLength(String length) {this.length = length;change(2);}private void change(int type) {if(type==1){calculateLength();}else {calculateEnd();}super.setChanged();super.notifyObservers();}/** * */private void calculateEnd() {int begin=Integer.parseInt(this.begin);int length=Integer.parseInt(this.length);int end=length+begin;this.end=String.valueOf(end);}/** * */private void calculateLength() {int begin=Integer.parseInt(this.begin);int end=Integer.parseInt(this.end);int length=end-begin;this.length=String.valueOf(length);}}