From: Ged Byrne Date: 2003-09-04T02:08:57+09:00 Subject: Re: OOP flavors - was Re: Python vs. Ruby > Thank you, that is perceptive. > > Actually, this does also apply to the real-world > examples > I mentioned. > > text.annotate(image) # counter-intuitive to > me: in both cases, > polygon.draw(window) # parameter (not > receiver) changes state > > Hal > > I think you have to recognise that there are different views of the objects, depending on what level or abstraction you are working on. Take, for example, Java's AWT components. A Button in Java knows how to draw itself. It has a paint method that gets called by a component. The Buttons container passes it a Graphics object, a canvas upon which to draw itself. However, the user doesn't have to have anything to do with this unless he is implementing a custom component. To add a Button to a window you write Window.add(Button). There are two different views here. There is the logical view. We have a window, and that window contains components such as buttons. Then there is an implementation view. We have a square bit of screen upon which a window and its components will render themselves. Good OO involves separating all of these views so that it is possible for somebody using the objects to work consistantly within a single model of a real world object. So, applying this to the polygon example: Think about the design of a button. The nice GUI buttons that look 3D. First of all you have one big rectangle. Inside that rectangle there is a slightly smaller rectangle and 4 rhombuses. 2 rhombuses are a darker shade of grey and 2 a lighter shade. When your dialog receives a repaint command, it says "Button, here is some canvas. Draw yourself upon it." [Button.paint(g)] The button takes this canvas and breaks it into 5 parts, passes each part to the necessary shape and tells them to draw themselves. Finally the chain comes to a Rectangle or Rhombus object. This shape object does not know that it is part of a button. It only knows that it is a shape and how to draw itself upon a canvas. Eventually a canvas will be given a command. Whether it is Canvas.draw(Rectangle) or Canvas.plot(x,y) depends on the canvas object. Can it just plot points, or does it know how to draw geometric shapes. What you wouldn't expect the Canvas to know is how to draw a button or a dialog. In this model you are dealing with a button as a group of geometric shapes. When you use a dialog that has a button on it, you don't worry about any of this. You say "Dialog, you have a button called Fred." "Fred, when somebody clicks on you, do this." You are working in a logical model that views a button as something on dialog that causes an action when it is pushed. So whether or not you say Window.draw(Square) or Square.draw(Window) depends upon the logical model you are dealing with. OO design does not dictate one form over the other. OO design dictates a systematic modeling of reality. ________________________________________________________________________ Want to chat instantly with your online friends? Get the FREE Yahoo! Messenger http://uk.messenger.yahoo.com/