الاثنين، 7 فبراير 2011

adapter pattern

TeaBag.java - the class which the adapter will make the adaptee adapt to
public class TeaBag { 
   boolean teaBagIsSteeped;
   
   public TeaBag() {
       teaBagIsSteeped = false;
   }
  
   public void steepTeaInCup() {
       teaBagIsSteeped = true;
       System.out.println("tea bag is steeping in cup");
   }
}
////////////////////////////////////
TeaBall.java - the adapter
public class TeaBall extends TeaBag { 
   LooseLeafTea looseLeafTea;
  
   public TeaBall(LooseLeafTea looseLeafTeaIn) {
       looseLeafTea = looseLeafTeaIn;
       teaBagIsSteeped = looseLeafTea.teaIsSteeped;
   }
   
   public void steepTeaInCup() {
       looseLeafTea.steepTea();
       teaBagIsSteeped = true;
   }
}//////////////////////////////////////
LooseLeafTea.java - the adaptee
public class LooseLeafTea { 
   boolean teaIsSteeped;
   
   public LooseLeafTea() {
       teaIsSteeped = false;
   }
  
   public void steepTea() {
       teaIsSteeped = true;
       System.out.println("tea is steeping");
   }
}
///////////////////////////////////////
TeaCup.java - the class that accepts class TeaBag in it's steepTeaBag() method, and so is being adapted for.

public class TeaCup { 
   public void steepTeaBag(TeaBag teaBag) {
       teaBag.steepTeaInCup();
   }
}
//////////////////////////////////////////
TestTeaBagAdaptation.java - testing the adapter

class TestTeaBagAdaptation {


   public static void main(String[] args) {
       TeaCup teaCup = new TeaCup();


       System.out.println("Steeping tea bag");
       TeaBag teaBag = new TeaBag();      
       teaCup.steepTeaBag(teaBag);


       System.out.println("Steeping loose leaf tea");
       LooseLeafTea looseLeafTea = new LooseLeafTea();
       TeaBall teaBall = new TeaBall(looseLeafTea);
       teaCup.steepTeaBag(teaBall);
   }
}
//////////////////////////////////////////
Test Results

Steeping tea bag
tea bag is steeping in cup
Steeping loose leaf tea
tea is steeping
/////////////////////////////////////////
Notes
The basic premise of the adapter is that you either can not or do not want to change the adaptee.
This might be because you purchased the adaptee, and do not have the source code.

There are two GoF versions of the adapter.

The First is the inheriting version, in which the adapter inherits from both "the adaptee" and
 "the class that adapter will make the adaptee adapt to".

The Second is the object version, which is shown here.

ref
http://www.fluffycat.com/Java-Design-Patterns/Adapter/

builder pattern

Factory , builder --> helpful for Decorator
============================================
as : composite --> uesd to build a complex object
as : iterator
see: bridge
============================================
builder --> construct complex object from simple objects step by step
        --> Make and return one object various ways.
       
       

ex: http://www.fluffycat.com/Java-Design-Patterns/Builder/       
run       
At the Boston Soup Buffet Today's Soups!  
Chicken Soup: ChickenSoup
Clam Chowder: QuahogChowder-----------------
Fish Chowder: ScrodFishChowder --------------
Minnestrone: Minestrone
Pasta Fazul: Pasta Fazul
Tofu Soup: Tofu Soup Vegetable
Soup: Vegetable Soup

At the Honolulu Soup Buffet Today's Soups!  
Chicken Soup: ChickenSoup
Clam Chowder: PacificClamChowder
Fish Chowder: OpakapakaFishChowder
Minnestrone: Minestrone
Pasta Fazul: Pasta Fazul
Tofu Soup: Tofu Soup Vegetable
Soup: Vegetable Soup


====
another example from head first
==> you have to build a vacation planner for Patternsland, a new them park just outside of objectville.
Park guests can choose a hotel and various typ ... lsa kamel elmara el gaya ISA

connect to foxpro DB

 try { // load driver ...
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();

// create connection...
//      String url = "jdbcdbc:TestDBF";
      String url = "jdbc:odbc:FoxProDS";
      Connection con = DriverManager.getConnection(url);

// test sql prepared statement...
      String sql = "SELECT Trkline FROM T1E1INFO";
      PreparedStatement stmt = con.prepareStatement(sql);
      ResultSet rs = stmt.executeQuery();
      String count = "";
      while (rs.next()) {
        count = rs.getString("Trkline");
        System.out.println(count);
      }

      rs.close();
      stmt.close();
      con.close();
    }

    catch (Exception exc) {
      exc.printStackTrace();
    }