Last active
August 29, 2015 13:56
-
-
Save ryands/8854971 to your computer and use it in GitHub Desktop.
Lunch Tech Talk - 2/6/2014 - Builder pattern - Sample code. Gist broke my formatting/indentation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Builder { | |
public static void main(String... args) { | |
DogeBuilder builder = new DogeBuilder(); | |
builder.setSo("design") | |
.setSuch("pattern") | |
.setVery("software engineering"); | |
System.out.println(builder.build()); | |
} | |
public static class DogeBuilder { | |
private String so, such, very; | |
public DogeBuilder() {/* nothing? */} | |
public DogeBuilder setSo(String so) { | |
this.so = so; | |
return this; | |
} | |
public DogeBuilder setSuch(String such) { | |
this.such = such; | |
return this; | |
} | |
public DogeBuilder setVery(String very) { | |
this.very = very; | |
return this; | |
} | |
public String build() { | |
/* Yo dawg, I heard you like builders, so we put a builder in your | |
builder, so you can build while you build. | |
-- Xzibit | |
*/ | |
return new StringBuilder() | |
.append("so ").append(so).append('\n') | |
.append("such ").append(such).append('\n') | |
.append("very ").append(very).append('\n') | |
.toString(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class DogeBuilder | |
def initialize &block | |
@doge = {} | |
block.call(self) | |
self | |
end | |
def method_missing(meth, *args, &blk) | |
@doge[meth.to_sym] = args.first | |
self | |
end | |
def build | |
str = "" | |
@doge.each do |prefix, action| | |
str << "#{prefix} #{action}\n" | |
end | |
str << 'wow' | |
end | |
end | |
# Example showing both block, chained (in block), and chained builder | |
builder = DogeBuilder.new do |d| | |
d.so 'ruby' | |
d.very('design').such('pattern') | |
end | |
builder.much 'fun' | |
builder.plz('write good software').not('scare') | |
# Lets build it. | |
puts builder.build |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Pretend example of an "HTTP Request Builder" | |
* (Mike requested an example after hearing my description of such a builder) | |
* Usage might look something like: | |
* RequestBuilder builder = new RequestBuilder("http://grio.com/api", "parties") | |
* .setType(RequestBuilder.RequestType.GET) | |
* .addParam("location", "office") | |
* .addParam("date", "2014-02-06"); | |
* HttpRequest request = builder.build(); | |
*/ | |
public class RequestBuilder { | |
private Map<String, String> params; | |
private String host; | |
private String endpoint; | |
private RequestType type; | |
public enum RequestType { | |
GET, POST; | |
} | |
public RequestBuilder(String host, String endpoint) { | |
this.host = host; | |
this.endpoint = endpoint; | |
params = new HashMap<String, String>(); | |
} | |
public RequestBuilder setType(RequestType type) { | |
this.type = type; | |
return this; | |
} | |
public RequestBuilder setHost(String host) { | |
this.host = host; | |
return this; | |
} | |
public RequestBuilder setEndpoint(String ep) { | |
this.endpoint = ep; | |
return this; | |
} | |
public RequestBuilder addParam(String key, String value) { | |
params.put(key, value); | |
return this; | |
} | |
public HttpRequest build() { | |
HttpRequest request = null; | |
String queryString = new StringBuilder(); | |
for (String p: params.keySet()) { | |
queryString.append(p) | |
.append('=') | |
.append(urlEncode(params.get(p))) | |
.append('&'); | |
} | |
// strip trailing & | |
queryString = queryString.substring(0, queryString.length()-1); | |
if (this.type == RequestType.GET) { | |
request = new HttpGetRequest(host + "/" + endpoint + "?" + queryString); | |
} elsif (this.type == RequestType.POST) { | |
request = new HttpPostRequest(host + "/" + endpoint); | |
request.setBody(queryString) | |
} | |
return request; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @ryands 💖