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
package com.t4b.test.java.dp.cp.bp;
class Menu {
}
Source code of ToolBar.java
package com.t4b.test.java.dp.cp.bp;
class ToolBar {
}
Source code of MainWindow.java
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;
}
}
Source code of WindowBuilder.java
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;
}
}
Source code of TestMain.java
package com.t4b.test.java.dp.cp.bp;
public class TestMain {
public static void main(String[] args) {
WindowBuilder.createWindow();
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.