Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
datax-cloud
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
黄营
datax-cloud
Commits
b9ee30ea
Commit
b9ee30ea
authored
Mar 05, 2020
by
yuwei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Java设计模式
parent
651ef128
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
84 additions
and
0 deletions
+84
-0
BuilderPattern.java
...x/learning/design/patterns/creational/BuilderPattern.java
+84
-0
No files found.
datax-learning/design-patterns/src/main/java/cn/datax/learning/design/patterns/creational/BuilderPattern.java
0 → 100644
View file @
b9ee30ea
package
cn
.
datax
.
learning
.
design
.
patterns
.
creational
;
/**
* 建造者模式(Builder Pattern)
*/
public
class
BuilderPattern
{
public
static
void
main
(
String
[]
args
)
{
MyDate
myDate
=
new
MyDate
();
IDateBuilder
builder
;
builder
=
new
DateBuilder1
(
myDate
);
String
date1
=
new
Derector
(
builder
).
getDate
(
2066
,
3
,
6
);
System
.
out
.
println
(
date1
);
builder
=
new
DateBuilder2
(
myDate
);
String
date2
=
new
Derector
(
builder
).
getDate
(
2067
,
4
,
6
);
System
.
out
.
println
(
date2
);
}
}
//首先定义一个产品类
class
MyDate
{
String
Date
;
}
//然后抽象生成器,描述生成器的行为
interface
IDateBuilder
{
IDateBuilder
buildDate
(
int
y
,
int
m
,
int
d
);
String
date
();
}
//接下来是具体生成器,一个以“-”分割年月日,另一个使用空格:
class
DateBuilder1
implements
IDateBuilder
{
private
MyDate
myDate
;
public
DateBuilder1
(
MyDate
myDate
)
{
this
.
myDate
=
myDate
;
}
@Override
public
IDateBuilder
buildDate
(
int
y
,
int
m
,
int
d
)
{
myDate
.
Date
=
y
+
"-"
+
m
+
"-"
+
d
;
return
this
;
}
@Override
public
String
date
()
{
return
myDate
.
Date
;
}
}
//具体生成器
class
DateBuilder2
implements
IDateBuilder
{
private
MyDate
myDate
;
public
DateBuilder2
(
MyDate
myDate
)
{
this
.
myDate
=
myDate
;
}
@Override
public
IDateBuilder
buildDate
(
int
y
,
int
m
,
int
d
)
{
myDate
.
Date
=
y
+
" "
+
m
+
" "
+
d
;
return
this
;
}
@Override
public
String
date
()
{
return
myDate
.
Date
;
}
}
//接下来是指挥官,向用户提供具体的生成器:
class
Derector
{
private
IDateBuilder
builder
;
public
Derector
(
IDateBuilder
builder
)
{
this
.
builder
=
builder
;
}
public
String
getDate
(
int
y
,
int
m
,
int
d
)
{
builder
.
buildDate
(
y
,
m
,
d
);
return
builder
.
date
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment