Adapter Pattern in SmallTalk

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Adapter Pattern in SmallTalk

KingNothing
Hi, i'm new to this forum and to smalltalk.
I've got to implement now the Adapeter Pattern in SmallTalk.
I've got an example in Java that i must translate in SmallTalk.
Here there are the Java Classes :

------------------------------------------
public class Glass
{
   /**
      in 12th of pint
   */
   float content;

   public Glass()
   {
      content=0;
   }

   /**
   */
   public void fill(float filling)
   {
      content=filling;
   }

   public String toString()
   {
      return
         super.toString()+
         ", level (12th of pint)="+content;
   }
}
-------------------------------

public class Keg
{
   /**
      content, in 12th of pint units
   */
   float filled;

   /**
      absolute level, in pints
   */
   public float getLevel()
   {
      return filled/12;
   }

   float original;
   /**
      in pints
   */
   public float getCapacity()
   {
      return original/12;
   }


   /**
      Keg filled with total gallons of beer
   */
   public Keg(float total)
   {
      filled=total*8*12;
      original=filled;
   }

   /**
      creates a Glass filled with
      quantity/12 pints of beer
   */
   public Glass draw(float quantity)
   {
      Glass g=new Glass();
      g.fill(quantity);
      filled-=quantity;
      return g;
   }

   /**
      % level
   */
   public float getPLevel()
   {
      return (100*filled)/original;
   }

   public String toString()
   {
      return
         super.toString()+
         ", level (%)="+getPLevel()+
         ", level (pints)="+getLevel();
   }
}
---------------------------------

public class Fusto
{
   /**
      rapporto LITRO/PINTA
   */
   public final static float LITRO_PINTA=2.1133f;

   Keg keg;
   public Fusto(Keg k)
   {
      keg=k;
   }

   /**
      crea un Glass riempito con
      quant/10 litri di birra
   */
   public Glass spilla(float quant)
   {
      return keg.draw(quant*LITRO_PINTA*12/10);
   }

   /**
      in litri, NON in percentuale!!!
   */
   public float fornisciLivello()
   {
      return keg.getLevel()/LITRO_PINTA;
   }

   /**
      in percentuale
   */
   public float fornisciPLivello()
   {
      return keg.getPLevel();
   }

   public String toString()
   {
      return
         super.toString()+
         ", livello (%)="+fornisciPLivello()+
         ", livello (litri)="+fornisciLivello();
   }
}
-------------------------------------

Can someone give me some hints to translate java code into SmallTalk ?
Thanks in advance.
Reply | Threaded
Open this post in threaded view
|

Re: Adapter Pattern in SmallTalk

Conrad Taylor
Hi, if you're only interested in converting the Java code to Smalltalk, I would simple take one class at a time.  For example, using the Glass class:

Object subclass: #Glass
     instanceVariableNames: ''
     classVariableNames: ''
     poolDictionaries: ''
     category:  'Adapter-Example'

 
instance methods ' initialize-release'

initialize
   content := 0.0

instance methods 'accessing ' protocol

content:  anObject
   content := anObject

class methods 'class initialization' protocol

new
  ^super new initialize

Good luck,

-Conrad

 
On 8/29/07, KingNothing <[hidden email]> wrote:

Hi, i'm new to this forum and to smalltalk.
I've got to implement now the Adapeter Pattern in SmallTalk.
I've got an example in Java that i must translate in SmallTalk.
Here there are the Java Classes :

------------------------------------------
public class Glass
{
   /**
      in 12th of pint
   */
   float content;

   public Glass()
   {
      content=0;
   }

   /**
   */
   public void fill(float filling)
   {
      content=filling;
   }

   public String toString()
   {
      return
         super.toString()+
         ", level (12th of pint)="+content;
   }
}
-------------------------------

public class Keg
{
   /**
      content, in 12th of pint units
   */
   float filled;

   /**
      absolute level, in pints
   */
   public float getLevel()
   {
      return filled/12;
   }

   float original;
   /**
      in pints
   */
   public float getCapacity()
   {
      return original/12;
   }


   /**
      Keg filled with total gallons of beer
   */
   public Keg(float total)
   {
      filled=total*8*12;
      original=filled;
   }

   /**
      creates a Glass filled with
      quantity/12 pints of beer
   */
   public Glass draw(float quantity)
   {
      Glass g=new Glass();
      g.fill(quantity);
      filled-=quantity;
      return g;
   }

   /**
      % level
   */
   public float getPLevel()
   {
      return (100*filled)/original;
   }

   public String toString()
   {
      return
         super.toString()+
         ", level (%)="+getPLevel()+
         ", level (pints)="+getLevel();
   }
}
------------------------------ ---

public class Fusto
{
   /**
      rapporto LITRO/PINTA
   */
   public final static float LITRO_PINTA=2.1133f;

   Keg keg;
   public Fusto(Keg k)
   {
      keg=k;
   }

   /**
      crea un Glass riempito con
      quant/10 litri di birra
   */
   public Glass spilla(float quant)
   {
      return keg.draw(quant*LITRO_PINTA*12/10);
   }

   /**
      in litri, NON in percentuale!!!
   */
   public float fornisciLivello()
   {
      return keg.getLevel()/LITRO_PINTA;
   }

   /**
      in percentuale
   */
   public float fornisciPLivello()
   {
      return keg.getPLevel ();
   }

   public String toString()
   {
      return
         super.toString()+
         ", livello (%)="+fornisciPLivello()+
         ", livello (litri)="+fornisciLivello();
   }
}
-------------------------------------

Can someone give me some hints to translate java code into SmallTalk ?
Thanks in advance.

--
View this message in context: http://www.nabble.com/Adapter-Pattern-in-SmallTalk-tf4347089.html#a12384904
Sent from the Squeak - Beginners mailing list archive at Nabble.com.

______________________________ _________________
Beginners mailing list
[hidden email]on.org
http://lists.squeakfoundation .org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Adapter Pattern in SmallTalk

KingNothing
Thank you very much.
I will make some exercise with smalltalk and come back to disturb you later probably ... :$
Thanks again.

Conrad Taylor wrote
Hi, if you're only interested in converting the Java code to Smalltalk, I
would simple take one class at a time.  For example, using the Glass class:
Object subclass: #Glass
     instanceVariableNames: ''
     classVariableNames: ''
     poolDictionaries: ''
     category:  'Adapter-Example'


instance methods '*initialize-release'*

initialize
   content := 0.0

instance methods '*accessing**' protocol*

content:  anObject
   content := anObject

class methods '*class initialization' protocol*
*
*
*new*
*  ^super new initialize*
*
*
Good luck,

-Conrad