OneThenTwo
Alternating
Cartesian
OneThenTwo
Alternating
Cartesian
OneThenTwo
Alternating
Cartesian
Strategy
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;
}
public class Test {
public static void main(String args[]) {
String array[] = {"Hello, World", "Hi there, Everyone", "6"};
for (String i : array) {
System.out.println(i);
}
}
}
Java
stuff = ["Hello, World!", "Hi there, Everyone!", 6]
for i in stuff:
print(i)
Python
fn main() {
let stuff = vec!["Hello, World!", "Hi there, Everyone!", 6];
for i in stuff.iter() {
println!("{}", i);
}
}
Rust
public class NullPointerExample {
public static void main(String[] args) {
String someVariable = null;
someVariable.length();
}
}
Java
# Attempting to call a method on a None object
some_variable = None
some_variable()
Python
fn main() {
let some_variable: Option<&str> = None;
some_variable.unwrap();
}
Rust
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "someVariable" is null
at NullPointerExample.main(NullPointerExample.java:4)
TypeError: 'NoneType' object is not callable
thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', src/main.rs:3:5
... and more!
Â
Good luck on the Final Exam!