Created
April 7, 2016 14:45
-
-
Save rhexgomez/014cbf7aaf5d96b0b6f5878e667e8007 to your computer and use it in GitHub Desktop.
Builder Design Pattern using Java Generics.
This file contains 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
/* | |
* Copyright 2016 Elmar Rhex Gomez. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example; | |
public class Foo extends Request { | |
public void customPrint(){ | |
System.out.println("Foo"); | |
} | |
public static class Builder extends Request.Builder<Foo, Builder> { | |
public Builder() { | |
super(Foo.class); | |
} | |
@Override | |
public Foo build() { | |
Foo c = getMainCLass(); | |
return c; | |
} | |
} | |
} |
This file contains 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
/* | |
* Copyright 2016 Elmar Rhex Gomez. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example; | |
public class MainClass { | |
public static void main(String[] args) { | |
Foo f = new Foo.Builder() | |
.name("elmar") | |
.last("gomez") | |
.build(); | |
f.print(); | |
f.customPrint(); | |
} | |
} |
This file contains 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
/* | |
* Copyright 2016 Elmar Rhex Gomez. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package com.example; | |
public class Request { | |
String name; | |
String lname; | |
public void name(String name) { | |
this.name = name; | |
} | |
public void last(String name) { | |
this.lname = name; | |
} | |
public void print() { | |
System.out.println(name); | |
System.out.println(lname); | |
} | |
public abstract static class Builder<T extends Request, T1> { | |
T gen; | |
public Builder(Class<T> clazz) { | |
try { | |
gen = clazz.newInstance(); | |
} catch (InstantiationException e) { | |
e.printStackTrace(); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
protected T getMainCLass() { | |
return gen; | |
} | |
public T1 name(String name) { | |
this.gen.name(name); | |
return (T1) this; | |
} | |
public T1 last(String name) { | |
this.gen.last(name); | |
return (T1) this; | |
} | |
public abstract T build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment