In this chapter we will discuss the reuse code.
1. Why?
This point I do not elaborate, casually say can say a few reasons.
The author sums up a bit:
For the boss: reduce costs, shorten development time, and pursue profit maximization
For Teamleader: Accelerate progress, compress durations, deliver the most items in the shortest time
For the program ape: no Overtime
2. What do I do?
The author is divided into four aspects to describe:
1) At the method level:
For example: We usually need time for this thing in many places, but we may copy and paste the following code
Calendar calendar = calendar.getinstance (); int thismonth = Calendar.get (calendar.month) +1;
Get time in the code, we can actually extract some code to form the time class
/** * Processing Date Time Tool class * @author Ray */public class Dateutil {/** * @return get current calendar */public static calendar Getcalendar () {C Alendar 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 Gets the current time */public static Date Getnow () {return Getcalendar (). GetDate ();}}
Another example: in the system we need to process the data that the servlet has sent back.
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");D ate now = Dateutil.getdate (Req.getparameter ("Now ")," Yyyy-mm-dd ");
We can abstract into a class of converters
/** * Default Reader, read post submitted data * @author Raylee */public class Converter {private HttpServletRequest req = null;/* * Converter * @par Am 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.ge Tparameter (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 (getstr ING (name)). Doublevalue ();} /* * @param name * @return returns an integer */public int getinteger (String name) throws IOException {return integer.valueof (getString (n AME)). Intvalue ();} /* * @param name * @return return string */public string getString (string name) throws IOException {return GetData (name). ToString () ;} /* * @param name * @return returns GBK encoded */public string Getchinese (string name) throws IOException {return new string (GetString ( Name). GetBytes (), "GBK");}}
2) at the class level:
(1) Polymerization
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 empty");}}}
First create a bird class.
And Jack and Rose had a little 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, in fact, is the has-a relationship, which is also one of the code reuse, the two re-use Bird this class.
(2) Inheritance
Let's take a look at the following two classes
Singer:
Package Com.ray.ch01;public class Singer {private Long id;private string Name;private string Sex;public long GetId () {Retu RN 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 () {retur n 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 above two classes can be seen, everyone's code is almost the same, this time we can abstract a person's class, and then let singer and boxer to inherit his
The inheritance concept involved here will be expanded in detail in our next section.
Package Com.ray.ch01;public class Person {private long id;private string Name;private string Sex;public long GetId () {Retu RN 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 inside, and then 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, the important means of code reuse.
3) at the configuration level:
For example, we usually use the framework, usually have a configuration file, we can put these configuration files centrally managed, and then reused
4) at the business level:
For example, there is a multi-system situation in the enterprise, but in general, each system needs to have a login module, we can separate the login module, into a component, so that it can be reused.
Summary: This chapter focuses on how to reuse code.
This chapter is here. Thank you.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Understanding java-1.4 reuse code from the beginning