KEMBAR78
20070329 Object Oriented Programing Tips | PDF
   M



public class Shape{
  protected int h,w;
  public void setWidth(int w){
           this.w = w;
  }
  public void setHeight(int h){
           this.h = h;                   →
  }
}
public class Rectangele extends Shape{
}

public class Square extends Rectangle{
  protected int h,w;
  public void setWidth(int w){
           this.w = w;                   →
           this.h = h;
  }
  public void setHeight(int h){
           this.w = w;
           this.h = h;
  }
}
LSP
public class Shape{
  protected int h,w;
                                         ArrayList<Rectangele> list
  public void setWidth(int w){                  = new ArrayList<Rectangele>();
           this.w = w;
  }
  public void setHeight(int h){          for(Rectangler r : list){
  }
           this.h = h;                          r.setWidth(20);
}                                               r.setHeight(10);
                                         }
public class Rectangele extends Shape{
}

public class Square extends Rectangle{
  protected int h,w;
  public void setWidth(int w){
           this.w = w;
           this.h = h;
  }                                      List        Square
  public void setHeight(int h){
           this.w = w;
           this.h = h;
  }
}
public class User{
   public void dosth(Shape s){                     public Interface Shape{
      s.dosth();                                      public void dosth();
   }                                               }
}


       public class Rectangle implements Shape{    public class Square implements Shape{
          public void dosth(){                        public void dosth(){
             System.out.println(“do something”);         System.out.println(“do something”);
          }                                           }
       }                                           }




                                  Light Weight
                   POJP          Plain Old Java Object


   Gof
   23        new



         →
   Temprate Method
   Strategy

   Factory Method

   Command
   State
   Null Object
Template Method
public abstract class DBInterface{                     public class OracleInterface extends DBInterface{
  public Date getData(){                                 protected void init(){
     init();                                                  //
     Data data = getData();
     exit();
     return data;                                          }
  }                                                        protected Data getData(){
   protected abstract void init();                             //
   protected abstract Data getData();                      }
   protected abstract void exit();                         protected void exit(){
}                                                              //


                                                         }
                                        extends        }ublic class FireBirdInterface extends DBInterface{
                                                       p
                                                          ////
                                                       }
                                   Data



                                                 DBInterface
             =
      =public
Strategy
    public Interface DBInterface{               public class OracleInterface implements DBInterface{
      public void init();                         public void init(){
      public Data getData();                           //
      public void exit();                           }
    }                                               public Data getData(){
                                                       //
                                                    }
                           implements               public void exit(){
                                                       //
                                                    }
                                                }


                                                public class FireBirdInterface implements DBInterface{
                                                   ////
                                                }

    Template



                                       DBInterface
Template Method                          Strategy
    public class TemlePrinter {                                     Template Method
      main(){
        DBInterface di = new OracleInterface();
        print(di.getData());                              uses
      }                                                                        getData()
    }


    public class StratPrinter {
      main(){                                                       Strategy
        DBInterface di = new OracleInterface();
        di.init()
        print(di.getData());                              uses       init()        getData()
        di.exit();
      }                                                              exit()
    }

    Template Method                                                               Strategy

                       =                         Template Method
                              Strategy
                        Strategy                                                              init
                                         DB                               Template Method
Factory Method
                                                                         public Interface Bond{
                public Interface BondFactory{           creates            public int getMaturity();
                  public Bond getBondInstance();
                                                                           public BigDecimal getFairValue();
                }
                                                                         }

                                  implements                                                implements

    public class TreasuryFactory implements BondFactory{             public class Treasury implements Bond{
       public Bond getBondInstance(){                                   ////
          return new Treasury();                                     }
       }
    }


    public class ConvertibleFactory implements BondFactory{         public class Convertible implements Bond{
       public Bond getBondInstance(){                                  ////
          return new Convertible();
                                                                    }
       }
    }






                     Bond                                    Strategy   Template
                      Factory                                                                     Factory
Command



 public class User{
    public void dosth(Tranzaction t,String id){
       t.do(id);                                             public Interface Tranzaction{
    }                                                           public void do(String id);
 }                                                           }



  public class addCommand implements Tranzaction{
     public void do(String id){
        //
      }
  }

                                                  public class removeCommand implements Tranzaction{
                                                     public void do(String id){
                                                        //
                                                      }
                                                  }
State



  public class User{
     State s = n;
     public void dosth(){                                 public Interface State{
        s.do();                                              public int do();
     }                                                    }
  }


   public class GoodState implements State{
      public void do(){
         System.ouit.println(“Good State”);
      }
   }

                                              public class BadState implements State{
                                                 public void do(){
                                                    System.ouit.println(“Good State”);
                                                 }
                                              }
Null Object

                 Null

 public class User{
    public void dosth(Tranzaction t,String id){   public Interface Tranzaction{
       t.do(id);                                     public void do(String id);
    }                                             }
 }




                              public class NullCommand implements Tranzaction{
                                 public void do(String id){
                                       //
                                   }
                              }
DI Dipendency Injection


 public class User{
    public void dosth(Tranzaction t,String id){
       t.do(id);                                             public Interface Tranzaction{
    }                                                           public void do(String id);
 }                                                           }




                DI

         Tranzaction

                                                  public class removeCommand implements Tranzaction{
                                                     public void do(String id){
                                                        //
                                                      }
                                                  }
DI
                new

     –


     –




   DI
     – Spring
     – Seaser2
     – EJB 3.0
Strategy/Temprate Method



DI
20070329 Object Oriented Programing Tips

20070329 Object Oriented Programing Tips

  • 2.
    M 
  • 3.
    public class Shape{ protected int h,w; public void setWidth(int w){ this.w = w; } public void setHeight(int h){ this.h = h; → } } public class Rectangele extends Shape{ } public class Square extends Rectangle{ protected int h,w; public void setWidth(int w){ this.w = w; → this.h = h; } public void setHeight(int h){ this.w = w; this.h = h; } }
  • 4.
    LSP public class Shape{ protected int h,w; ArrayList<Rectangele> list public void setWidth(int w){ = new ArrayList<Rectangele>(); this.w = w; } public void setHeight(int h){ for(Rectangler r : list){ } this.h = h; r.setWidth(20); } r.setHeight(10); } public class Rectangele extends Shape{ } public class Square extends Rectangle{ protected int h,w; public void setWidth(int w){ this.w = w; this.h = h; } List Square public void setHeight(int h){ this.w = w; this.h = h; } }
  • 5.
    public class User{ public void dosth(Shape s){ public Interface Shape{ s.dosth(); public void dosth(); } } } public class Rectangle implements Shape{ public class Square implements Shape{ public void dosth(){ public void dosth(){ System.out.println(“do something”); System.out.println(“do something”); } } } } Light Weight POJP Plain Old Java Object
  • 6.
      Gof  23 new  →
  • 7.
    Temprate Method  Strategy  Factory Method  Command  State  Null Object
  • 8.
    Template Method public abstractclass DBInterface{ public class OracleInterface extends DBInterface{ public Date getData(){ protected void init(){ init(); // Data data = getData(); exit(); return data; } } protected Data getData(){ protected abstract void init(); // protected abstract Data getData(); } protected abstract void exit(); protected void exit(){ } // } extends }ublic class FireBirdInterface extends DBInterface{ p //// }  Data   DBInterface = =public
  • 9.
    Strategy public Interface DBInterface{ public class OracleInterface implements DBInterface{ public void init(); public void init(){ public Data getData(); // public void exit(); } } public Data getData(){ // } implements public void exit(){ // } } public class FireBirdInterface implements DBInterface{ //// }  Template   DBInterface
  • 10.
    Template Method Strategy public class TemlePrinter { Template Method main(){ DBInterface di = new OracleInterface(); print(di.getData()); uses } getData() } public class StratPrinter { main(){ Strategy DBInterface di = new OracleInterface(); di.init() print(di.getData()); uses init() getData() di.exit(); } exit() }  Template Method Strategy  = Template Method Strategy Strategy init DB Template Method
  • 11.
    Factory Method public Interface Bond{ public Interface BondFactory{ creates public int getMaturity(); public Bond getBondInstance(); public BigDecimal getFairValue(); } } implements implements public class TreasuryFactory implements BondFactory{ public class Treasury implements Bond{ public Bond getBondInstance(){ //// return new Treasury(); } } } public class ConvertibleFactory implements BondFactory{ public class Convertible implements Bond{ public Bond getBondInstance(){ //// return new Convertible(); } } }    Bond Strategy Template Factory Factory
  • 12.
    Command public classUser{ public void dosth(Tranzaction t,String id){ t.do(id); public Interface Tranzaction{ } public void do(String id); } } public class addCommand implements Tranzaction{ public void do(String id){ // } } public class removeCommand implements Tranzaction{ public void do(String id){ // } }
  • 13.
    State publicclass User{ State s = n; public void dosth(){ public Interface State{ s.do(); public int do(); } } } public class GoodState implements State{ public void do(){ System.ouit.println(“Good State”); } } public class BadState implements State{ public void do(){ System.ouit.println(“Good State”); } }
  • 14.
    Null Object Null public class User{ public void dosth(Tranzaction t,String id){ public Interface Tranzaction{ t.do(id); public void do(String id); } } } public class NullCommand implements Tranzaction{ public void do(String id){ // } }
  • 15.
    DI Dipendency Injection public class User{ public void dosth(Tranzaction t,String id){ t.do(id); public Interface Tranzaction{ } public void do(String id); } } DI Tranzaction public class removeCommand implements Tranzaction{ public void do(String id){ // } }
  • 16.
    DI  new – –  DI – Spring – Seaser2 – EJB 3.0
  • 17.