Java Class Design
-
Upload
ganesh-samarthyam -
Category
Software
-
view
120 -
download
0
Transcript of Java Class Design
Java Class Design Ganesh [email protected]
Name the first object oriented programming
language
Simula (1965!)
OOP sometimes becomes
❖ Uh-oh
or
❖ Oops!
Guess the outputclass Base { public Base() { foo(); } public void foo() { System.out.println("In Base's foo "); } }
class Derived extends Base { public Derived() { i = new Integer(10); } public void foo() { System.out.println("In Derived's foo " + i.toString() ); } private Integer i; }
class Test { public static void main(String [] s) { new Derived(); } }
Guess the output
abstract class Printer { private Integer portNumber = getPortNumber();
abstract Integer getPortNumber(); public static void main(String[]s) {
Printer p = new LPDPrinter(); System.out.println(p.portNumber); }
}
class LPDPrinter extends Printer { /* Line Printer Deamon port no is 515 */ private Integer defaultPortNumber = 515; Integer getPortNumber() {
return defaultPortNumber; }
}
Date & time exercise
❖ Your project requires support for date and time functionality❖ Assume that the Java library or third-party library support is
not available for date and time related functionality
❖ Will you provide separate classes for date, time and timezone or a single class that supports all the three? How to support calculating differences between two date/time values?
Class design problem: Example
Discussion example
// using java.util.Date Date today = new Date(); System.out.println(today);
$ java DateUse Wed Dec 02 17:17:08 IST 2015
Why should we get the time and timezone details if I only want a date? Can
I get rid of these parts? No!
So what!Date today = new Date(); System.out.println(today); Date todayAgain = new Date(); System.out.println(todayAgain);
System.out.println(today.compareTo(todayAgain) == 0);
Thu Mar 17 13:21:55 IST 2016 Thu Mar 17 13:21:55 IST 2016 false
What is going on here?
java.time package!
SolutionLocalDate today = LocalDate.now(); System.out.println(today); LocalDate todayAgain = LocalDate.now(); System.out.println(todayAgain); System.out.println(today.compareTo(todayAgain) == 0);
2016-03-17 2016-03-17 true
Works fine now!
Revisiting the example … You can use only date, time, or even timezone, and combine them as
needed!
LocalDate today = LocalDate.now(); System.out.println(today); LocalTime now = LocalTime.now(); System.out.println(now);
ZoneId id = ZoneId.of("Asia/Tokyo"); System.out.println(id);
LocalDateTime todayAndNow = LocalDateTime.now(); System.out.println(todayAndNow);
ZonedDateTime todayAndNowInTokyo = ZonedDateTime.now(ZoneId.of("Asia/Tokyo")); System.out.println(todayAndNowInTokyo);
2016-03-17 13:28:06.927 Asia/Tokyo 2016-03-17T13:28:06.928 2016-03-17T16:58:06.929+09:00[Asia/Tokyo]
More classes in Date/Time API
Avoid switch casespublic'Insets'getBorderInsets(Component'c,'Insets'insets){'
''''''''Insets'margin'='null;'
'''''''''//'Ideally'we'd'have'an'interface'defined'for'classes'which'
''''''''//'support'margins'(to'avoid'this'hackery),'but'we've'
''''''''//'decided'against'it'for'simplicity'
''''''''//'
''''''''if'(c'instanceof'AbstractBuEon)'{'
'''''''''''''''''margin'='((AbstractBuEon)c).getMargin();'
''''''''}'else'if'(c'instanceof'JToolBar)'{'
''''''''''''''''margin'='((JToolBar)c).getMargin();'
''''''''}'else'if'(c'instanceof'JTextComponent)'{'
''''''''''''''''margin'='((JTextComponent)c).getMargin();'
''''''''}'
''''''''//'rest'of'the'code'omiEed'…'
Interface hierarchy for polymorphism
Using runtime polymorphism
!!!!!!!margin!=!c.getMargin();
!!!!!!!!if!(c!instanceof!AbstractBu8on)!{!
!!!!!!!!!!!!!!!!!margin!=!((AbstractBu8on)c).getMargin();!
!!!!!!!!}!else!if!(c!instanceof!JToolBar)!{!
!!!!!!!!!!!!!!!!margin!=!((JToolBar)c).getMargin();!
!!!!!!!!}!else!if!(c!instanceof!JTextComponent)!{!
!!!!!!!!!!!!!!!!margin!=!((JTextComponent)c).getMargin();!
!!!!!!!!}
Assign responsibilities correctlyclassGraphicsDevice
public voidsetFullScreenWindow(Window w){
if(w!=null){
if(w.getShape() !=null) {w.setShape(null); }
if(w.getOpacity() <1.0f){w.setOpacity(1.0f);}
if(!w.isOpaque()) {
Color bgColor =w.getBackground();
bgColor =newColor(bgColor.getRed(),
bgColor.getGreen(),
bgColor.getBlue(), 255);
w.setBackground(bgColor);
}
}
…
}
Keep data members private
Protect from direct external mutations
Ensure each class has a single responsibility
Adhere to IS-A relationship
Depend on abstractions
SOLID Principles
S Single Responsibility Principle
Every object should have a single responsibility and that should be encapsulated by the class
O Open Closed Principle Software should be open for extension, but closed for modification
L Liskov’s Substitution Principle
Any subclass should always be usable instead of its parent class
I Interface Segregation Principle
Many client specific interfaces are better than one general purpose interface
D Dependency Inversion Principle
Abstractions should not depend upon details. Details should depend upon abstractions
Tools to exploreJhawk(Java)
CodeCity(C++,Java,C#)
CppDepend(C++)
Sotograph(C++,Java,C#)
Imagix 4D(C,C++,Java)
Lattix(C/C++, Java,C#)
SolidSX(C++,Java,C#)
Bauhaus(C/C++, Java,C#)
Structure101(Java,C#)
Understand(C/C++,Java,C#)
Simian(C/C++, Java,C#,…)
Jarchitect(Java)
Ndepend(C#)
Stan4J(Java)
InFusion(C/C++, Java)
InCode(C/C++, Java)
Meetups
h"p://www.meetup.com/JavaScript-Meetup-Bangalore/h"p://www.meetup.com/Container-Developers-Meetup-Bangalore/h"p://www.meetup.com/So>ware-Cra>smanship-Bangalore-Meetup/h"p://www.meetup.com/Core-Java-Meetup-Bangalore/h"p://www.meetup.com/CloudOps-Meetup-Bangalore/h"p://www.meetup.com/Bangalore-SDN-IoT-NetworkVirtualizaGon-Enthusiasts/h"p://www.meetup.com/So>wareArchitectsBangalore/h"ps://www.meetup.com/Mobile-App-Developers-Bangalore-Meetup/
[email protected] @GSamarthyam
www.codeops.tech slideshare.net/sgganesh
+91 98801 64463 bit.ly/ganeshsg