3. java basics

45
Software Development for Large and Open Source Projects Kun-Ta Chuang Department of Computer Science and Information Engineering National Cheng Kung University 1

Upload: netdbncku

Post on 27-Jun-2015

708 views

Category:

Documents


1 download

TRANSCRIPT

  • 1. Software Development for Largeand Open Source ProjectsKun-Ta ChuangDepartment of Computer Science and Information EngineeringNational Cheng Kung University 1

2. Introduction to Java Coding StyleKun-Ta ChuangDepartment of Computer Science and Information EngineeringNational Cheng Kung University 2 3. What is Coding Style? Why we need to follow coding standard? Java coding standard 3 4. What is Coding Style? Coding 80% 4 5. Java coding standard Java general: 5 6. code why... what 6 7. Java implementation comments C++ /*...*/ // documentation comments Java /**...*/ javadoc HTML 7 8. /*Description * A briefde scription of the class/interface. /*********************History**yyyy-mm-dd Author* *What has been changed.**Copyright notice*********************/*/ // Do a double-flip. In general, 8 9. /** * Disposes of this graphics context once it is no longer * referenced. * * @see #dispose() * @since 1.0 */ public void finalize() { dispose(); } 9 10. 15 10 11. Parameters/customer, account a an aCustomer, anAccountFields/propertie wordfirstName, lastNames / isisDead, isString, isFailed getter Classes Customer, SavingAccountCompilation .javaCustomer.java,unit files SavingAccount.javaExceptions e eFinal staticMIN_BALANCE,fields (consants)DEFAULT_DATE11 12. Getter get getFirstName(), getLastName()InterfacesRunnable, Contactable,Singleton ableibleLocal variables word grandTotal, customer, newAccount firstNameLoop counters i, j, k counter i, j, k, cnt, counterPackages java.awt, internet com.company.utility com.apple.quicktime.v2 Member word openFile(), addAccount()functions Setter set setFirstName(), setLastName() 12 13. int level; // Indentation level Java {...} if (condition) {int int2 = 0; // ... } int count;... myMethod() { if (condition) { int count; // ...}... }13 14. ( { } } { :class Sample extends Object {int ivar1;int ivar2;Sample(int i, int j) {ivar1 = i;ivar2 = j;}int emptyMethod() {}...}14 15. 2 Tab Tab 2 80 70 8 15 16. if ... else Example:if if 16 17. {...} { } if-else, for, while... return 17 18. if, if-else, if else-if else for while switch try-catch 18 19. . for (ex. myMethod((byte) aNum, (Object) x); ) / 19 20. StringBuffer String null pointer assignment null String.equals() String String null : if ("Michael".equals(s)) FileNotFoundExceptionException finalstatic 20 21. Reference http://geosoft.no/development/javastyle.html http://chigo-studio.blogspot.tw/2008/05/coding-style-httpwww.html http://www.javaworld.com.tw/jute/post/view?bid=20&id=27897&sty=1&tpg=1&age=-1 http://www.yestrip.com/load_file/unoprj/aaa/aaaaa%E5%B0%88%E6%A1%88%E5%90%8D%E7%A8%B1.htm 21 22. Introduction to Java Design PatternKun-Ta ChuangDepartment of Computer Science and Information EngineeringNational Cheng Kung University 22 23. System Development Concept Why Design Pattern Simple Factory Abstract Factory Default Adapter Visitor Iterator Strategy Singleton 23 24. Software Development Concept 24 25. Why Design Patterns Pattern, , ,-- Examples Pattern ... ... ... ... 10050, Java Java From , Design Pattern 25 26. Why Design Patterns Examples PatternCome on, Come one close Come on, Come one Pattern From , Design Pattern 26 27. Why Design Patterns There are still many patterns in your life pattern 27 28. Why Design Patterns ,Pattern ? , Pattern is not good But for engineering, patterns is good Some Patterns are , 28 29. Object-Oriented Analysis Object-Oriented Programming Object-Oriented Design We use Design Patterns in OOD OOP 29 30. Simple Factory class : 30 31. Simple Factory :public class ClothFactory {public static Cloth getCloth() { //generate and get a clothCloth cloth = new Cloth();//do somethingreturn cloth;}public static Pant getPant() { //generate and get a pantPant pant = new Pant();//do somethingreturn pant;}}public static void main(String args[]) { //main program/**only one line, user can get object they want**/Cloth myCloth = ClothFactory.getCloth();} 31 32. Abstract Factory () ()32 33. Abstract Factory 33 34. Default Adapter JavaEventListener WindowListener7 :public interface WindowListener extends EventListener {public void windowOpened(WindowEvent e);public void windowClosing(WindowEvent e);public void windowClosed(WindowEvent e);public void windowIconified(WindowEvent e);public void windowDeiconified(WindowEvent e);public void windowActivated(WindowEvent e);public void windowDecativated(WindowEvent e);}34 35. Default Adapter : WindowAdapterWindowListener public class WindowEventHandler extends WindowAdapter { public void windowClosed(WindowEvent e) { System.exit(0); } } 35 36. Visitor : Visitor : public void addPrice(Item item) { //add price of passing item to total price if(item instanceof Book) { //this item is a book totalPrice = totalPrice + ((Book) item).getWeight() * 2 + 100; } else if(item instanceof CD) { //this item is a CD totalPrice = totalPrice + ((CD) item).getWeight() * 5; } //a series of else if definition } : 36 37. Visitor Visitor? Visitor overload interface Visitor public interface Visitor{public void visit(Book book); //visit Book itempublic void visit(CD cd); //visit CD item//other definition} interface Visitable public void accept(Visitor visitor) visitableaccept public interface Visitable{ public void accept(Visitor visitor); //separate varied operation }37 38. Visitor/** BookVisitable**//* Visitor */public class Book implements Visitable { public class PostageVisitor implements Visitor{//variable definition//variable definition//accept the visitor //collect data about the bookpublic void accept(Visitor vistor) {public void visit(Book book) {visitor. visit(this); //**overload** totalPrice = totalPrice + book. getWeight() * 2 + 100;} }//other method definition //define other visitors} public void visit(CD cd){...}//other definition object with implementation //other method definition//of Visitable }/****/Public static void main(String args[]) { //create visitor PostageVisitor visitor = new PostageVisitor(); //iterate through all items for(Visitable item: items){ item. accept(visitor); } //get total price double totalPrice = visitor.getTotalPrice();38} 39. Iterator foreach() : (ex. ListSet) foreach() : ListSetCollection iterator()IteratorhasNext() next(): public static void foreach(Collection collection) { Iterator iterator = collection.iterator(); while(iterator.hasNext()) {System.out.println(iterator.next()); } } 39 40. Strategy Step1. ServerSocket Step2. accept() Step3. 40 41. Strategy StrategyStrategy : 41 42. Singleton design pattrn : (global access) Think1. : 42 43. Singleton Think2. Lazy Initialization : public class Singleton { private static Singleton instance = null; private Singleton() { } //judge and get instance public static Singleton getInstance() {/* lazy initialization */if (instance == null) { //check object instance = new Singleton();}return instance; } //other definition } 43 44. Singleton Think3. Double-check Locking public class Singleton { private static Singleton instance = null; private Singleton(){} //judge and get instance public static Singleton getInstance() {if (instance == null){ //check object synchronized(Singleton.class){ ///* lazy initialization */if(instance == null) { //check object againinstance = new Singleton();} }}return instance; } }44 45. http://caterpillar.onlyfun.net/Gossip/DesignPattern/DesignPattern.htm Java Papers Design Patterns http://javapapers.com/category/design-patterns/45