Anup Cowkur
public void checkAndPut(final String key,
final String value) {
if (!map.containsKey(key)) {
map.put(key, value);
}
)
public void checkAndPut(final String key,
final String value) {
synchronized(this) {
if (!map.containsKey(key)) {
map.put(key, value);
}
}
)
public class EntCounter {
private long count = 0;
public long increment() {
return ++count;
}
}
public class EntCounter {
private long count = 0;
public long increment() {
synchronized(this) {
return ++count;
}
}
}
public class RunFrodoRun {
private boolean stopped = false;
public void run() {
while(!stopped) {
// RUN!!!
}
}
public void stop() {
this.stopped = true;
}
}
public class RunFrodoRun {
private volatile boolean stopped = false;
public void run() {
while(!stopped) {
// RUN!!!
}
}
public void stop() {
this.stopped = true;
}
}
public class OrcFunerals {
int orcsKilledYesterday = 0;
int orcsKilledToday = 0;
public void init() {
orcsKilledYesterday = 1;
orcsKilledToday = 2;
}
public int getDeadOrcs() {
return orcsKilledYesterday + orcsKilledToday;
}
}
public class OrcFunerals {
int orcsKilledYesterday = 0;
int orcsKilledToday = 0;
public synchronized void init() {
orcsKilledYesterday = 1;
orcsKilledToday = 2;
}
public synchronized int getDeadOrcs() {
return orcsKilledYesterday + orcsKilledToday;
}
}
source: http://www.whattofix.com/images/ComplexERDExample.gif
int[] numbers = {5,10,15};
int sum() {
int total = 0;
for(int i=0; i<numbers.size ; i++) {
total = total + numbers[i];
}
return total;
}
sum(sum(sum(0,5), 10), 15)
int sum(x,y) {
return x + y;
}
public class Library {
public List<String> books;
public void addBooks(List<String> newBooks){
books.addAll(newBooks);
}
}
public class Library {
// 1. Make list of books private
private final List<String> books;
public Library(List<String> books) {
this.books = books;
}
public void addBooks(List<String> additionalBooks){
// 2. Copy old books
List<String> oldBooks = copy(books);
// 3. Add additional books to copy of old books
List<String> newBooks = oldBooks.addAll(additionalBooks);
// 4. Create and return new Library with new
// list of books
return new Library(newBooks);
}
}
https://github.com/konmik/solid
https://github.com/andrewoma/dexx
@anupcowkur