message channel
play

Message Channel class - PowerPoint PPT Presentation

Message Channel class MessageChannelSimple Console StringProducer + readln() : String TestStringQ / MyQ + remove() : String TextGUI StringConsumer + write() : void


  1. �������������������������

  2. Message Channel class MessageChannelSimple Console StringProducer + readln() : String TestStringQ / MyQ + remove() : String TextGUI StringConsumer + write() : void ... it looks simple enough ... (c) schmiedecke 06 Inf1-9b-Encapsulation 2

  3. The Message Channel Shock class MessageChannelComplete «interface» Animate Console + readln() : String EmptyException Exception StringProducer «interface» AnimatorThread TextContainer + enter() : void + remove() : String FullException RuntimeException StringConsumer TextGUI + write() : void TestStringQ MyQ Start + main() : void and that is still a tiny program... (c) schmiedecke 06 Inf1-9b-Encapsulation 3

  4. Comprehension Alarm! • We need structure rules to ensure understandability: • Rules against Complexity! (c) schmiedecke 06 Inf1-9b-Encapsulation 4

  5. ������������������� � ������������� � ������������� � ������������������

  6. Step 1: Readable Code • Code needs to follow conventions: – ordering rules • attributes – constructors – main - public methods – private methods – indentation and whitespace rules • indent after an opening brace, unindent after a closing brace • always leave a blank line between methods • heavily indent line continuation – naming and spelling rules • use camelCase style for names • only class names start with capital letter • write constants in all-capitals • used conjugated verb in modifiers • use get/set and is/set for getters and setters (c) schmiedecke 06 Inf1-9b-Encapsulation 6

  7. ... Readable Code – code templates for certain constructs • always use new line for conditional or loop body • indent, even if single statement without braces • put catch on same line as previous closing bracket • always provide a standard constructor together with an explicit constructor • always import classes individually – general styles for literals, exceptions, ... • avoid using literals except in initializations • avoid using String literals except in initializations • never ignore an exception – size rules for methods and classes • a class must not have more than 20 public members • a method must not be longer than 15 lines (c) schmiedecke 06 Inf1-9b-Encapsulation 7

  8. ... Readable Code – commenting rules • always provide javadoc comments for classes and public members • use line comments for adding technical explanations (c) schmiedecke 06 Inf1-9b-Encapsulation 8

  9. Readable Code • Java Style Guides – strictly enforced in professional projects – http://java.sun.com/docs/codeconv/html/CodeCon vTOC.doc.html – http://www.bluej.org/objects-first/styleguide.html – http://www.cwu.edu/~gellenbe/javastyle/ • Supported by IDEs – in Eclipse, you can choose and modify the coding style – you can choose "edit-format" on a selecred code portion to make Eclipse format it accor- ding to your coding style guide (c) schmiedecke 06 Inf1-9b-Encapsulation 9

  10. Documentation Requirements • For every class: – Author, Date – Overview, purpose • for every method – Author, Date – Overview, purpose – Exceptions: type, condition – Parameters: type, purpose, value set • for attributes / groups of attributes – Author, Date – Meaning, purpose, value set (c) schmiedecke 06 Inf1-9b-Encapsulation 10

  11. The Javadoc Tool • command line tool javadoc.exe • generates standard HTML documentation – for a class – or a package (directory) • precision adjustable: – public – protected – package – private • command: javadoc –protected Singleton.java • in eclipse: – Project � Generate Javadoc (c) schmiedecke 06 Inf1-9b-Encapsulation 11

  12. Class Singleton public class Singleton { private Singleton() {} private static Singleton instance; public static Singleton getInstance() { if (instance == null) instance = new Singleton(); return instance; } } (c) schmiedecke 06 Inf1-9b-Encapsulation 12

  13. Generated HTML Documentation (c) schmiedecke 06 Inf1-9b-Encapsulation 13

  14. Javadoc Comments • Now "enrich" your javadoc documentation: • javadoc adds comments to your documentation: • comment markers /** .... */ • HTML text in between • important Javadoc tags (to be placed immediately before commented unit): – @author – @exception – @param – @return – @see – @since – @throws – @version http://java.sun.com/j2se/javadoc/writingdoccomments/index.html (c) schmiedecke 06 Inf1-9b-Encapsulation 14

  15. /** class allows only one instace to be created. * @author smi 04 * @version 1.0 * */ public class Singleton { /** private constructor prevents external instantiation */ private Singleton() {} /** private filed instance for storing the single instance */ private static Singleton instance ; /** public static method getInstance provides the only instance of * the class; if none exists, it is created here * * @author smi * @return instance of type Singleton */ pubic static Singleton getInstance() { if ( instance == null) // create if necessary instance = new Singleton(); return instance ; } (c) schmiedecke 06 Inf1-9b-Encapsulation 15 }

  16. (c) schmiedecke 06 Inf1-9b-Encapsulation 16

  17. Step 2: Encapsulation � ��������� ������������� � ��� ���� ��� !��������� � �������"����#��� � ������� $������ � �����������%���������� (c) schmiedecke 06 Inf1-9b-Encapsulation 17

  18. Encapsulation in Every-Day life: Enable Usage Without Detailed Knowledge � I need not be a mechanic to drive a car � I need not be an electronic engineer to turn on a tv set � ... and non computer scientists are using computers all over the world � It is normal to hide technical details from users (c) schmiedecke 06 Inf1-9b-Encapsulation 18

  19. Java: Two Levels of Encapsulation • classes – hide all private methods (algorithms) – and attributes (inner state) • packages – hide "technical" classes (c) schmiedecke 06 Inf1-9b-Encapsulation 19

  20. Package � Group of classes, not all of them public. � Non-public classes only visible within the package. � Packages can be nested. � Class corresponds to file (same name) � Package corresponds to directory (same name) � Package statement (first line!) puts a class into a package: package mypackage; � Classes without package statement belong to the default package. (c) schmiedecke 06 Inf1-9b-Encapsulation 20

  21. Classes and Packages package bank; public class Account {} package bank.customers; public class Customer {} package bank.exchange; public class Currency {} package bank.customers; public class Company {} package bank.estate; package bank.exchange; public class Appartment {} public class ExchangeRate { package bank; public class Credit {} (c) schmiedecke 06 Inf1-9b-Encapsulation 21

  22. Files and Directories Classpath : • Classpath can contain several directories with the same name. (c) schmiedecke 06 Inf1-9b-Encapsulation 22 • � A package can be distributed over several directory paths.

  23. Namespaces • How many methods called getX exist in the Java universe? • How many getX methods are in your classpath? (Think about libraries, e.g. cs101!) • How many getX methods exist in your BallWorld project (overridden methods)? • All different – because they belong to different classes • A class is a namespace for its attributes and methods. (c) schmiedecke 06 Inf1-9b-Encapsulation 23

  24. Packages as Namespaces � How many Date classes exist in the Java universe? � How many Date classes exist in your classpath? � Class namespace: package. � classes have a fist and second name: pakagename.Class outerpackage.innerpackage.Class � Probable classes in your classpath: java.util.Date java.sql.Date java.util.List java.awt.List cs101.io.Console cs101ext.Console (c) schmiedecke 06 Inf1-9b-Encapsulation 24

  25. The import statement • No mixing up possible: import cs101.ext.Console; // first nam basis w.Console public class Textleser { import java.util.List; // first name basis w. List public int ganzzahlLesen() { public class Textleser { java.util.List zahl = new java.util.List() ; public int ganzzahlLesen() { char c = cs101ext.Console.read(); while (c>='0' && c<='9') { List zahl = new List() ; char c = Console.read(); zahl.add(c); while (c>='0' && c<='9') { cs101ext.Console.read(); } zahl.add(c); return Integer.parseInt(zahl.toString()); Console.read(); } } } return Integer.parseInt(zahl.toString()); } } • Writing long package paths is tedious. • import saves (ONLY!!!) writing effort! (c) schmiedecke 06 Inf1-9b-Encapsulation 25

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend