首页 > 其他 > 详细

面向对象应用(一)

时间:2014-05-24 01:27:16      阅读:332      评论:0      收藏:0      [点我收藏+]

1.被客户不断变化的需求 “折磨”

 

第一次需求

          bubuko.com,布布扣

bubuko.com,布布扣
class Printer{
    void Open(){
        System.out.println("Open");
    }
    
    void Close(){
        System.out.println("Close");
    }
    
    void Print(String s){
        System.out.println("Print--->" + s);
    }
}
bubuko.com,布布扣
bubuko.com,布布扣
1 class Test{
2     public static void main(String args []){
3         Printer printer = new Printer();
4         printer.Open();
5         printer.Print("abc");
6         printer.Close();
7     }
8 }
bubuko.com,布布扣

            bubuko.com,布布扣

 

 

第二次需求

                     bubuko.com,布布扣          

bubuko.com,布布扣
 1 class HPPrinter{
 2     void Open(){
 3         System.out.println("Open");
 4     }
 5     
 6     void Close(){
 7         System.out.println("Close");
 8     }
 9     
10     void Print(String s){
11         System.out.println("Print--->" + s);
12     }
13 }
bubuko.com,布布扣
bubuko.com,布布扣
 1 class CannonPrinter{
 2     void Open(){
 3         System.out.println("Open");
 4     }
 5     
 6     void Close(){
 7         this.Clean();
 8         System.out.println("Close");
 9     }
10     
11     void Print(String s){
12         System.out.println("Print--->" + s);
13     }
14     
15     void Clean(){
16         System.out.println("Clean");
17     }
18 }
bubuko.com,布布扣
bubuko.com,布布扣
 1 class Test{
 2     public static void main(String args []){
 3         int flag = 0;
 4         
 5         if( flag == 0){
 6             HPPrinter hpprinter = new HPPrinter();
 7             hpprinter.Open();
 8             hpprinter.Print("abc");
 9             hpprinter.Close();
10         }
11         else if( flag == 1){
12             CannonPrinter cannonprinter = new CannonPrinter();
13             cannonprinter.Open();
14             cannonprinter.Print("abc");
15             cannonprinter.Close();
16         }
17     }
18 }
bubuko.com,布布扣

              bubuko.com,布布扣

              

第二次需求优化(消除重复代码)         

bubuko.com,布布扣
 1 class Printer{
 2     void Open(){
 3         System.out.println("Open");
 4     }
 5     
 6     void Close(){
 7         System.out.println("Close");
 8     }
 9     
10     void Print(String s){
11         System.out.println("Print--->" + s);
12     }
13 }
bubuko.com,布布扣

 

 1 class HPPrinter extends Printer{2 }

bubuko.com,布布扣
 1 class CannonPrinter extends Printer{
 2     void Close(){
 3         this.Clean();
 4         super.Close();
 5     }
 6         
 7     void Clean(){
 8         System.out.println("Clean");
 9     }
10 }
bubuko.com,布布扣
bubuko.com,布布扣
 1 class Test{
 2     public static void main(String args []){
 3         int flag = 1;
 4         
 5         if( flag == 0){
 6             HPPrinter hpprinter = new HPPrinter();
 7             hpprinter.Open();
 8             hpprinter.Print("abc");
 9             hpprinter.Close();
10         }
11         else if( flag == 1){
12             CannonPrinter cannonprinter = new CannonPrinter();
13             cannonprinter.Open();
14             cannonprinter.Print("123");
15             cannonprinter.Close();
16         }
17     }
18 } 
bubuko.com,布布扣

 

             bubuko.com,布布扣

       

              

                 

 

面向对象应用(一),布布扣,bubuko.com

面向对象应用(一)

原文:http://www.cnblogs.com/iMirror/p/3736861.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!