Skip to content

Instantly share code, notes, and snippets.

class CheckBoxSystem {
CheckBoxSystem () {
CheckBox checkBox = new CheckBox(false);
CheckBoxController controller = new CheckBoxController(checkBox.getMutator());
CheckBoxViewer viewer = new CheckBoxViewer(checkBox.getReadOnlyView());
// ...
}
}
// View interface for Controller
interface MutableCheckbox {
void setState (boolean isChecked);
}
// View interface for components that only need to know the state
interface ReadOnlyCheckbox {
boolean getState ();
}
interface CheckBoxInterface {
void setState (boolean isChecked);
boolean getState ();
}
@mearns
mearns / myapp_helper.rb
Last active July 24, 2019 10:14
Example of a helper library in chef to act as a single source of truth, instead of misusing attributes for that.
# A helper class for encapsulating information about the installation and configuration of the app.
class MyApp
def initialize(node)
@node = node
end
# The name of the application
def app_name
'MyApp'
end
@mearns
mearns / recipe.rb
Created August 1, 2018 16:45
Example use of a helper library "myapp" in a chef recipe.
template myapp.config_file_path do # Access helper library methods inside a recipe.
source 'config.yml.erb'
owner 'root'
group 'root'
mode '644'
variables(
log_level: myapp.logging_level # Access helper library methods inside a resource
log_file_path: myapp.log_file_path
)
end
@mearns
mearns / README.md
Created April 16, 2018 15:25
A trivial example of a circular dependency in python

If you run python a.py from this directory, you get an error such as:

Traceback (most recent call last):
  File "a.py", line 1, in <module>
    import b
  File "./b.py", line 1, in <module>
    import a
  File "./a.py", line 3, in <module>
 b.bar()
@mearns
mearns / Assuming.java
Created February 2, 2018 18:54
Java Assumptions Pattern
import com.google.common.base.Suppliers;
import com.google.common.collect.Lists;
import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
@mearns
mearns / follow-up.java
Last active January 22, 2018 03:24
Follow Up Pattern
map.put(key, value)
.ifHadKey(oldValue -> System.out.println("Replaced " + oldValue + " at key " + key))
.ifDidNotHaveKey(() -> System.out.println("New key added: " + key));
@mearns
mearns / map.java
Last active December 6, 2017 17:39
normalization in design - non-normalized mapping
Map<String, Thing> createLookupMap (Collection<Thing> thingsToMap) {
Map<String, Collection<Thing>> lookup = new HashMap<>();
for (Thing thing : thingsToMap) {
String propValue = thing.getSomeProperty();
lookup.put(propValue, thing);
}
return lookup;
}
@mearns
mearns / chain-over-nest-local.js
Last active October 16, 2017 17:58
Toying around with some ideas of chaining instead of nesting, provoked by the presentation I'm currently writing about Promises.
const foo = (() => {
return bar() + baz() // = 80, I think.
})
.local('bar', () => 3*baz())
.local('baz', () => 2*trot)
.local('trot', 10)