Spring: A developer's notebook notes and summary (1)

Source: Internet
Author: User
/**
Author: willpower
Source: rifoo Technology (http://www.rifoo.com)
Time: 2005-12-25
Remarks: Reprinted please keep the above statement
**/

This book is mainly about code and practice. It builds a roller coaster ordering system and sports stores can manage and order their roller coasters. The author first hardcoded two dependent classes commandlineview. Java and javasabike. java. Let's first look at the source code:

Example 1-1. Bike. Java
Public class bike {
Private string manufacturer;
Private string model;
Private int frame;
Private string serialno;
Private double weight;
Private string status;

Public bike (string manufacturer, string model, int frame,
String serialno, double weight, string status ){

This. Manufacturer = manufacturer;
This. Model = model;
This. Frame = frame;
This. serialno = serialno;
This. Weight = weight;
This. Status = status;
}

Public String tostring (){
Return "bike:" +
"Manufacturer --" + manufacturer +
"/N: Model --" + model +
"/N: frame --" + frame +
"/N: serialno --" + serialno +
"/N: weight --" + weight +
"/N: status --" + status +
"./N ";
}

Public String getmanufacturer () {return manufacturer ;}

Public void setmanufacturer (string manufacturer ){
This. Manufacturer = manufacturer;
}

Public String GetModel () {return model ;}

Public void setmodel (String Model) {This. Model = model ;}

Public int getframe () {return frame ;}

Public void setframe (INT frame) {This. Frame = frame ;}

Public String getserialno () {return serialno ;}

Public void setserialno (string serialno) {This. serialno = serialno ;}

Public double getweight () {return weight ;}

Public void setweight (double weight) {This. Weight = weight ;}

Public String getstatus () {return status ;}

Public void setstatus (string status) {This. Status = status ;}
}

It can be seen that bike. Java is a common JavaBean used to express a roller coaster entity, including the manufacturer, model, specification, serial number, weight and status tree.

Example 1-2. abstrabike. Java
Import java. util .*;
Public class extends abike {

Private string storename;
Final list bikes = new arraylist ();

Public writable abike (string storename ){
This. storename = storename; // store name
// Add a roller coaster to the array
Bikes. Add (new bike ("Shimano", "roadmaster", 20, "11111", 15,
"Fair "));
Bikes. Add (new bike ("cannondale", "F2000 XTR", 18, "22222", 12,
"Excellent "));
Bikes. Add (new bike ("Trek", "6000", 19, "33333", 12.4,
"Fair "));
}

Public String tostring () {return "incluabike:" + storename ;}
// Get all roller coasters
Public list getbikes () {return bikes ;}
// Get a roller coaster with a serial number
Public bike getbike (string serialno ){
Iterator iter = bikes. iterator ();
While (ITER. hasnext ()){
Bike bike = (bike) ITER. Next ();
If (serialno. Equals (bike. getserialno () return bike;
}
Return NULL;
}
}

This class describes how to rent a roller coaster. storename is the name of the incoming store. It is a facade for the client. It stores all the rented roller coasters in a list array and provides two external Methods: getbikes and getbike, the client can know all the roller coasters currently rented and what a serial number of roller coasters are.

Let's look at how the user interface is called:
Example 1-3. commandlineview. Java
Import java. util .*;
Public class commandlineview {
Private rentabike;
Public commandlineview () {symbol abike = new symbol abike ("Bruce's bikes ");}

Public void printallbikes (){
System. Out. println (abstrabike. tostring ());
// Call the facade Method
Iterator iter = effecabike. getbikes (). iterator ();
While (ITER. hasnext ()){
Bike bike = (bike) ITER. Next ();
System. Out. println (bike. tostring ());
}
}

Public static final void main (string [] ARGs ){
Commandlineview CLV = new commandlineview ();
CLV. printallbikes ();
}
}

Running result:

C:/rentabikeapp/out> JAVA commandlineview

Rentabike: Bruce's bikes
Bike: manufacturer -- Shimano
: Model -- roadmaster
: Frame -- 20
: Serialno -- 11111
: Weight-15.0
: Status -- fair.

Bike: manufacturer -- cannondale
: Model -- F2000 XTR
: Frame -- 18
: Serialno -- 22222
: Weight-12.0
: Status -- excellent.

Bike: manufacturer -- trek
: Model -- 6000
: Frame -- 19
: Serialno -- 33333
: Weight-12.4
: Status -- fair.

Do you see the problem?
On the facade, mongoabike creates a rental roller coaster in a store. If a new roller coaster is introduced, you need to manually modify the code of mongoabike, for example, we add bike2.java, attributes, and bike. java is different. We also need to instantiate It In The rentabike class, which causes hard encoding.
2. This bike. Java model is difficult to test, because the bikes array is fixed.
3. This user interface is strongly coupled with the facade. They have a hard-coded dependency. Pay attention to the two lines of code in commandlineview. Java:
Private rentabike;
Public commandlineview () {symbol abike = new symbol abike ("Bruce's bikes ");}

Therefore, although this program can complete a simple operation of renting a roller coaster, it is not easy to maintain and expand.

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.