DOC PREVIEW
Duke CPS 108 - Law of Demeter

This preview shows page 1 out of 2 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 2 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Software Design6.1Tell, Don't Askz Tell objects what you want them to do, do not ask questions about state, make a decision, then tell them what to do (Pragmatic Programmers, LLC)¾ Think declaratively, not procedurally¾ Don't ask for a map, then walk through the map¾ Instead of iteration, apply to all• Breaks when we don't want to apply to allzRules are made to be broken¾ Reduce coupling, better codeSoftware Design6.2Law of Demeterz Don't talk to objects, don't call methods. The more you talk, the more you rely on something that will break later¾ Call your own methods¾ Call methods of parameter objects¾ Call methods if you create the objectz Do NOT call methods on objects returned by callsList all = obj.getList();all.addSpecial(key,getValue());obj.addToList(key,getValue()); // ok hereSoftware Design6.3Toward a GUI-programming modelz We want to adhere to language-independent ideals¾ Concepts move from GUIs in Java to …¾ javax.swing and java.awt offer thousands of choices• Too many to have to understand/find comfort in, but …z But, write reasonable, robust , GUI applications¾ Actually write code, not simply adhere to lofty ideals¾ Show me the code!Software Design6.4One GUI Conceptual Frameworkz Create a JPanel for the GUI contentPane¾ Provide a BorderLayout, organize hierarchically¾ Ok to use GridLayout, FlowLayout, … nestedz Create Buttons, Menu-items, and other widgets¾ Bind each event-generator to a listener¾ Do not dispatch within a listener on event source• No "if event-generator is button A do this"z Use anonymous inner classes, or named inner classes¾ Process events, created and attached close-to-source¾ Make a button, make a button-listenerSoftware Design6.5Click on a button, display the clickActionListener textDisplayer = new ActionListener(){public void actionPerformed(ActionEvent e){showText(e.getActionCommand());}};z What does an ActionListener do?¾ Listens for an event, e.g., from Button, Menu, …¾ Processes the command/eventz How do anonymous classes work?¾ Note: ActionListener is an interface, but object created!¾ See what Eclipse refactoring will do with thisSoftware Design6.6Making a Move: View and ControllerActionListener moveMaker = new ActionListener(){public void actionPerformed(ActionEvent e){int val = Integer.parseInt(e.getActionCommand());myControl.makeMove(new GameMove(val));}};z We know this will be bound to a specific type of button¾ Not generic, completely application specific¾ Turns swing/GUI event into application event: Movez Controllers should be programmed abstractly¾ Don't base code on a GUI toolkit, separate concernsSoftware Design6.7Alan Kay, winner of 2004 Turing Awardz Alto, Smalltalk, Squeak, Object-Oriented Programming"Don't worry about what anybody else is going to do… The best way to predict the future is to invent it. Really smart people with reasonable funding can do just about anything that doesn't violate too many of Newton's Laws!" Software Design6.8Alan Kay on Education and OOz "By the time I got to school, I had already read a couple hundred books. I knew in the first grade that they were lying to me because I had already been exposed to other points of view. School is basically about one point of view -- the one the teacher has or the textbooks have. They don't like the idea of having different points of view, so it was a battle. Of course Iwould pipe up with my five-year-old voice."z Java and C++ make you think that the new ideas are like the old ones. Java is the most distressing thing to hit computing since MS-DOS. z I invented the term "Object-Oriented", and I can tell you I did not have C++ in


View Full Document

Duke CPS 108 - Law of Demeter

Download Law of Demeter
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Law of Demeter and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Law of Demeter 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?