Advanced Programming
SUT • Spring 2019
OOP Design
Design Patterns
Defines a protocol
an interface promises a certain functionality
All the classes implementing the interface provide their own implementations for the promised functionality
A class combines the state and the behavior of a real object
An interface specifies (only) the behavior of an abstract entity
Comparable<T>
int compareTo(T o)
java.io.Serializable
No method
Sometimes, interfaces have no method
The interface itself, is the protocol
Collection, Itertable, Iterator
Generics
Inheritance
Composition
interface
Inheritance: is-a
Composition: has-a
Circle has a center
Other examples?
public class Circle {
private Point center;
}
You can make a base class
And put the common functionality of many classes in it
But always check:
Whether the is-a relationship exists between the derived classes and the base class
If the is-a relationship does not hold, Use composition instead of inheritance
Favor Composition over Inheritance
public Class Sorting {
public list sort(List list) {
// sort implementation
return list;
}
}
class DynamicDataSet extends Sorting {
// DynamicDataSet implementation
}
class SnapshotDataSet extends Sorting {
// SnapshotDataSet implementation
}
is-a
DynamicDataSet is-a Sorting? No.
What if the two types of data set classes have a genuine base class, DataSet?
Favor composition over inheritance
Use composition to get code that is easy to change
and loosely coupled
Make your classes dependent on interfaces, not on the actual implementation
findBest(List<T>)
class DynamicDataSet { Sorting sorting; }
Do not depend on MergeSorting
Refactoring techniques are widely supported by IDEs
Practice it in Intelij
The Rule of Three:
Refactor When You Add Function
Refactor When You Need to Fix a Bug
Refactor As You Do a Code Review
public class Main {
public void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
int a1 = s.nextInt();
System.out.print("Enter the length: ");
int a2 = s.nextInt();
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
int b1 = s.nextInt();
System.out.print("Enter the length: ");
int b2 = s.nextInt();
int x = a1*a2;
int y = b1*b2;
if(x == y)
System.out.println("Equal");
}
}
Find bad smells!
Refactor the Code!
public class Main {
public void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
int width1 = scanner.nextInt();
System.out.print("Enter the length: ");
int length1 = scanner.nextInt();
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
int width2 = scanner.nextInt();
System.out.print("Enter the length: ");
int length2 = scanner.nextInt();
int area1 = width1*length1;
int area2 = width2*length2;
if(area1 == area2)
System.out.println("Equal");
}
}
Rename…
public class Rectangle{
private int length , width;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public Rectangle(int length, int width) {
this.length = length;
this.width = width;
}
}
Extract Class…
public class Main {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
int width = scanner.nextInt();
System.out.print("Enter the length: ");
int length = scanner.nextInt();
Rectangle rectangle1 = new Rectangle(length, width);
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
width = scanner.nextInt();
System.out.print("Enter the length: ");
length = scanner.nextInt();
Rectangle rectangle2 = new Rectangle(length, width);
int area1 = rectangle1.getWidth()*rectangle1.getLength();
int area2 = rectangle2.getWidth()*rectangle2.getLength();
if(area1 == area2)
System.out.println("Equal");
}
}
Extract Class…
public class Rectangle {
private static Rectangle readRectangle(Scanner scanner) {
int width;
int length;
System.out.println("Rectangle Info.");
System.out.print("Enter the width: ");
width = scanner.nextInt();
System.out.print("Enter the length: ");
length = scanner.nextInt();
Rectangle rectangle = new Rectangle(length, width);
return rectangle;
}
}
Extract Method…
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Rectangle rectangle1 = readRectangle(scanner);
Rectangle rectangle2 = readRectangle(scanner);
int area1 = rectangle1.area();
int area2 = rectangle2.area();
if(area1 == area2)
System.out.println("Equal");
}
}
Final Refactor ...