Mazen
From high-level design principles to a sample of concrete design patterns
Every class should have a
single responsibility
Measure how system components depend on each other
We make mistakes
We identify our mistakes
We learn from our mistakes
We prevent mistakes
We Evolve Design Patterns
Lessons Learned!
I have a tree structure and I don't know all the types of objects in the tree
Run operations recursively through my tree without knowing the types in my tree
I want objects to know about a state but I don't know what these objects are
Objects can be added and removed as they desire
I want to coordinate a state across my system
There is always exactly one instance of this state in my system
I have a collection of elements and there should always be a "next" element
Traverse elements without exposing its implementation
ArrayListIterator<E>
LinkedListIterator<E>
HashSetIterator<E>
Iterable<E>
Iterator<E> iterator()
<<interface>>
Iterator<E>
boolean hasNext()
E next()
void remove()
<<interface>>
Collection
<<interface>>
Iterator<Item> itr = collection.iterator();
while (itr.hasNext()) {
Item nextItem = itr.next();
// do something with nextItem
}
Before Java 5
Since Java 5 (2004)
for (Item next : collection) {
// do something with next
}
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.out.println("Do something");
}
});
Before Java 8
button.addActionListener(e -> {
System.out.println("Do something");
});
Since Java 8 (2014)
List<String> list = new ArrayList<>();
list.add("dog");
list.add("cat");
list.add("hamster");
Before Java 9
List<String> list = List.of(
"dog", "cat", "hamster");
Since Java 9 (2017)
Map<String, Integer> map = new HashMap<>();
map.put("dog", 1);
map.put("cat", 5);
map.put("hamster", 7);
Before Java 9
Map<String, Integer> map = Map.of(
"dog", 1, "Cat", 5, "Hamster", 7);
Since Java 9 (2017)
public class User {
private String firstName;
private String lastName;
private int age;
private String email;
public User(String firstName, String lastName,
int age, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.email = email;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setAge(int age) {
this.age = age;
}
public void setEmail(String email) {
this.email = email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public String getEmail() {
return email;
}
}
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class User {
private String firstName;
private String lastName;
private int age;
private String email;
}
System.out.print("Hello");
System.out.println("Hello");
Java
print("Hello!")
println("Hello!")
Kotlin
final int a;
final int b = 21;
int c;
int d = 25;
d = 23;
c = 21;
Java
val a: Int
val b = 21
var c: Int
var d = 25
d = 23
c = 21
Kotlin
if(str instanceof String){
String result = ((String) str).substring(1);
}
Java
if (str is String) {
val result = str.substring(1)
}
Kotlin