首页 > 代码库 > 第一章.良好应用程序基石(3)

第一章.良好应用程序基石(3)

客户又通知我们,他的Guitars库中有很多相同属性的吉他,但是可能售价上不同。

现在,我们要实现的是,怎样返回库中能匹配所有属性的Guitar,并列出来。

更新Inventory.java中的search()方法:

 1 public List search(Guitar searchGuitar){
 2         List matchingGuitars = new LinkedList();
 3         for(Iterator i = guitars.iterator(); i.hasNext();){
 4             Guitar guitar = (Guitar) i.next();
 5             
 6             if(searchGuitar.getBuilder() != guitar.getBuilder())
 7                     continue;
 8             
 9             String model = searchGuitar.getModel().toLowerCase();
10             if((model != null) && (!model.equals("")) && 
11                     (!model.equals(guitar.getModel().toLowerCase())))
12                     continue;
13             
14             if(searchGuitar.getType() != guitar.getType())
15                     continue;
16             
17             if(searchGuitar.getBackWood() != guitar.getBackWood())
18                     continue;
19             
20             if(searchGuitar.getTopWood() != guitar.getTopWood())
21                     continue;
22             
23             matchingGuitars.add(guitar);
24         }
25         
26         return matchingGuitars;
27     }

更新FindGuitarTester.java:

 1 package com.headfirst.guitar;
 2 
 3 import java.util.Iterator;
 4 import java.util.List;
 5 
 6 public class FindGuitarTester {
 7     public static void main(String[] args){
 8         Inventory inventory = new Inventory();
 9         initializeInventory(inventory);
10         
11         Guitar whatErinLikes = new Guitar("", 0, Builder.FENDER, "Stratocastor", 
12                 Type.ELECTRIC, Wood.ALDER, Wood.ALDER);
13         
14         List matchingGuitars = inventory.search(whatErinLikes);
15         if(!matchingGuitars.isEmpty()){
16             System.out.println("Erin, you might like these guitars: "); 
17             for(Iterator i = matchingGuitars.iterator(); i.hasNext();){
18                 Guitar guitar = (Guitar) i.next();
19                 System.out.println("We have a " +
20                 guitar.getBuilder() + " " + guitar.getModel() + " " + 
21                 guitar.getType() + " guitar:\n " + 
22                 guitar.getBackWood() + " back and sides,\n " +
23                 guitar.getTopWood() + " top.\nYou can have it for only $" + 
24                 guitar.getPrice() + "!");
25             }
26         }else{
27             System.out.println("Sorry, Erin, we have nothing for you.");
28         }
29     }
30     
31     private static void initializeInventory(Inventory inventory){
32         inventory.addGuitar("V95693", 1499.95, Builder.FENDER, "Stratocastor", Type.ELECTRIC, Wood.ALDER, Wood.ALDER);
33         inventory.addGuitar("B1234", 1600.35, Builder.FENDER, "Stratocastor", Type.ELECTRIC, Wood.ALDER, Wood.ALDER);
34     }
35 }

技术分享

通过以上的修改,我们完成了步骤一:确认你的软件做客户要它做的事。

我们开始步骤二:运用基本的OO原则来增加软件的灵活性。

1.对象应该做其名称所指之事。

2.每个对象应该代表单一概念。

3.未使用的特性是无用的赠品。

4.任何时候,看到重复的代码,就找个地方进行封装。

Inventory.java中的search()方法,需要改进的地方就是,用户不必传入一个Guitar对象,因为Guitar里的变量不全是用来搜索的,比如价格、序号用户是不知道的。

所以,我们要从Guitar类中分割出来一个类,专门用来存储用户搜索数据。

技术分享

项目架构:

技术分享

Guitar.java:

 1 package com.headfirst.guitar;
 2 
 3 public class Guitar {
 4     private String serialNumber;
 5     private double price;
 6     private GuitarSpec guitarSpec;
 7     
 8     public Guitar(String serialNumber, double price, GuitarSpec guitarSpec){
 9         this.serialNumber = serialNumber;
10         this.price = price;
11         this.guitarSpec = guitarSpec;
12     }
13     
14     public String getSerialNumber(){
15         return serialNumber;
16     }
17     public double getPrice(){
18         return price;
19     }
20     public void setPrice(float newPrice){
21         this.price = newPrice;
22     }
23     public GuitarSpec getGuitarSpec(){
24         return guitarSpec;
25     }
26 }

GuitarSpec.java:

 1 package com.headfirst.guitar;
 2 
 3 public class GuitarSpec {
 4     Builder builder;
 5     String model;
 6     Type type;
 7     Wood backWood;
 8     Wood topWood;
 9     
10     public GuitarSpec(Builder builder, String model, Type type, Wood backWood, Wood topWood){
11         this.builder = builder;
12         this.model = model;
13         this.type = type;
14         this.backWood = backWood;
15         this.topWood = topWood;
16     }
17     
18     public Builder getBuilder(){
19         return this.builder;
20     }
21     
22     public String getModel(){
23         return this.model;
24     }
25     
26     public Type getType(){
27         return this.type;
28     }
29     
30     public Wood getBackWood(){
31         return this.backWood;
32     }
33     
34     public Wood getTopWood(){
35         return this.topWood;
36     }
37 }

Inventory.java 修改:

 1 package com.headfirst.guitar;
 2 
 3 import java.util.Iterator;
 4 import java.util.LinkedList;
 5 import java.util.List;
 6 
 7 public class Inventory {
 8     
 9     private List guitars;
10     
11     public Inventory(){
12         guitars = new LinkedList();
13     }
14     
15     public void addGuitar(String serialNumber, double price, GuitarSpec guitarSpec){
16         Guitar guitar = new Guitar(serialNumber, price, guitarSpec);
17         guitars.add(guitar);
18     }
19     
20     public Guitar getGuitar(String serialNumber){
21         for(Iterator i = guitars.iterator(); i.hasNext();){
22             Guitar guitar = (Guitar) i.next();
23             if(guitar.getSerialNumber().equals(serialNumber)){
24                 return guitar;
25             }
26         }
27         
28         return null;
29     }
30     
31     public List search(GuitarSpec searchGuitar){
32         List matchingGuitars = new LinkedList();
33         for(Iterator i = guitars.iterator(); i.hasNext();){
34             Guitar guitar = (Guitar) i.next();
35             GuitarSpec guitarSpec = guitar.getGuitarSpec();
36             
37             if(searchGuitar.getBuilder() != guitarSpec.getBuilder())
38                     continue;
39             
40             String model = searchGuitar.getModel().toLowerCase();
41             if((model != null) && (!model.equals("")) && 
42                     (!model.equals(guitarSpec.getModel().toLowerCase())))
43                     continue;
44             
45             if(searchGuitar.getType() != guitarSpec.getType())
46                     continue;
47             
48             if(searchGuitar.getBackWood() != guitarSpec.getBackWood())
49                     continue;
50             
51             if(searchGuitar.getTopWood() != guitarSpec.getTopWood())
52                     continue;
53             
54             matchingGuitars.add(guitar);
55         }
56         
57         return matchingGuitars;
58     }
59 }

FindGuitarTester.java修改:

 1 package com.headfirst.guitar;
 2 
 3 import java.util.Iterator;
 4 import java.util.List;
 5 
 6 public class FindGuitarTester {
 7     public static void main(String[] args){
 8         Inventory inventory = new Inventory();
 9         initializeInventory(inventory);
10         
11         GuitarSpec whatErinLikes = new GuitarSpec(Builder.FENDER, "Stratocastor", Type.ELECTRIC, Wood.ALDER, Wood.ALDER);
12         
13         List matchingGuitars = inventory.search(whatErinLikes);
14         if(!matchingGuitars.isEmpty()){
15             System.out.println("Erin, you might like these guitars: "); 
16             for(Iterator i = matchingGuitars.iterator(); i.hasNext();){
17                 Guitar guitar = (Guitar) i.next();
18                 GuitarSpec guitarSpec = guitar.getGuitarSpec();
19                 System.out.println("We have a " +
20                 guitarSpec.getBuilder() + " " + guitarSpec.getModel() + " " + 
21                 guitarSpec.getType() + " guitar:\n " + 
22                 guitarSpec.getBackWood() + " back and sides,\n " +
23                 guitarSpec.getTopWood() + " top.\nYou can have it for only $" + 
24                 guitar.getPrice() + "!");
25             }
26         }else{
27             System.out.println("Sorry, Erin, we have nothing for you.");
28         }
29     }
30     
31     private static void initializeInventory(Inventory inventory){
32         GuitarSpec guitarSpec1 = new GuitarSpec(Builder.FENDER, "Stratocastor", Type.ELECTRIC, Wood.ALDER, Wood.ALDER);
33         inventory.addGuitar("V95693", 1499.95, guitarSpec1);
34         inventory.addGuitar("B1234", 1600.35, guitarSpec1);
35     }
36 }

我们把Guitar类中给用户搜索用的变量,重新构造一个新的类GuitarSpec。

这样Guitar类的功能就很单一了,就是存储Guitar对象,GuitarSpec类则是为了提供给用户搜索相关信息的。

运行结果:

技术分享

第一章.良好应用程序基石(3)