4. Conwey’s Law
Organisations which design systems ... are constrained to
produce designs which are copies of the communication
structures of these organisations
From a 1967 paper “How do Committees Invent?”
by Mel Conway
5. Monolithic Architecture
Traditional Web Application Architecture
All code is built into a single application
that is deployed
Simple to develop, test, deploy, scale
Clear layering: Presentation, Domain,
Data Source
Tomcat
ApacheBrowser
WAR
Web Components
Customer
Wallet
Data Source
DB
WAR file = Web
ARchive
Tomcat = web server
for Java Servlets (web
components)
6. Monolithic Architecture
▪ Drawbacks
– User interface challenge – old style UI architecture
– Real-time applications (like node.js) don’t fit in easy
– Obstacle to frequent deployment – fear of change
– Overloads your IDE and container – slow build, development
– Obstacle to scaling development teams
– Locks down the technology stack – long term commitment
7. Technical Debt
Concept in programming that reflects the extra development
work that arises when code that is easy to implement in the
short run is used instead of applying the best overall solution
Small decisions that accumulate over time -
“I’ll fix this later”
Hard to see until any small change is
extremely expensive (CoC = Cost of
Change), and then Conway’s Second Law
applies
If the debt is not repaid, then it will keep on
accumulating interest, making it hard to
implement changes later on
8. Why does it Happen?
Or maybe… University Teachers that teach about concrete inheritance
Object Oriented progamming is good but can be misused
9. Architecture - noun
The decomposition of a
product into a collection of
components/modules and
interactions
Structure
11. A component is a software building block that is
independently replaceable
independently upgradable
Component
12. Architecture - types
Application Architecture: Application is the focus
System Architecture: Focus on multiple applications across a
number of tiers and technologies
Software Architecture: The combination of application and
system architecture and includes the technical practices to build
the software
13. Service Oriented Architecture
SOA actually means that components of an application
act as interoperable services, and can be used
independently and recombined into other applications.
Engineering Software as a Service by David Patterson and Armando Fox
14. Microservices
In recent years a new term has emerged, Microservices:
The microservice architectural style is an approach to
developing a single application as a suite of small services, each
running in its own process and communicating with lightweight
mechanisms, often an HTTP resource API.
From the Lewis & Fowler article
15. Layering
• Software systems can get complicated
• Abstractions are needed
• Layering provides abstraction by separating computer systems in layers
• Higher layers use services from
lower layers
• Each layer has dedicated tasks
and hides complexity from upper
layers
18. 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!
Programmers tend to forget the power of OO
20. Object Oriented Programming
Programming languages with objects
Objects hold data and methods
Object variables (reference) point to objects
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
22. 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
Object Oriented Programming
23. Powerful programming - beautiful programming
Right use of polymorphism
Separation of concerns
Separating what varies from what stays the same
Object Oriented Programming
24. Powerful programming - beatiful programming
Right use of polymorphism
Separation of concerns
Separating what varies from what stays the same
Object Oriented Programming
This is the secret of powerful programming
25. Powerful programming - beatiful programming
Right use of polymorphism
Separation of concerns
Right use of Polymorphism
Object Oriented Programming
Beautiful programming
26. Separate Variations Design Principle
Identify the aspects of your
application that vary and separate
them from what stays the same
27. Every piece of knowledge must have
a single, unambiguous, authoritative
representation within a system
Don’t Repeat Yourself – DRY
28. 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
Object Oriented Programming
29. Remember technical dept discussion…
University teachers talking about concreate inheritance?
Object Oriented Programming
30. Object Oriented Programming
Concrete inheritance did not make it to the list of powerful features of
object oriented programming
In fact, its the most dangerous thing you can do in programs and
needs to be used as such
Directly causes technical debt
It’s extremely easy to violate Liskov’s Principle, make code brittle and
rise the CoC
32. Subtypes must be substitutable for
their base types. Code that uses
references to base class must be
able to use objects of derived
classes without knowing it.
Liskov Substitution Principle
33. public class Rectangle {
protected int _width;
protected int _height;
public int getWidth() {
return _width;
}
public int getHeight() {
return _height;
}
public void setWidth(int width) {
_width = width;
}
public void setHeight(int height) {
_height = height;
}
}
Liskov Substitution Principle
34. public class Square extends Rectangle {
public void setWidth(int width) {
_width = width;
_height = width;
}
public void setHeight(int height) {
_height = height;
_width = _height;
}
}
Implementation convenience
Liskov Substitution Principle
35. import junit.framework.Assert;
import org.junit.Test;
public class RectangleTests {
@Test
public void areaOfRectangle() {
Rectangle r = new Square();
r.setWidth(5);
r.setHeight(2);
// Will Fail - r is a square and sets
// width and height equal to each other.
Assert.assertEquals(r.getWidth() * r.getHeight(),10);
}
}
Liskov Substitution Principle
36. 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
38. Objects
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
...
Object created
Object used
39. Objects
Date d = new Date();
Operator new creates memory
String is an exception
Reference variables always point
to some memory or null
day1 null
day2
Date
before
tostring
...
Can
be
any
supertype
or
interface
of
the
concrete
class
Must
be
a
concrete
class
40. Classes
Object
+getId() : String
-id : int
-name : String
-username : String
-password : String
-email : String
User
Object obj = new User ();
User u = (User)obj;
supertype
subtype
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
41. 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
42. 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
43. 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;
Dateobj
Reference of type Object
Memory space holding object Date
Concrete object of type Date
Left side = type or supertype
or interface og abstract class
right side = concrete class
with memory space
44. 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);
...
}
45. 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 + ")";
}
}
Example
Class variables
Default constuctor
Overloaded constuctor
this used to refer to the
class variables
Override toString method
of Object
46. Example
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)
p0 p1
x=0
y=0
p2
x=1
y=2
obj
x=2
y=3
47. Java
uses
this
method
to
pass
objects
reference
to
methods
A) Call
by
reference
B) Call
by
value
C) Call
by
method
reference
D) Call
by
value
reference
QUIZ
48. 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
49. Inheritance
▪ Classes extend other classes
– Subclasses extend superclasses
▪ Reference variables of super types can reference objects of subtypes
Empolyee e;
e = new Employee(. . .)
e = new Manager(. . .)
class Manager extends Employee
{ ... }
Polymorphism
Employee
Manager
extends
50. 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;
}
51. public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public void setName(String name)
{
this.name = name;
}
public String toString()
{
return "Employee: " + getName();
}
}
52. 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 String toString ()
{
return "Manager: " + getName() +
", " + getTitle ();
}
}
New variables
New method
Overridden
method
53. 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
54. 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
Employee e1 = new Manager("Pointy Haired", “Boss");
e1.getTitle();
what method is run?
55. 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
56. class Manager extends Employee
{
private double bonus;
public void setBonus(double bonus)
{
this.bonus = bonus;
}
public double getSalary()
{
return this.bonus + super.getSalery();
}
Manager m = new Manager("Fay", "Boss");
m.setBonus(200);
Violates the Liskov substitution principle
WHAT IS POSSIBLE WRONG WITH THIS?
// meantime, somewhere else
Employee e = (Employee)something.getEmployees();
m.setSalary(100);
System.out.println(e.getSalary()); // should print 100
ATH
57. 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
58. 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
59. 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
Abstract method
Must implement
61. 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
63. 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?
64. 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
<interface>
Comparable
Arrays Employee
implements
calls
65. 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 = many faces
66. 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
67. 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!
68. 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
{
...
69. 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 ();
}
Client
Code
Interface Implementation
70. 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+")");
}
}
71. 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)
72. 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 Layer Supertype
Shape is Template Method
Generic Programming
73. 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