Creational Design Pattern: Builder Pattern - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

demo-image

Creational Design Pattern: Builder Pattern

Share This


The Builder Pattern is a design pattern that involves constructing a complicated thing by employing a step-by-step method with simple objects. It is mostly employed in situations where an object cannot be generated in a single step, such as during the de-serialization process of a complex object.
 
The primary benefits of the Builder Pattern are as follows: 
  • It offers distinct segregation between the production and portrayal of an object. 
  • It offers enhanced command over the construction process. 
  • It enables the modification of the internal representation of objects.
Source code of Menu.java
1
2
3
4
5
6
7
package com.t4b.test.java.dp.cp.bp;
class Menu {
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Source code of ToolBar.java
1
2
3
4
5
6
7
package com.t4b.test.java.dp.cp.bp;
class ToolBar {
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Source code of MainWindow.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.t4b.test.java.dp.cp.bp;
class MainWindow {
Menu menu;
ToolBar toolBar;
public Menu getMenu() {
return menu;
}
public void setMenu(Menu menu) {
this.menu = menu;
}
public ToolBar getToolBar() {
return toolBar;
}
public void setToolBar(ToolBar toolBar) {
this.toolBar = toolBar;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Source code of WindowBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.t4b.test.java.dp.cp.bp;
class WindowBuilder {
public static MainWindow createWindow() {
MainWindow window = new MainWindow();
window.setMenu(new Menu());
window.setToolBar(new ToolBar());
return window;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Source code of TestMain.java
1
2
3
4
5
6
7
8
9
10
package com.t4b.test.java.dp.cp.bp;
public class TestMain {
public static void main(String[] args) {
WindowBuilder.createWindow();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Happy Exploring!

Comment Using!!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.