SlideShare une entreprise Scribd logo
1  sur  94
Lecture 02
Software Design
Agenda
 Programming with Objects
– Classes
– Interfaces
– Generic programming
– Reflection
 Software Design
– Ducks…
Reading
 Don’t Repeat Yourself
 Polymorphism
 Separation of concerns
 Loose coupling
 Optional:
– http://docs.oracle.com/javase/tutorial/
Programming with Objects
Object Oriented Programming
 Object Oriented programming can be powerful
– One of the best ways for general purpose computing
 But
– The power of object oriented languages needs to be
used properly!
 However
– Programmers tend to forget the power of OO
Object Oriented Programming
Object Oriented Programming
 Programming languages with objects
– Objects hold data and methods
– Object variables (reference) point to objects
 Object Oriented
– Object are instances of classes, created with new
– Classes describe the objects
– Classes extend other classes – inheritance
– Instance variables are encapsulated
– Methods manipulate instance variable
Explain these concepts and why they are important in programming
Encapsulation
Interfaces
Polymorphism
EXERCISE
Think About This!
 Object-oriented programming
– Is not about class inheritance and creating advanced
class diagrams
 Remember
– Encapsulation – Hiding data
– Interfaces – Hiding implementation
– Polymorphism – Flexible and Generic Programming
 Powerful programming
– Separation of concerns
– Separating what varies from what stays the same
Think About This!
 Object-oriented programming
– Is not about class inheritance and creating advanced
class diagrams
 Remember
– Encapsulation – Hiding data
– Interfaces – Hiding implementation
– Polymorphism – Flexible and Generic Programming
 Powerful programming
– Separation of concerns
– Separating what varies from what stays the same
Separate Variations Design Principle
Identify the aspects of
your application that
vary and separate
them from what stays the
same
Don’t Repeat Yourself – DRY
 Single source of truth
Every piece of knowledge
must have a
single, unambiguous, authorit
ative representation within a
system
Object Oriented Design
 Design of classes and interfaces
– Class diagram shows relationships
– Sequence diagrams show flows
 Design Patterns
– Reoccurring solutions in design
– “Best practices” – known solutions for common
problems
Classes
Objects
 Object created
 Object used
Date day1;
Date day2 = new Date();
System.out.println (new Date ());
String s = new Date().toString ();
day1 null
day2
Date
Date now = new Date();
if (day2.before(now))
{
System.out.println (day2.toString());
}
before
tostring
...
Objects
 Operator new creates
memory
– String is an exception
 Reference variables
always point to some memory
day1 null
day2
Date
before
tostring
...
Date d = new Date();
Must be a concrete
class
Can be any supertype or
interface of the concrete class
Classes
Object
+getId() : String
-id : int
-name : String
-username : String
-password : String
-email : String
User
 Classes extend other classes
– Concrete Inheritance
– Subtype extends supertype
 Class contains
– Instance variables
– Methods
 Reference variables of type
Object points to any class
– Any supertype can reference subtype
Object obj = new User ();
User u = (User)obj;
supertype
subtype
Class methods
 Methods can be overridden
– Class extends a class and overrides a method
 Methods can be overloaded
– Same method with different parameters
 Methods can be
– public – any class can call the method
– private – only available within the class
– protected – only available within the class and
extended classes
Class A inherits class B. A overwrites method f. Variable b is created like
this:
B b = new A();
What happens when this line is run:
b.f();
A) The method in A is run
B) The method in B is run
C) First the method in B is run, then the method in A
D) The new statement is illegal and does not compile
QUIZ
✔
Classes
 References
– Point to concrete objects
– Must be same type or supertype of concrete object
– Can be interface or abstract class
Object obj = new Date ();
Date d = (Date)obj;
Constructors
 Classes have constructor
– Instantiation methods
– Same name as the class
 References
– this – refers to the class
– super – extended class
public Employee (String name,
double salary)
{
this (name);
this.salary = salary;
}
public Manager (String name)
{
super (name, 0.0);
...
}
class Point
{
private int x, y;
public Point ()
{
x = 0; y = 0;
}
public Point (int x, int y)
{
this.x = x; this.y = y;
}
public void move (intdx, intdy)
{
x+=dx;y+=dy;
}
public String toString ()
{
return "(" + x + "," + y + ")";
}
this used to refer to the
class variables
Override toString
method of Object
Class variables
Default constructors
Overloaded constructor
Example
Example
Bla
public class Test
{
public static void main (String[] args)
{
System.out.println ("Test");
Test test = new Test();
}
public Test ()
{
Point p0; // null reference
Point p1 = new Point ();
Point p2 = new Point (1,2);
Object obj = p2;
p0 = (Point)obj;
p0.move (1, 1);
System.out.println("p0=" + p0);
}
}
C:java>javac Test.java
C:java>java Test
Test
p0=(2,3)
X = 2
Y = 3
Java uses this method to pass objects to methods
A) Call by reference
B) Call by value
C) Call by object reference
D) Call by value reference
QUIZ
✔
Call By Value
 Methods use call by value
– Object references are passed by value
– The reference cannot change, but the object can
x:
y:
42
0
Point
void changeReferance(Point p)
{
while (p.x>0) p.x--;
}
p
Point p = new Point (42,0);
changeReferance(p);
System.out.println(p.x);
Reference p to Point
Local copy of a reference
p to Point
p is same as this.p
p
Inheritance
 Classes extend other classes
– Subclasses extend superclasses
 Reference variables of super types can
reference objects of subtypes
Employee
Manager
Empolyee e;
e = new Employee(. . .)
e = new Manager(. . .)
class Manager extends Employee
{ ... }
Polymorphism
public class Employee
{
private String name;
private double salary;
private Date hireDate;
public Employee()
{
}
public Employee(String name, double salary, Date hireDate)
{
this.name = name;
this.salary = salary;
this.hireDate = hireDate;
}
public Employee(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public void setName(String name)
{
this.name = name;
}
public String toString()
{
return "Employee: " + getName();
}
}
Bla
class Manager extends Employee
{
String title;
double bonus;
public Manager (String name, String title)
{
super (name);
this.title = title;
}
public String getTitle ()
{
return title;
}
public double getSalary()
{
return this.bonus + super.getSalery();
}
public String toString ()
{
return "Manager: " + getName() +
", " + getTitle ();
}
}
New method
New variables
Overridden
methods
What does this program print?
public class Test2
{
public static void main (String[] args)
{
System.out.println ("Test2");
Test2 test2 = new Test2();
}
public Test2 ()
{
Employee e0 = new Employee ("Dilbert");
Employee e1 = new Manager ("Pointy Haired", "Boss");
System.out.println("e0: " + e0);
System.out.println("e1: " + e1);
}
}
C:java>java Test2
Test2
e0: Employee: Dilbert
e1: Manager: Pointy Haired, Boss
EXERCISE
Dynamic binding
 Decision on which method to run is taken at
runtime
– The virtual machine uses a method table for each
class
Manager m = new Manager();
m.setName(“P.H. Carl”); // Employee.setName
m.setTitle (“Boss”); // Manager.setTitle
m.getSalary (); // Manager.getSalary
Employee e1 = new Manager("Pointy Haired", "Boss");
e1.getSalary();
Is this manager
salary with bonus?
What does this program print?
public class Test1
{
public static void main(String[] args)
{
System.out.println("Test1");
new Test1();
}
public Test1()
{
Employee e0 = new Employee ("Dilbert");
Employee e1 = new Manager ("Pointy", "Boss");
System.out.println(e1.getTitle();
}
} Trick question!
Does not compile since getTitle is not in Employee
EXERCISE
Think About This!
 Why use Concrete Inheritance?
– Powerful implementation approach
– Layered Supertype Pattern
– Enables polymorphism if supertypes are used
– New classes can be added without recompile
 But remember
– Object oriented programming is not just about
concrete inheritance
– It has to be natural!
– Class hierarchies are rigid
– Not always good to force others to extend
Abstract Classes
 Abstract classes put the responsibility of
implementation on subclasses
– Classes extending an abstract class must implement
the abstract methods
– Can contain both concrete and abstract methods
– Normal classes are concrete classes
 Abstract classes cannot be instantiated
 Reference variables of abstract types are
allowed
– Object must be a concrete class
Abstract Example
abstract class Person
{
private String name;
public Person(String name)
{
this.name = name;
}
// get and set methods ...
public abstract String getDescription ();
}
class Employee extends Person
{
public String getDescription()
{
return "Employee called " + super.getName();
}
}
// Person p1 = new Person (); Does not work!
Person p2;
Person p3 = new Employee ("Dilbert");
System.out.println (p3.getDescription());
Key Concept: Polymorphism
Interfaces
Interfaces
 Interface is a class without implementation
– Declaration of how to implement class
– All methods and variables are static final
 Classes implement interfaces
– implements keyword
– Must implement all the methods – or be abstract
Interfaces
public interface Comparable
{
public int compareTo(Object other);
}
class Employee extends Person implements Comparable
{
public int compareTo(Object o)
{ ...
Example
public interface Comparable
{
public int compareTo(Object other);
}
class Employee extends Person implements Comparable
{
public int compareTo(Object o)
{
Employee e = (Employee)o;
return this.getName().compareTo (e.getName());
} ...
Employee[] ale = new Employee[3];
ale[0] = new Employee ("Dilbert");
ale[1] = new Employee ("Wally");
ale[2] = new Employee ("Alice");
Arrays.sort(ale);
for (int j=0; j <ale.length; j++)
System.out.println(ale[j].getName());
Alice
Dilbert
Wally
How is this
possible?
Think About This!
 Class A calls class B -> A depends on B
 Class java.util.Arrays calls the
Employee.compareTo method
 Does Arrays depend on Employee?
Polymorphism
Separated interface
Many Faces
 Arrays looks at the class as Comparable, while
we regard it as Employee
Class Employee
implements Comparable
{
...
compare
Class Arrays
...
sort(Object[]
{
Comparable c ...
Test...
Arrays.sort(persons
Arrays does NOT call Employee.compareTo,
it calls Comaparable.compareTo
which happens to be Employee Polymorphism
Objects
class Employee extends Person
abstract class Person implements Comparable
class Manager extends Employee
Object o = new Manager()
Person p = new Manager()
Comparable c = new Manager()
Employee e = new Manager()
Manager m = new Manager()
Memory for Manager
reference
Must be
concrete class
Can be any
supertype
Using Interfaces
 Interfaces cannot be instantiated
– Variables of interface types can reference objects that
implement the interface
 Interface can extend interfaces
public interface Powered extends Movable
{
double milesPerGallon();
double SPEED_LIMIT = 95;
}
Comarable c = new Comparable (); // NO!!!
Comarable c = new Employee (); // OK!
Why interfaces?
 Why not use abstract classes?
– Only one class can be extended
– Class hierarchies are rigid and not always suitable
 Interfaces can improve software design
– Provide abstraction – hide the implementation
– Classes that use interfaces are not dependant on a
particular implementation
class Employee extends Person implements Comparable
{
...
Example Pattern
 Table Data Gateway or Data Access Object
provide an interface to database table
– Decision on what database access methods to use
can be configured
 Example
public interface TeamDAO extends RuDAO
{
public void addTeam (Team team);
public Collection getTeams ();
} Interface
Implementation
Client
code
Example: Drawing system
public interface Drawable
{
public void draw ();
}
public class Rectangle extends Shape
{
private int h, w;
public Rectangle (int x, int y, int h, int w)
{
this.x=x; this.y=y; this.h=h; this.w=w;
}
public void draw ()
{
System.out.println ("Rectange (x="+x+",y="+y+",h="+h+",w="+w+")");
}
}
public abstract class Shape implements Drawable
{
protected int x,y;
}
public class Circle extends Shape
{
private int r;
public Circle(int x, int y, int r)
{
this.x = x; this.y = y; this.r = r;
}
public void draw()
{
System.out.println ("Circle (x="+x+",y="+y+",r="+r+")");
}
}
Example: Drawing system
 Drawing all objects
– All draw objects implement Drawable
public DrawTest()
{
List<Drawable> l = new ArrayList<Drawable>();
l.add(new Rectangle(1, 1, 1, 1));
l.add(new Circle(2, 1, 1));
l.add(new Rectangle(8, 4, 1, 1));
for (Drawable d: l)
{
d.draw();
}
}
Rectange (x=1,y=1,h=1,w=1)
Circle (x=2,y=1,r=1)
Rectange (x=8,y=4,h=1,w=1)
Think About This!
 All drawing objects in this Layer extend Shape
 Shape is abstract and implements Drawable
 Client code does not know about the classes
that implement Drawable
Shape is Layered Supertype
Shape is Template Method
Generic Programming
X extends Y. Which is true?
A) Correct if and only if X is a class and Y is an interface
B) Correct if and only if X is an interface and Y is a class
C) Correct if X and Y are either both classes or both interfaces
D) Correct for all combinations of X and Y being classes and/or interfaces.
QUIZ
✔
Generic Programming
Generic Programming
 Programming in an data type independent way
– Same code is used regardless of the data type
 Example
– Sort can be applied to any data type
– Generic collection
• Java Collection Framework
 Design Principle
– Always use the most generic data type possible
Generic Programming
 All classes extend Object
– Allows generic algorithms and data structures
static int find (Object[] a, Object key)
{
int i;
for (i=0;i<a.length;i++)
if (a[i].equals(key)) return i;
return -1;
}
Employee[] staff = new Employee[10];
Employee e1 = new Employee("Dilbert");
staff[x] = e1;
int n = find(staff, e1);
Generic Programming
 Generic collections
– ArrayList is an example class that uses Object
ArrayList al = new ArrayList();
al.add (new Employee ("Dilbert"));
al.add (new Employee ("Wally"));
al.add (new Employee ("Alice"));
Iterator i = al.iterator();
Employee e;
while (i.hasNext())
{
e = (Employee)i.next();
System.out.println(e.getName());
}
Dilbert
Wally
Alice
Generic Programming
 Generic collections
– The Collections class is another example
List<Employee> list = new ArrayList<Employee>();
list.add (new Employee ("Dilbert"));
list.add (new Employee ("Wally"));
list.add (new Employee ("Alice"));
Collections.sort(list);
for (Employee e: list)
{
System.out.println(e);
}
Alice
Dilbert
Wally
Reflection
Reflection
 Reflection allows examination and manipulation of
objects at runtime
– Get information about a class
• Fields, methods, constructors, and super classes
• Constants and method declarations belong to an interface
– Create an instance of a class whose name is not known
until runtime
– Get and set the value of an object's field, even if the field
name is unknown to your program until runtime
– Invoke a method on an object, even if the method is not
known until runtime
Reflection
static void showMethods(Object o)
{
Class c = o.getClass();
Method[] theMethods = c.getMethods();
for (int i = 0; i < theMethods.length; i++) {
String methodString = theMethods[i].getName();
System.out.println("Name: " + methodString);
String returnString = theMethods[i].getReturnType().getName();
System.out.println(" Return Type: " + returnString);
Class[] parameterTypes = theMethods[i].getParameterTypes();
System.out.print(" Parameter Types:");
for (int k = 0; k < parameterTypes.length; k ++)
{
String parameterString = parameterTypes[k].getName();
System.out.print(" " + parameterString);
}
System.out.println();
}
} }
Reflection
Bla
public class ReflectMethods
{
public static void main(String[] args)
{
Polygon p = new Polygon();
showMethods(p);
} Name: getBoundingBox
Return Type: java.awt.Rectangle
Parameter Types:
Name: contains
Return Type: boolean
Parameter Types: java.awt.geom.Point2D
...
Name: toString
Return Type: java.lang.String
Parameter Types:
Reflection
 Reflection is very useful in frameworks
– Infrastructure code
– “plumbing” – The “Noise”
 Examples
– Create Java objects from XML descriptions
– Load classes at runtime and invoke methods
– Tools and utilities for development
Dynamically Loading Classes
 Classes can be dynamically loaded at runtime
– Offers the flexibility to decide which class to run
dynamically
– Class names can be specified in configuration files
 Class class
Class instanceClass = Class.forName("RssFeedReader");
reader = (FeedReader)instanceClass.newInstance();
A) BD
B) DB
C) BDC
D) Compilation fails
QUIZ
✔
class Top {
public Top(String s) {
System.out.print("B");
} }
public class Bottom2 extends Top {
public Bottom2(String s) {
System.out.print("D");
}
public static void main(String [] args) {
new Bottom2("C");
System.out.println(" ");
} }
Software Design
Object Oriented Design
 Design and implementation of software needs to
be of quality
– Badly designed, well implemented = problem!
– Well designed, badly implemented = problem!
CODE
HORROR!!
CODE HORROR DUDE
Object Oriented Design
 Good design
Is based on OO principles
Abstracts complex APIs such as J2EE
Is flexible and can be changed
Contains loosely coupled components
 Example from Head First Design Patterns
Getting Started
 SimUDuck is highly successful duck pond
simulation game
 Original design
Change Request
 But now we need the ducks to FLY
Problem!
 But not all duck fly – We forgot Rubber Duck!
How can we fix this?
 Just override fly and quack to do nothing
We even think ahead
 We fix all non-flyable and non-quackable ducks
as well
Code smell!
Which of the following are disadvantages of using inheritance to
provide Duck behavior?
A) Code is duplicated across subclasses
B) Runtime behavior changes are difficult
C) We can’t make ducks dance
D) Hard to gain knowledge of all duck behaviors
E) Ducks can’t fly and quack at the same time
F) Changes can unitentionally affect other ducks
QUIZ
✔
✔
✔
✔
The Problem
 The problem is this
– Derived classes (RubberDuck) are forced to inherit
behaviour they don’t have
– Derived classes (RubberDuck) needs to be exposed
to the inner workings of the superclass (Duck)
– Users of the base class (Duck) should expect same
functionality
– Violation of the Liskov Substitution Principle
Trying to fix the Problem
 Let’s try using interfaces
– Flyable and Quackable Code duplication!
What is the Problem?
 We tried this
– Inheritance changes all subcasses
– Interfaces cause code duplication
 The problem is we are mixing different types of
code in one type of classes
 Fix
– Separate Variation Design Principle
– Take what varies and encapsulate it so it wont affect
the rest of the code
Separate Variations Design Principle
Identify the aspects of
your application that
vary and separate
them from what stays the
same
Separation of Concerns
 Separate what changes from what stays the
same
– Move duck behavior to a separte classes
FlyWithWings flyBehavior = new FlyWithWings();
DATA TYPE IS TOO SPECIFIC
Separation of Concerns
 But the Duck classes cannot use the concrete
behavior classes!
– We need an interface or supertype
FlyBehavior flyBehavior = new FlyWithWings();
INTERFACE - POLYMORPHISIM
The Interface Design Principle
Program to an
interface, not an
implementation
Loose Coupling with Interfaces
 Advantages
– The ability to change the implementing class of any
application object without affecting calling code
– Total freedom in implementing interfaces
– The ability to provide simple test implementations and
stub implementations of application interfaces as
necessary
Program to an interfaces
 Program to an implementation
 Program to interface/subtype
 Program to unknown creation
Dog d = new Dog();
d.bark();
Animal animal = new Dog();
animal.makeSound();
Animal animal = getAnimal();
animal.makeSound();
Program to an interfaces
 Dependency Injection
– Make the caller responsible for setting the dependency
private Animal animal;
public setAnimal(Animal animal)
{
this.animal = animal;
}
...
animal.makeSound();
Injection happens
here, in the
set-method
LOOSE COUPLING = BEAUTIFUL!
Implementing Behavior
 We can add new behaviors without touching the
Duck classes
Integrating the Behavior
 The Duck classes will now delegate its flying
and quacking behavior
Behavior interfaces
Perform the
Bahavior
Integrating the Behavior
 Using the behavior
public class Duck
{
QuackBehavior quackBehavior;
...
public void performQuack()
{
quackBehavior.performQuack()
}
}
We don’t care what kind of object this
is, all we care is that it knows how to
quack!
Integrating the Behavior
 Setting the behavior
public class MallardDuck extends Duck
{
public MallardDuck()
{
quackBehavior = new Quack();
flyBehavior = new FlyWithWings();
}
}
This is not
programming
to an interface!
Setting Behavior Dynamically
 Add two new methods to the Duck class
 Dependency Injection
public void setFlyBehavior(FlyBehavior flyBehavior)
{
this.flyBehavior = flyBehavior
}
public void setQuackBehavior(QuackBehavior quackBehavior)
{
this.quackBehavior = quackBehavior
}
DuckFactory
{
public Duck getMallardDuck()
{
Duck duck = new MallardDuck()
duck.setFlyBehavior(new FlyWithWings());
duck.setQuackBehavior(new Quack());
return duck;
}
}
Setting Behavior Dynamically
 The idea
– Don´t think: Mallard is-a flying duck, think: it has-a
flying behavior
– Putting two classes together where one is a member
in the other is a composition
 Creating systems using composition give
flexibilty
– You can change the behavior at runtime
Composition Design Principle
Favor composition over
inheritance
Object Composition
 Problems with concrete inheritance
– Class hierarchy can get rigid
– Difficult to change the implementation
 Object Composition is more flexible
– Allows the behaviour of an object to be altered at run
time, through delegating part of its behaviour to an
interface and allowing callers to set the
implementation of that interface
Summary
 OO Programming is powerful
– If used correctly
– Remember Encapsulation, Interfaces, Polymorphism
 Generic programming
– Using classes, abstract classes and interfaces can
lead to powerful and flexible programs
 Reflection
– Powerful for building infrastructure
Job interview question
You are given the assignment of creating a component that needs to
know sales statistics of Lottery tickets. You know that there is a
another component in the system, Sale Server, that handles the sale.
You need real-time information. What would you suggest?
EXERCISE
Design Patterns
 Design pattern is a general solution to a common
problem in software design
– Systematic approach for problems that reoccur in software
development
– Not complete solution but starting point for design
– Not code ready to use
– Patterns have names and definitions
– Built on common practices
 Patterns should not be language dependant
– However patterns apply for types of programming
languages
Next
 Design Patterns

Contenu connexe

Tendances

Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++Hoang Nguyen
 
Python: Common Design Patterns
Python: Common Design PatternsPython: Common Design Patterns
Python: Common Design PatternsDamian T. Gordon
 
Abstract data types
Abstract data typesAbstract data types
Abstract data typesHoang Nguyen
 
Java SE 8 - New Features
Java SE 8 - New FeaturesJava SE 8 - New Features
Java SE 8 - New FeaturesNaveen Hegde
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)Durga Devi
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and VariablesNilesh Dalvi
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Simon Ritter
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objectsmaznabili
 
Sedna XML Database System: Internal Representation
Sedna XML Database System: Internal RepresentationSedna XML Database System: Internal Representation
Sedna XML Database System: Internal RepresentationIvan Shcheklein
 
Introduction to programming language using c
Introduction to programming language using cIntroduction to programming language using c
Introduction to programming language using cTrinity Dwarka
 
Core java concepts
Core java  conceptsCore java  concepts
Core java conceptsRam132
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...Sagar Verma
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHPMichael Peacock
 

Tendances (18)

Object oriented programming using c++
Object oriented programming using c++Object oriented programming using c++
Object oriented programming using c++
 
Python: Common Design Patterns
Python: Common Design PatternsPython: Common Design Patterns
Python: Common Design Patterns
 
Abstract data types
Abstract data typesAbstract data types
Abstract data types
 
Java SE 8 - New Features
Java SE 8 - New FeaturesJava SE 8 - New Features
Java SE 8 - New Features
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
C++ classes
C++ classesC++ classes
C++ classes
 
3. Data types and Variables
3. Data types and Variables3. Data types and Variables
3. Data types and Variables
 
Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8Improved Developer Productivity In JDK8
Improved Developer Productivity In JDK8
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
 
Sedna XML Database System: Internal Representation
Sedna XML Database System: Internal RepresentationSedna XML Database System: Internal Representation
Sedna XML Database System: Internal Representation
 
Unit v(dsc++)
Unit v(dsc++)Unit v(dsc++)
Unit v(dsc++)
 
Introduction to programming language using c
Introduction to programming language using cIntroduction to programming language using c
Introduction to programming language using c
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
 

Similaire à L02 Software Design

Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
Take the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphpTake the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphpAlena Holligan
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.pptParikhitGhosh1
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshowilias ahmed
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented ProgrammingIqra khalil
 

Similaire à L02 Software Design (20)

L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
oops-1
oops-1oops-1
oops-1
 
Unit i
Unit iUnit i
Unit i
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
L04 Software Design 2
L04 Software Design 2L04 Software Design 2
L04 Software Design 2
 
Design pattern
Design patternDesign pattern
Design pattern
 
Take the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphpTake the Plunge with OOP from #pnwphp
Take the Plunge with OOP from #pnwphp
 
06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt06 InheritanceAndPolymorphism.ppt
06 InheritanceAndPolymorphism.ppt
 
Oop features java presentationshow
Oop features java presentationshowOop features java presentationshow
Oop features java presentationshow
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Only oop
Only oopOnly oop
Only oop
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
04inherit
04inherit04inherit
04inherit
 

Plus de Ólafur Andri Ragnarsson

New Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course IntroductionNew Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course IntroductionÓlafur Andri Ragnarsson
 
New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine Ólafur Andri Ragnarsson
 

Plus de Ólafur Andri Ragnarsson (20)

Nýsköpun - Leiðin til framfara
Nýsköpun - Leiðin til framfaraNýsköpun - Leiðin til framfara
Nýsköpun - Leiðin til framfara
 
Nýjast tækni og framtíðin
Nýjast tækni og framtíðinNýjast tækni og framtíðin
Nýjast tækni og framtíðin
 
New Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course IntroductionNew Technology Summer 2020 Course Introduction
New Technology Summer 2020 Course Introduction
 
L01 Introduction
L01 IntroductionL01 Introduction
L01 Introduction
 
L23 Robotics and Drones
L23 Robotics and Drones L23 Robotics and Drones
L23 Robotics and Drones
 
L22 Augmented and Virtual Reality
L22 Augmented and Virtual RealityL22 Augmented and Virtual Reality
L22 Augmented and Virtual Reality
 
L20 Personalised World
L20 Personalised WorldL20 Personalised World
L20 Personalised World
 
L19 Network Platforms
L19 Network PlatformsL19 Network Platforms
L19 Network Platforms
 
L18 Big Data and Analytics
L18 Big Data and AnalyticsL18 Big Data and Analytics
L18 Big Data and Analytics
 
L17 Algorithms and AI
L17 Algorithms and AIL17 Algorithms and AI
L17 Algorithms and AI
 
L16 Internet of Things
L16 Internet of ThingsL16 Internet of Things
L16 Internet of Things
 
L14 From the Internet to Blockchain
L14 From the Internet to BlockchainL14 From the Internet to Blockchain
L14 From the Internet to Blockchain
 
L14 The Mobile Revolution
L14 The Mobile RevolutionL14 The Mobile Revolution
L14 The Mobile Revolution
 
New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine New Technology 2019 L13 Rise of the Machine
New Technology 2019 L13 Rise of the Machine
 
L12 digital transformation
L12 digital transformationL12 digital transformation
L12 digital transformation
 
L10 The Innovator's Dilemma
L10 The Innovator's DilemmaL10 The Innovator's Dilemma
L10 The Innovator's Dilemma
 
L09 Disruptive Technology
L09 Disruptive TechnologyL09 Disruptive Technology
L09 Disruptive Technology
 
L09 Technological Revolutions
L09 Technological RevolutionsL09 Technological Revolutions
L09 Technological Revolutions
 
L07 Becoming Invisible
L07 Becoming InvisibleL07 Becoming Invisible
L07 Becoming Invisible
 
L06 Diffusion of Innovation
L06 Diffusion of InnovationL06 Diffusion of Innovation
L06 Diffusion of Innovation
 

Dernier

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Dernier (20)

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

L02 Software Design

  • 2. Agenda  Programming with Objects – Classes – Interfaces – Generic programming – Reflection  Software Design – Ducks…
  • 3. Reading  Don’t Repeat Yourself  Polymorphism  Separation of concerns  Loose coupling  Optional: – http://docs.oracle.com/javase/tutorial/
  • 5. Object Oriented Programming  Object Oriented programming can be powerful – One of the best ways for general purpose computing  But – The power of object oriented languages needs to be used properly!  However – Programmers tend to forget the power of OO
  • 7. Object Oriented Programming  Programming languages with objects – Objects hold data and methods – Object variables (reference) point to objects  Object Oriented – Object are instances of classes, created with new – Classes describe the objects – Classes extend other classes – inheritance – Instance variables are encapsulated – Methods manipulate instance variable
  • 8. Explain these concepts and why they are important in programming Encapsulation Interfaces Polymorphism EXERCISE
  • 9. Think About This!  Object-oriented programming – Is not about class inheritance and creating advanced class diagrams  Remember – Encapsulation – Hiding data – Interfaces – Hiding implementation – Polymorphism – Flexible and Generic Programming  Powerful programming – Separation of concerns – Separating what varies from what stays the same
  • 10. Think About This!  Object-oriented programming – Is not about class inheritance and creating advanced class diagrams  Remember – Encapsulation – Hiding data – Interfaces – Hiding implementation – Polymorphism – Flexible and Generic Programming  Powerful programming – Separation of concerns – Separating what varies from what stays the same
  • 11. Separate Variations Design Principle Identify the aspects of your application that vary and separate them from what stays the same
  • 12. Don’t Repeat Yourself – DRY  Single source of truth Every piece of knowledge must have a single, unambiguous, authorit ative representation within a system
  • 13. Object Oriented Design  Design of classes and interfaces – Class diagram shows relationships – Sequence diagrams show flows  Design Patterns – Reoccurring solutions in design – “Best practices” – known solutions for common problems
  • 15. Objects  Object created  Object used Date day1; Date day2 = new Date(); System.out.println (new Date ()); String s = new Date().toString (); day1 null day2 Date Date now = new Date(); if (day2.before(now)) { System.out.println (day2.toString()); } before tostring ...
  • 16. Objects  Operator new creates memory – String is an exception  Reference variables always point to some memory day1 null day2 Date before tostring ... Date d = new Date(); Must be a concrete class Can be any supertype or interface of the concrete class
  • 17. Classes Object +getId() : String -id : int -name : String -username : String -password : String -email : String User  Classes extend other classes – Concrete Inheritance – Subtype extends supertype  Class contains – Instance variables – Methods  Reference variables of type Object points to any class – Any supertype can reference subtype Object obj = new User (); User u = (User)obj; supertype subtype
  • 18. Class methods  Methods can be overridden – Class extends a class and overrides a method  Methods can be overloaded – Same method with different parameters  Methods can be – public – any class can call the method – private – only available within the class – protected – only available within the class and extended classes
  • 19. Class A inherits class B. A overwrites method f. Variable b is created like this: B b = new A(); What happens when this line is run: b.f(); A) The method in A is run B) The method in B is run C) First the method in B is run, then the method in A D) The new statement is illegal and does not compile QUIZ ✔
  • 20. Classes  References – Point to concrete objects – Must be same type or supertype of concrete object – Can be interface or abstract class Object obj = new Date (); Date d = (Date)obj;
  • 21. Constructors  Classes have constructor – Instantiation methods – Same name as the class  References – this – refers to the class – super – extended class public Employee (String name, double salary) { this (name); this.salary = salary; } public Manager (String name) { super (name, 0.0); ... }
  • 22. class Point { private int x, y; public Point () { x = 0; y = 0; } public Point (int x, int y) { this.x = x; this.y = y; } public void move (intdx, intdy) { x+=dx;y+=dy; } public String toString () { return "(" + x + "," + y + ")"; } this used to refer to the class variables Override toString method of Object Class variables Default constructors Overloaded constructor Example
  • 23. Example Bla public class Test { public static void main (String[] args) { System.out.println ("Test"); Test test = new Test(); } public Test () { Point p0; // null reference Point p1 = new Point (); Point p2 = new Point (1,2); Object obj = p2; p0 = (Point)obj; p0.move (1, 1); System.out.println("p0=" + p0); } } C:java>javac Test.java C:java>java Test Test p0=(2,3) X = 2 Y = 3
  • 24. Java uses this method to pass objects to methods A) Call by reference B) Call by value C) Call by object reference D) Call by value reference QUIZ ✔
  • 25. Call By Value  Methods use call by value – Object references are passed by value – The reference cannot change, but the object can x: y: 42 0 Point void changeReferance(Point p) { while (p.x>0) p.x--; } p Point p = new Point (42,0); changeReferance(p); System.out.println(p.x); Reference p to Point Local copy of a reference p to Point p is same as this.p p
  • 26. Inheritance  Classes extend other classes – Subclasses extend superclasses  Reference variables of super types can reference objects of subtypes Employee Manager Empolyee e; e = new Employee(. . .) e = new Manager(. . .) class Manager extends Employee { ... } Polymorphism
  • 27. public class Employee { private String name; private double salary; private Date hireDate; public Employee() { } public Employee(String name, double salary, Date hireDate) { this.name = name; this.salary = salary; this.hireDate = hireDate; } public Employee(String name) { this.name = name; }
  • 28. public String getName() { return name; } public double getSalary() { return salary; } public void setName(String name) { this.name = name; } public String toString() { return "Employee: " + getName(); } }
  • 29. Bla class Manager extends Employee { String title; double bonus; public Manager (String name, String title) { super (name); this.title = title; } public String getTitle () { return title; } public double getSalary() { return this.bonus + super.getSalery(); } public String toString () { return "Manager: " + getName() + ", " + getTitle (); } } New method New variables Overridden methods
  • 30. What does this program print? public class Test2 { public static void main (String[] args) { System.out.println ("Test2"); Test2 test2 = new Test2(); } public Test2 () { Employee e0 = new Employee ("Dilbert"); Employee e1 = new Manager ("Pointy Haired", "Boss"); System.out.println("e0: " + e0); System.out.println("e1: " + e1); } } C:java>java Test2 Test2 e0: Employee: Dilbert e1: Manager: Pointy Haired, Boss EXERCISE
  • 31. Dynamic binding  Decision on which method to run is taken at runtime – The virtual machine uses a method table for each class Manager m = new Manager(); m.setName(“P.H. Carl”); // Employee.setName m.setTitle (“Boss”); // Manager.setTitle m.getSalary (); // Manager.getSalary Employee e1 = new Manager("Pointy Haired", "Boss"); e1.getSalary(); Is this manager salary with bonus?
  • 32. What does this program print? public class Test1 { public static void main(String[] args) { System.out.println("Test1"); new Test1(); } public Test1() { Employee e0 = new Employee ("Dilbert"); Employee e1 = new Manager ("Pointy", "Boss"); System.out.println(e1.getTitle(); } } Trick question! Does not compile since getTitle is not in Employee EXERCISE
  • 33. Think About This!  Why use Concrete Inheritance? – Powerful implementation approach – Layered Supertype Pattern – Enables polymorphism if supertypes are used – New classes can be added without recompile  But remember – Object oriented programming is not just about concrete inheritance – It has to be natural! – Class hierarchies are rigid – Not always good to force others to extend
  • 34. Abstract Classes  Abstract classes put the responsibility of implementation on subclasses – Classes extending an abstract class must implement the abstract methods – Can contain both concrete and abstract methods – Normal classes are concrete classes  Abstract classes cannot be instantiated  Reference variables of abstract types are allowed – Object must be a concrete class
  • 35. Abstract Example abstract class Person { private String name; public Person(String name) { this.name = name; } // get and set methods ... public abstract String getDescription (); } class Employee extends Person { public String getDescription() { return "Employee called " + super.getName(); } } // Person p1 = new Person (); Does not work! Person p2; Person p3 = new Employee ("Dilbert"); System.out.println (p3.getDescription()); Key Concept: Polymorphism
  • 37. Interfaces  Interface is a class without implementation – Declaration of how to implement class – All methods and variables are static final  Classes implement interfaces – implements keyword – Must implement all the methods – or be abstract
  • 38. Interfaces public interface Comparable { public int compareTo(Object other); } class Employee extends Person implements Comparable { public int compareTo(Object o) { ...
  • 39. Example public interface Comparable { public int compareTo(Object other); } class Employee extends Person implements Comparable { public int compareTo(Object o) { Employee e = (Employee)o; return this.getName().compareTo (e.getName()); } ... Employee[] ale = new Employee[3]; ale[0] = new Employee ("Dilbert"); ale[1] = new Employee ("Wally"); ale[2] = new Employee ("Alice"); Arrays.sort(ale); for (int j=0; j <ale.length; j++) System.out.println(ale[j].getName()); Alice Dilbert Wally How is this possible?
  • 40. Think About This!  Class A calls class B -> A depends on B  Class java.util.Arrays calls the Employee.compareTo method  Does Arrays depend on Employee? Polymorphism Separated interface
  • 41. Many Faces  Arrays looks at the class as Comparable, while we regard it as Employee Class Employee implements Comparable { ... compare Class Arrays ... sort(Object[] { Comparable c ... Test... Arrays.sort(persons Arrays does NOT call Employee.compareTo, it calls Comaparable.compareTo which happens to be Employee Polymorphism
  • 42. Objects class Employee extends Person abstract class Person implements Comparable class Manager extends Employee Object o = new Manager() Person p = new Manager() Comparable c = new Manager() Employee e = new Manager() Manager m = new Manager() Memory for Manager reference Must be concrete class Can be any supertype
  • 43. Using Interfaces  Interfaces cannot be instantiated – Variables of interface types can reference objects that implement the interface  Interface can extend interfaces public interface Powered extends Movable { double milesPerGallon(); double SPEED_LIMIT = 95; } Comarable c = new Comparable (); // NO!!! Comarable c = new Employee (); // OK!
  • 44. Why interfaces?  Why not use abstract classes? – Only one class can be extended – Class hierarchies are rigid and not always suitable  Interfaces can improve software design – Provide abstraction – hide the implementation – Classes that use interfaces are not dependant on a particular implementation class Employee extends Person implements Comparable { ...
  • 45. Example Pattern  Table Data Gateway or Data Access Object provide an interface to database table – Decision on what database access methods to use can be configured  Example public interface TeamDAO extends RuDAO { public void addTeam (Team team); public Collection getTeams (); } Interface Implementation Client code
  • 46. Example: Drawing system public interface Drawable { public void draw (); } public class Rectangle extends Shape { private int h, w; public Rectangle (int x, int y, int h, int w) { this.x=x; this.y=y; this.h=h; this.w=w; } public void draw () { System.out.println ("Rectange (x="+x+",y="+y+",h="+h+",w="+w+")"); } } public abstract class Shape implements Drawable { protected int x,y; } public class Circle extends Shape { private int r; public Circle(int x, int y, int r) { this.x = x; this.y = y; this.r = r; } public void draw() { System.out.println ("Circle (x="+x+",y="+y+",r="+r+")"); } }
  • 47. Example: Drawing system  Drawing all objects – All draw objects implement Drawable public DrawTest() { List<Drawable> l = new ArrayList<Drawable>(); l.add(new Rectangle(1, 1, 1, 1)); l.add(new Circle(2, 1, 1)); l.add(new Rectangle(8, 4, 1, 1)); for (Drawable d: l) { d.draw(); } } Rectange (x=1,y=1,h=1,w=1) Circle (x=2,y=1,r=1) Rectange (x=8,y=4,h=1,w=1)
  • 48. Think About This!  All drawing objects in this Layer extend Shape  Shape is abstract and implements Drawable  Client code does not know about the classes that implement Drawable Shape is Layered Supertype Shape is Template Method Generic Programming
  • 49. X extends Y. Which is true? A) Correct if and only if X is a class and Y is an interface B) Correct if and only if X is an interface and Y is a class C) Correct if X and Y are either both classes or both interfaces D) Correct for all combinations of X and Y being classes and/or interfaces. QUIZ ✔
  • 51. Generic Programming  Programming in an data type independent way – Same code is used regardless of the data type  Example – Sort can be applied to any data type – Generic collection • Java Collection Framework  Design Principle – Always use the most generic data type possible
  • 52. Generic Programming  All classes extend Object – Allows generic algorithms and data structures static int find (Object[] a, Object key) { int i; for (i=0;i<a.length;i++) if (a[i].equals(key)) return i; return -1; } Employee[] staff = new Employee[10]; Employee e1 = new Employee("Dilbert"); staff[x] = e1; int n = find(staff, e1);
  • 53. Generic Programming  Generic collections – ArrayList is an example class that uses Object ArrayList al = new ArrayList(); al.add (new Employee ("Dilbert")); al.add (new Employee ("Wally")); al.add (new Employee ("Alice")); Iterator i = al.iterator(); Employee e; while (i.hasNext()) { e = (Employee)i.next(); System.out.println(e.getName()); } Dilbert Wally Alice
  • 54. Generic Programming  Generic collections – The Collections class is another example List<Employee> list = new ArrayList<Employee>(); list.add (new Employee ("Dilbert")); list.add (new Employee ("Wally")); list.add (new Employee ("Alice")); Collections.sort(list); for (Employee e: list) { System.out.println(e); } Alice Dilbert Wally
  • 56. Reflection  Reflection allows examination and manipulation of objects at runtime – Get information about a class • Fields, methods, constructors, and super classes • Constants and method declarations belong to an interface – Create an instance of a class whose name is not known until runtime – Get and set the value of an object's field, even if the field name is unknown to your program until runtime – Invoke a method on an object, even if the method is not known until runtime
  • 57. Reflection static void showMethods(Object o) { Class c = o.getClass(); Method[] theMethods = c.getMethods(); for (int i = 0; i < theMethods.length; i++) { String methodString = theMethods[i].getName(); System.out.println("Name: " + methodString); String returnString = theMethods[i].getReturnType().getName(); System.out.println(" Return Type: " + returnString); Class[] parameterTypes = theMethods[i].getParameterTypes(); System.out.print(" Parameter Types:"); for (int k = 0; k < parameterTypes.length; k ++) { String parameterString = parameterTypes[k].getName(); System.out.print(" " + parameterString); } System.out.println(); } } }
  • 58. Reflection Bla public class ReflectMethods { public static void main(String[] args) { Polygon p = new Polygon(); showMethods(p); } Name: getBoundingBox Return Type: java.awt.Rectangle Parameter Types: Name: contains Return Type: boolean Parameter Types: java.awt.geom.Point2D ... Name: toString Return Type: java.lang.String Parameter Types:
  • 59. Reflection  Reflection is very useful in frameworks – Infrastructure code – “plumbing” – The “Noise”  Examples – Create Java objects from XML descriptions – Load classes at runtime and invoke methods – Tools and utilities for development
  • 60. Dynamically Loading Classes  Classes can be dynamically loaded at runtime – Offers the flexibility to decide which class to run dynamically – Class names can be specified in configuration files  Class class Class instanceClass = Class.forName("RssFeedReader"); reader = (FeedReader)instanceClass.newInstance();
  • 61. A) BD B) DB C) BDC D) Compilation fails QUIZ ✔ class Top { public Top(String s) { System.out.print("B"); } } public class Bottom2 extends Top { public Bottom2(String s) { System.out.print("D"); } public static void main(String [] args) { new Bottom2("C"); System.out.println(" "); } }
  • 63. Object Oriented Design  Design and implementation of software needs to be of quality – Badly designed, well implemented = problem! – Well designed, badly implemented = problem! CODE HORROR!! CODE HORROR DUDE
  • 64. Object Oriented Design  Good design Is based on OO principles Abstracts complex APIs such as J2EE Is flexible and can be changed Contains loosely coupled components
  • 65.
  • 66.  Example from Head First Design Patterns
  • 67. Getting Started  SimUDuck is highly successful duck pond simulation game  Original design
  • 68. Change Request  But now we need the ducks to FLY
  • 69. Problem!  But not all duck fly – We forgot Rubber Duck!
  • 70. How can we fix this?  Just override fly and quack to do nothing
  • 71. We even think ahead  We fix all non-flyable and non-quackable ducks as well Code smell!
  • 72. Which of the following are disadvantages of using inheritance to provide Duck behavior? A) Code is duplicated across subclasses B) Runtime behavior changes are difficult C) We can’t make ducks dance D) Hard to gain knowledge of all duck behaviors E) Ducks can’t fly and quack at the same time F) Changes can unitentionally affect other ducks QUIZ ✔ ✔ ✔ ✔
  • 73. The Problem  The problem is this – Derived classes (RubberDuck) are forced to inherit behaviour they don’t have – Derived classes (RubberDuck) needs to be exposed to the inner workings of the superclass (Duck) – Users of the base class (Duck) should expect same functionality – Violation of the Liskov Substitution Principle
  • 74. Trying to fix the Problem  Let’s try using interfaces – Flyable and Quackable Code duplication!
  • 75. What is the Problem?  We tried this – Inheritance changes all subcasses – Interfaces cause code duplication  The problem is we are mixing different types of code in one type of classes  Fix – Separate Variation Design Principle – Take what varies and encapsulate it so it wont affect the rest of the code
  • 76. Separate Variations Design Principle Identify the aspects of your application that vary and separate them from what stays the same
  • 77. Separation of Concerns  Separate what changes from what stays the same – Move duck behavior to a separte classes FlyWithWings flyBehavior = new FlyWithWings(); DATA TYPE IS TOO SPECIFIC
  • 78. Separation of Concerns  But the Duck classes cannot use the concrete behavior classes! – We need an interface or supertype FlyBehavior flyBehavior = new FlyWithWings(); INTERFACE - POLYMORPHISIM
  • 79. The Interface Design Principle Program to an interface, not an implementation
  • 80. Loose Coupling with Interfaces  Advantages – The ability to change the implementing class of any application object without affecting calling code – Total freedom in implementing interfaces – The ability to provide simple test implementations and stub implementations of application interfaces as necessary
  • 81. Program to an interfaces  Program to an implementation  Program to interface/subtype  Program to unknown creation Dog d = new Dog(); d.bark(); Animal animal = new Dog(); animal.makeSound(); Animal animal = getAnimal(); animal.makeSound();
  • 82. Program to an interfaces  Dependency Injection – Make the caller responsible for setting the dependency private Animal animal; public setAnimal(Animal animal) { this.animal = animal; } ... animal.makeSound(); Injection happens here, in the set-method LOOSE COUPLING = BEAUTIFUL!
  • 83. Implementing Behavior  We can add new behaviors without touching the Duck classes
  • 84. Integrating the Behavior  The Duck classes will now delegate its flying and quacking behavior Behavior interfaces Perform the Bahavior
  • 85. Integrating the Behavior  Using the behavior public class Duck { QuackBehavior quackBehavior; ... public void performQuack() { quackBehavior.performQuack() } } We don’t care what kind of object this is, all we care is that it knows how to quack!
  • 86. Integrating the Behavior  Setting the behavior public class MallardDuck extends Duck { public MallardDuck() { quackBehavior = new Quack(); flyBehavior = new FlyWithWings(); } } This is not programming to an interface!
  • 87. Setting Behavior Dynamically  Add two new methods to the Duck class  Dependency Injection public void setFlyBehavior(FlyBehavior flyBehavior) { this.flyBehavior = flyBehavior } public void setQuackBehavior(QuackBehavior quackBehavior) { this.quackBehavior = quackBehavior } DuckFactory { public Duck getMallardDuck() { Duck duck = new MallardDuck() duck.setFlyBehavior(new FlyWithWings()); duck.setQuackBehavior(new Quack()); return duck; } }
  • 88. Setting Behavior Dynamically  The idea – Don´t think: Mallard is-a flying duck, think: it has-a flying behavior – Putting two classes together where one is a member in the other is a composition  Creating systems using composition give flexibilty – You can change the behavior at runtime
  • 89. Composition Design Principle Favor composition over inheritance
  • 90. Object Composition  Problems with concrete inheritance – Class hierarchy can get rigid – Difficult to change the implementation  Object Composition is more flexible – Allows the behaviour of an object to be altered at run time, through delegating part of its behaviour to an interface and allowing callers to set the implementation of that interface
  • 91. Summary  OO Programming is powerful – If used correctly – Remember Encapsulation, Interfaces, Polymorphism  Generic programming – Using classes, abstract classes and interfaces can lead to powerful and flexible programs  Reflection – Powerful for building infrastructure
  • 92. Job interview question You are given the assignment of creating a component that needs to know sales statistics of Lottery tickets. You know that there is a another component in the system, Sale Server, that handles the sale. You need real-time information. What would you suggest? EXERCISE
  • 93. Design Patterns  Design pattern is a general solution to a common problem in software design – Systematic approach for problems that reoccur in software development – Not complete solution but starting point for design – Not code ready to use – Patterns have names and definitions – Built on common practices  Patterns should not be language dependant – However patterns apply for types of programming languages

Notes de l'éditeur

  1. OO is one way – not the only wayFor general purpose computations, OO is really good