Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Snowleopard_wu
Java
提交
b532d707
J
Java
项目概览
Snowleopard_wu
/
Java
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
Java
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b532d707
编写于
5月 16, 2019
作者:
A
Abhijay Kumar
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added Builder Design pattern implementation in Java and its test
上级
616d5cfc
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
114 addition
and
0 deletion
+114
-0
src/main/java/com/designpatterns/builderpattern/Desktop.java
src/main/java/com/designpatterns/builderpattern/Desktop.java
+82
-0
src/test/java/com/designpatterns/builderpattern/DesktopBuilderTest.java
...com/designpatterns/builderpattern/DesktopBuilderTest.java
+32
-0
未找到文件。
src/main/java/com/designpatterns/builderpattern/Desktop.java
0 → 100644
浏览文件 @
b532d707
package
src.main.java.com.designpatterns.builderpattern
;
/**
* The Builder is a design pattern designed to provide a flexible solution to various object creation problems in
* object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex
* object from its representation.
*
* @see <a href="https://en.wikipedia.org/wiki/Builder_pattern">Builder Pattern</a>
*/
public
class
Desktop
{
private
String
CPU
;
private
String
RAM
;
private
boolean
isGraphicCardEnabled
;
private
String
operatingSystem
;
private
int
diskSizeGB
;
private
String
graphicCard
;
private
Desktop
(
DesktopBuilder
builder
)
{
this
.
CPU
=
builder
.
CPU
;
this
.
RAM
=
builder
.
RAM
;
this
.
isGraphicCardEnabled
=
builder
.
isGraphicCardEnabled
;
this
.
operatingSystem
=
builder
.
operatingSystem
;
this
.
diskSizeGB
=
builder
.
diskSizeGB
;
this
.
graphicCard
=
builder
.
graphicCard
;
}
/**
* Builder class for the above Desktop class. Constructs the Desktop by invoking the Desktop class constructor and
* allows access to set optional fields in the Desktop class.
*/
public
static
class
DesktopBuilder
{
private
String
CPU
;
private
String
RAM
;
private
boolean
isGraphicCardEnabled
;
private
String
operatingSystem
;
private
int
diskSizeGB
;
private
String
graphicCard
;
public
DesktopBuilder
(
String
CPU
,
String
RAM
)
{
this
.
CPU
=
CPU
;
this
.
RAM
=
RAM
;
}
public
DesktopBuilder
setGraphicCardEnabled
(
boolean
graphicCardEnabled
)
{
this
.
isGraphicCardEnabled
=
graphicCardEnabled
;
return
this
;
}
public
DesktopBuilder
setOperatingSystem
(
String
operatingSystem
)
{
this
.
operatingSystem
=
operatingSystem
;
return
this
;
}
public
DesktopBuilder
setDiskSizeGB
(
int
diskSize
)
{
this
.
diskSizeGB
=
diskSize
;
return
this
;
}
public
DesktopBuilder
setGraphicCard
(
String
graphicCard
)
{
this
.
graphicCard
=
graphicCard
;
return
this
;
}
public
Desktop
build
()
{
return
new
Desktop
(
this
);
}
}
@Override
public
String
toString
()
{
return
"Desktop{"
+
"CPU='"
+
CPU
+
'\''
+
", RAM='"
+
RAM
+
'\''
+
", isGraphicCardEnabled="
+
isGraphicCardEnabled
+
", operatingSystem='"
+
operatingSystem
+
'\''
+
", diskSizeGB="
+
diskSizeGB
+
", graphicCard='"
+
graphicCard
+
'\''
+
'}'
;
}
}
src/test/java/com/designpatterns/builderpattern/DesktopBuilderTest.java
0 → 100644
浏览文件 @
b532d707
package
src.test.java.com.designpatterns.builderpattern
;
import
org.junit.Assert
;
import
org.junit.Test
;
import
src.main.java.com.designpatterns.builderpattern.Desktop
;
public
class
DesktopBuilderTest
{
private
final
String
configOne
=
"Desktop{CPU='Intel i7', RAM='Corsair Vengeance 3000', isGraphicCardEnabled=true"
+
", operatingSystem='Windows 10', diskSizeGB=16, graphicCard='NVIDIA GTX 1080'}"
;
private
final
String
configTwo
=
"Desktop{CPU='Intel i5', RAM='HyperX Fury v5', isGraphicCardEnabled=true, "
+
"operatingSystem='Red Hat Enterprise', diskSizeGB=16, graphicCard='NVIDIA RTX 2080'}"
;
@Test
public
void
testDesktopBuilder
()
{
Desktop
d1
=
new
Desktop
.
DesktopBuilder
(
"Intel i7"
,
"Corsair Vengeance 3000"
)
.
setDiskSizeGB
(
16
)
.
setGraphicCard
(
"NVIDIA GTX 1080"
)
.
setGraphicCardEnabled
(
true
)
.
setOperatingSystem
(
"Windows 10"
)
.
build
();
Assert
.
assertEquals
(
d1
.
toString
(),
configOne
);
Desktop
d2
=
new
Desktop
.
DesktopBuilder
(
"Intel i5"
,
"HyperX Fury v5"
)
.
setDiskSizeGB
(
16
)
.
setGraphicCard
(
"NVIDIA RTX 2080"
)
.
setGraphicCardEnabled
(
true
)
.
setOperatingSystem
(
"Red Hat Enterprise"
)
.
build
();
Assert
.
assertEquals
(
d2
.
toString
(),
configTwo
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录