当前位置:网站首页>Builder pattern
Builder pattern
2022-06-10 05:36:00 【Qwe7】
Two 、 Builder pattern
The builder pattern focuses on the details of creating objects , The structure of complex objects or composite objects created with this pattern will be very clear
1、 Example of builder mode
For example, there is a recruitment agency , Now there are a batch of paper resumes or resume data information , In order to make a unified electronic resume , We need to sort this information out , How do we do it
// Before using builder mode
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var data = [
{
name: 'zhang san',
age: 24,
work: 'engineer'
},
{
name: 'li si',
age: 25,
work: 'teacher'
},
{
name: 'wang wu',
age: 26,
work: 'sb'
},
]
function Candidate(param) {
var _candidate = {};
_candidate.name = param.name
_candidate.age = param.age;
_candidate.firstName = _candidate.name.split(' ')[0];
_candidate.secondName = _candidate.name.split(' ')[1];
_candidate.work = {};
switch(param.work) {
case 'engineer':
_candidate.work.name = ' The engineer '
_candidate.work.description = ' Love programming '
break;
case 'teacher':
_candidate.work.name = ' teacher '
_candidate.work.description = ' Love to share '
break;
default:
_candidate.work.name = param.work
_candidate.work.description = ' nothing '
break
}
_candidate.work.changeWork = function(work) {
this.name = work
}
_candidate.work.changeDes = function(des) {
this.description = des;
}
return _candidate
}
var candidateArr = []
for (let i = 0; i < data.length; i++) {
candidateArr[i] = Candidate(data[i])
}
console.log(candidateArr[0])
candidateArr[1].work.changeWork('xxx')
console.log(candidateArr[1].work)
</script>
</head>
<body>
</body>
</html>// Builder pattern
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var data = [
{
name: 'zhang san',
age: 24,
work: 'engineer'
},
{
name: 'li si',
age: 25,
work: 'teacher'
},
{
name: 'wang wu',
age: 26,
work: 'sb'
},
]
function Candidate(param) {
var _candidate = new Person(param);
_candidate.name = new CreateName(param.name)
_candidate.work = new CreateWork(param.work)
return _candidate
}
function Person(param) {
this.name = param.name
this.age = param.age
}
function CreateName(name) {
this.wholeName = name
this.firstName = name.split(' ')[0]
this.secondName = name.split(' ')[1]
}
function CreateWork(work) {
switch(work) {
case 'engineer':
this.name = ' The engineer '
this.description = ' Love programming '
break;
case 'teacher':
this.name = ' teacher '
this.description = ' Love to share '
break;
default:
this.name = work
this.description = ' nothing '
break
}
}
CreateWork.prototype.changeWork = function(work) {
this.name = work
}
CreateWork.prototype.changeDes = function(des) {
this.description = des
}
var candidateArr = []
for (let i = 0; i < data.length; i++) {
candidateArr[i] = Candidate(data[i])
}
console.log(candidateArr[0])
candidateArr[1].work.changeWork('xxx')
console.log(candidateArr[1].work)
</script>
</head>
<body>
</body>
</html>边栏推荐
- 蚂蚁集团加入低碳专利承诺,向全球无偿开放专利,促进节能减排
- Power mathematics of leetcode326-3
- Blocking problem after the mobile terminal pulls up the keyboard
- MYSQL第二篇(核心技术)
- Use nodejs to export the pictures in the md/markdown document to the local and replace the original picture link with the local picture link
- Ant group's all-in-one privacy computing machine has obtained double certification, and 83 indicators meet the requirements
- Talk about the importance of technology and tasks
- Methods of using variables in shell parameters
- Installation and configuration of NPM and yarn
- Recursive function Hanoi Tower
猜你喜欢
![[sans titre]](/img/a2/230bd1f4faa5a945bb495189b7ca3a.png)
[sans titre]

Talk about the importance of technology and tasks

Model Lightweight - cutting distillation Series yolov5 nonestructive Cutting (attached source)

With the advent of the digital wave, how to achieve agile business delivery and sustainable technology governance? Uncover the ant bizstack

MTK based on gat tool and spoffinedebugsuite tool dump capture and parsing

MTK基于GAT工具和SpOffineDebugSuite工具 dump 抓取和解析

蚂蚁集团加入低碳专利承诺,向全球无偿开放专利,促进节能减排

Chapter VI software testing tools (end of this chapter)

Initial experience of using XXL job

【软件工程导论】知识点汇总 | 适用于考试复习 | 轻松通过考试
随机推荐
Solution to control double click conflict in WPF
An analysis of DuPont analysis: the financial situation of East China Construction Group Co., Ltd
Odometer. JS digital scrolling plug-in
《模型輕量化-剪枝蒸餾量化系列》YOLOv5無損剪枝(附源碼)
Recursive function Hanoi Tower
Warning: Accessing non-existent property ‘column‘ of module exports inside circular depen
Using gat to parse minidump (graphical interface)
Be diligent to tell me what I have done these days
Daily question - leetcode497 - random points in non overlapping rectangle - prefix and - bisection
photoClip. JS mobile image upload and interception plug-in
Web89~web96---php feature of ctfshow (1)
Overview and example analysis of opengauss database performance tuning
R language uses t.test function to perform Welch double sample t-test analysis and double independent sample t-test on the mean value of target variables corresponding to different groups
Study notes for typescript
Flutter DIO example
midway的使用教程
Installation and configuration of NPM and yarn
Power mathematics of leetcode326-3
聊一聊蚂蚁AI技术里的“老实担当”
Simple socket implementation of chat room system