Java-1.4 code reuse
In this section, we will discuss how to reuse code.
1. Why?
I am not going to elaborate on this. I can give you a few reasons for this.
I have summarized the following:
For the boss: reduce costs, shorten development time, and maximize profits
For teamleader: accelerate the progress, compress the construction period, and deliver the most projects in the shortest time
For programmers: No overtime
2. How?
I will describe it in four aspects:
1) At the method level:
For example, we usually need time in many places, but we may copy and paste the following code.
Calendar calendar = Calendar.getInstance();int thisMonth = calendar.get(Calendar.MONTH)+1;
Get the time in all parts of the Code. In this case, we can extract some code to form a time class.
/*** Processing date and time tool class ** @ author ray */public class DateUtil {/*** @ return get current Calendar */public static Calendar getCalendar () {Calendar calendar = Calendar. getInstance (); return calendar;}/*** @ return get this year */public static int getThisYear () {return getCalendar (). get (Calendar. YEAR);}/*** @ return get this month */public static int getThisMonth () {return getCalendar (). get (Calendar. MONTH) + 1;}/*** @ return get current time */public static Date getNow () {return getCalendar (). getDate ();}}
For example, in the system, we need to process the data transmitted by servlet.
double amount = Double.valueOf(req.getParameter(“amount”)).doubleValue();int count = Integer.valueOf(req.getParameter(“count”)).intValue();String name = new String(req.getParameter(“name”).getBytes(),GBK);Date now = DateUtil.getDate(req.getParameter(“now”),yyyy-MM-dd);
We can abstract it into a converter class.
/*** The default reader reads data submitted by POST * @ author raylee */public class Converter {private HttpServletRequest req = null; /** Converter * @ param HttpServletRequest */public Converter (HttpServletRequest req) throws IOException {this. req = req;}/** @ param name * @ return the data of Object */public Object getData (String name) throws IOException {Object data = req. getParameter (name); if (data = null) {throw new IOException (converter failed);} return data ;} /** @ param name * @ return returns double */public double getDouble (String name) throws IOException {return Double. valueOf (getString (name )). doubleValue ();}/** @ param name * @ return returns an Integer */public int getInteger (String name) throws IOException {return Integer. valueOf (getString (name )). intValue ();}/** @ param name * @ return String */public String getString (String name) throws IOException {return getData (name ). toString ();}/** @ param name * @ return returns GBK encoding */public String getChinese (String name) throws IOException {return new String (getString (name ). getBytes (), GBK );}}
2) at the class level:
(1) Aggregation
Let's look at the following example:
Package com. ray. ch01; public class Bird {private String name; public String getName () {return name;} public void setName (String name) {if (name! = Null) {this. name = name;} else {System. out. println (name cannot be blank );}}}
Create a Bird class first.
Then jack and rose raised a bird at the same time.
package com.ray.ch01;public class Jack {private Bird bird;public Bird getBird() {return bird;}public void setBird(Bird bird) {this.bird = bird;}}
package com.ray.ch01;public class Rose {private Bird bird;public Bird getBird() {return bird;}public void setBird(Bird bird) {this.bird = bird;}}
Aggregation is actually the has-a relationship. The above is also one of code reuse. The two reuse the Bird class.
(2) Inheritance
Let's look at the two classes below
Singer:
package com.ray.ch01;public class Singer {private long id;private String name;private String sex;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}@Overridepublic String toString() {return id: + getId() + ;name: + getName() + ;sex: + getSex();}public void sing() {}}
Boxer:
package com.ray.ch01;public class Boxer {private long id;private String name;private String sex;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}@Overridepublic String toString() {return id: + getId() + ;name: + getName() + ;sex: + getSex();}public void fight() {}}
From the two classes above, we can see that everyone's code is almost the same. At this time, we can abstract a Person's class, and then let singer and boxer inherit from him.
The inheritance concepts involved here will be detailed in the next chapter.
package com.ray.ch01;public class Person {private long id;private String name;private String sex;public long getId() {return id;}public void setId(long id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}@Overridepublic String toString() {return id: + getId() + ;name: + getName() + ;sex: + getSex();}}
We put the common code in it, and the other two implementation classes will implement their own unique code.
package com.ray.ch01;public class Boxer extends Person {public void fight() {}}
package com.ray.ch01;public class Singer extends Person {public void sing() {}}
Inheritance: is-a relationship, an important means of code reuse.
3) at the configuration level:
For example, we usually use the framework and usually have configuration files. We can centrally manage these configuration files and reuse them.
4) at the business level:
For example, there are multiple systems in the enterprise, but generally every system requires a login module. We can extract this login module separately and turn it into a component so that it can be reused.
Summary: This chapter mainly discusses how to reuse code.
This chapter is here. Thank you.