• No se han encontrado resultados

Construcción social de la realidad

CAPÍTULO 2. MARCO TEÓRICO

2.3 Construcción social de la realidad

Every button has an associated CalculatorCommands.Command object reference and a JCalculator reference.

Lets assume the calculator button that was clicked has a Sqrt command of type CalculatorCommands.Sqrt which implements Unary. The code for Sqrt is:

public static class Sqrt implements Unary {

public void exec(JCalculator calculator) {

CalculatorStack stack = calculator.getStack(); stack.pushNumber(Math.sqrt(stack.popNumber())); }

}

The Sqrt button is clicked, an ActionEvent is raised and the buttons actionPerformed() method is invoked. Java Quick Reference - Case Study - JCalculator - Unary Function Behaviour

the actionPerformed() method checks its objects (the buttons) command object type and determines its of type CalculatorCommands.Unary

1.

it invokes its own evaluate() method 2.

the buttons reference to the calculator object is used to get a reference to the calculators stack 3.

the stack isEmpty() method is invoked and returns 'true' so evaluate() returns control to the actionPerformed() method 4.

the actionPerformed() method retrieves a reference to the calculators stack through the buttons calculator reference 5.

the buttons command object (in this case aSqrt) is pushed onto the stack 6.

the buttons evaluate() method is again invoked 7.

a reference to the calculators stack is retrieved 8.

this time the stack is not empty and its top object is a function (the Sqrt object) 9.

the buttons calculator reference is used to retrieve a reference to the calculators display field 10.

the Sqrt object is popped off the calculators stack 11.

the field reference is used to retrieve the number currently displayed 12.

the retrieved number is pushed onto the stack 13.

the Sqrt objects exec() method is invoked and a copy of the buttons calculator reference is passed as an argument 14.

the Sqrt object uses its calculator reference (the one passed to exec() ) to get a reference to the calculator stack 15.

the number pushed onto the stack in evaluate() is popped off the stack and used as an argument to Math.sqrt() 16.

the result returned by Math.sqrt() is pushed onto the stack 17.

control returns to the buttons evaluate() method 18.

the result pushed onto the stack by the Sqrt object is retrieved and passed to the calculator field by invoking the fields setNumber() method

19.

the evaluate() method returns control to the actionPerformed() method 20.

there is nothing else to do 21.

Whew! There are an awful lot of busy objects! The actual job of providing the square of a number is handled by the command object, Sqrt, but the responsibility for getting everything ready for the Sqrt object is being handled by the button object.

Hmmm ... still not all that clear; lets try to think of it as a conversation between actors. The cast: Button - a CalculatorButton

Calculator - a JCalculator that belongs to Button

Sqrt - a CalculatorCommands.Unary that belongs to Button

Math.sqrt - a friend of Sqrt's

ActionPerformed - Button's helper

Evaluate - Button's helper

Stack - a CalculatorStack that belongs to Buttons calculator

Display - a CalculatorField that belongs to Buttons calculator

Button: "Hey, I've just been clicked! ActionPerformed, you need to get to work!"

ActionPerformed: "Ok. Do we have a command? Oh yeah, a Sqrt and its a Unary. Evaluate

can you check things for me?"

Evaluate: "Sure thing. Calculator, pass me your Stack for a minute."

Calculator: "Here he is." [Hands Stack to Evaluate]

Evaluate: "Oops .. its empty nothing for me to do. ActionPerformed, it's up to you."

ActionPerformed: "Calculator, let me have your Stack. Stack, here, take a copy of Sqrt."

[Hands Sqrt to Stack] "Ok Evaluate, your turn again."

Java Quick Reference - Case Study - JCalculator - Unary Function Behaviour

Evaluate: "Calculator, can you let me see Stack again?"

Calculator: "Sure." [Hands Stack to Evaluate]

Evaluate: "Alright Stack are you empty?"

Stack: "Nope."

Evaluate: "Do you have a function?"

Stack: "Let me see, yup, I got a function on top of me."

Evaluate: "Great. Calculator, can you give me your Display?"

[Calculator hands Display to Evaluate]

Evaluate: "Stack, let me have that function and Display, you give Stack the number you're holding."

[Stack hands Sqrt to Evaluate and Display gives Stack a

number]

Evaluate: "Ok Sqrt, you do your thing. Oops, here you need to talk to Calculator"

[Hands Sqrt a connection to Calculator]

Sqrt: "Calculator, give me Stack please."

[Calculator hands over Stack] Stack (sotto voice): "Hey, I'm tired of being man handled! Geesh, don't you guys

have

anything better to do!"

Sqrt: "Stack, give me the number your holding. I need to pass it to Math.sqrt."

Math.sqrt: "Here Sqrt, I did my thing with the number, you can have it back now."

Sqrt: "Here you go Stack ... take number back now. Hey Evaluate, I'm finished."

Evaluate: "Stack, let me have the number Sqrt just gave you." [Stack

hands

the number to Evaluate]. "Display, can you show this to

everyone?"

Display: "Sure thing." [Display takes the number and holds it up for

all

to see]

Evaluate: "Ok ActionPerformed, I'm finished!"

ActionPerformed: "Button, we're all done now."

Button: "Thanks guys. What a team!"

[The End] That's a little clearer. The calculator is basically acting as a holder for all the objects. The actionPerformed() and evaluate()

methods in CalculatorButton are directing events and the actual work/function is being handled by the Java Quick Reference - Case Study - JCalculator - Unary Function Behaviour

CalculatorCommands.Command object.

So what happens if the command object is a Binary function?

Home | Case Studies | Previous | TOC | Next

Java Quick Reference - Case Study - JCalculator - Unary Function Behaviour

Java Case Study - JCalculator - Binary Function Behaviour