Guest User

Untitled

a guest
Feb 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. /**
  2. * The Class BuilderDesignOfInterheritanceDemo.
  3. */
  4. public class BuilderDesignOfInterheritanceDemo {
  5.  
  6. public static void main(String[] args) {
  7. PersonA p = new EmployementBuilder()
  8. .withName("Noeik")
  9. .position("Founder")
  10. .build();
  11.  
  12. System.out.println(p);
  13. }
  14.  
  15.  
  16. }
  17. class PersonA{
  18. private String name;
  19.  
  20. // Employement Builder will fill the value
  21. private String position;
  22.  
  23.  
  24. public PersonA() {
  25. // TODO Auto-generated constructor stub
  26. }
  27.  
  28.  
  29. public String getName() {
  30. return name;
  31. }
  32.  
  33.  
  34. public void setName(String name) {
  35. this.name = name;
  36. }
  37.  
  38.  
  39. public String getPosition() {
  40. return position;
  41. }
  42.  
  43.  
  44. public void setPosition(String position) {
  45. this.position = position;
  46. }
  47.  
  48.  
  49. @Override
  50. public String toString() {
  51. return "PersonA [name=" + name + ", position=" + position + "]";
  52. }
  53.  
  54. }
  55.  
  56. class PersonBuilderA<T extends PersonBuilderA<T>>
  57. {
  58. PersonA p = new PersonA ();
  59.  
  60.  
  61. public T withName(String name) {
  62. p.setName(name);
  63. return self();
  64. }
  65. public PersonA build() {
  66. return p;
  67. }
  68. public T self() {
  69. return (T)this;
  70. }
  71. }
  72.  
  73. class EmployementBuilder extends PersonBuilderA<EmployementBuilder>
  74. {
  75. public EmployementBuilder position(String position) {
  76. p.setPosition(position);
  77. return self();
  78. }
  79.  
  80. @Override
  81. public EmployementBuilder self() {
  82. return this;
  83. }
  84.  
  85. }
Add Comment
Please, Sign In to add comment