当前位置:网站首页>Composite mode example
Composite mode example
2022-06-10 10:09:00 【Qwe7】
5、 ... and 、 Portfolio model
The combination mode is used to combine multiple parts into a whole . It's like we went to McDonald's and ordered a hamburger 、 Two French fries and a coke . We can think of these things as parts or components , Through combination, the whole package can be output to customers . This is the combination mode
1、 Example of combination mode
For example, we often make some forms in our work , For example, login. 、 register , Or fill in some information, etc , These forms are actually similar , If you make a registration form today , Make a questionnaire form tomorrow , Would you be in trouble , Some feeling of repetitive work . It will be much better to learn the combination mode
notes : Combination mode belongs to that kind of , The larger the amount of code , The greater the need for repetition ( It's the kind where only a small part of the code is inconsistent between two pages , But you can't use the requirement of component code ), The more efficient . If it's just a small capacity system , There is basically no need to use composite mode to create forms .
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
window.onload = function() {
// Combined parasitic inheritance
function inheritPrototype(subClass, superClass) {
function F() {};
F.prototype = superClass.prototype
subClass.prototype = new F()
subClass.prototype.constructor = subClass
}
// Container base class
function Container() {
this.children = []
this.element = null
}
Container.prototype = {
init: () => {
throw new Error(' Please rewrite init Method ')
},
add: function(child){
this.children.push(child);
this.element.appendChild(child.element)
return this // Convenient chain call
}
}
// Create a form container based on the container base class
function CreateForm(id, method, action, parent) {
Container.call(this)
this.id = id || ''
this.method = method || ''
this.action = action || ''
this.parent = parent || ''
this.init()
}
inheritPrototype(CreateForm, Container);
CreateForm.prototype.init = function() {
this.element = document.createElement('form')
this.element.id = this.id
this.element.method = this.method
this.element.action = this.action
}
CreateForm.prototype.show = function() {
this.parent.appendChild(this.element)
}
// Row container component
function CreateLine(className) {
Container.call(this)
this.className = className === undefined ? 'form-line' : 'form-line' + className
this.init()
}
inheritPrototype(CreateLine, Container);
CreateLine.prototype.init = function() {
this.element = document.createElement('div')
this.element.className = this.className
}
// label // The lowest leaf node does not need to inherit the methods of the container base class
function CreateLabel(text, forName) {
this.text = text || ''
this.forName = forName || ''
this.init()
}
CreateLabel.prototype.init = function() {
this.element = document.createElement('label')
this.element.setAttribute('for', this.forName)
this.element.innerHTML = this.text
}
// input
function CreateInput(type, id, name, defaultValue) {
this.type = type || ''
this.id = id || '';
this.name = name || '';
this.defaultValue = defaultValue || '';
this.init()
}
CreateInput.prototype.init = function() {
this.element = document.createElement('input')
this.element.type = this.type
this.element.id = this.id
this.element.name = this.name
this.element.value = this.defaultValue
}
var form = new CreateForm('owner-form', 'GET', '/aaa.html', document.body);
var userLine = new CreateLine()
.add( new CreateLabel(' user name ', 'user') )
.add( new CreateInput('text', 'user', 'user'))
var passwordLine = new CreateLine()
.add( new CreateLabel(' password ', 'pwd') )
.add( new CreateInput('password', 'pwd', 'pwd'))
var submitLine = new CreateLine()
.add( new CreateLabel('submit', '', '', ' Sign in ') )
form.add(userLine).add(passwordLine).add(submitLine).show()
}
// var aa = new Container()
</script>
</head>
<body>
<!-- Implement forms in a normal way -->
<!-- <form action="xxx" method="GET">
<div class = 'form-line'>
<label for="user"> user name </label>
<input type="text" id = "user" name ="user"/>
</div>
<div class = 'form-line'>
<label for="pwd"> user name </label>
<input type="password" id = "pwd" name ="pwd"/>
</div>
<div class = 'form-line'>
<input type="submit" value=" Sign in "/>
</div>
</form>
-->
</body>
</html>边栏推荐
- On the night of the joint commissioning, I beat up my colleagues
- What are the tools and software needed for SCM development
- OSG basic operation
- Neo 黑客松获奖名单揭晓,上万美金花落谁家?
- Lambda ellipsis rule
- 收藏 | VLOOKUP函数的这些妙用你都知道吗?
- To serve the "nervous system" with a broad and subtle vision
- Ce soir - là, j'ai battu mon collègue...
- 2022年金属非金属矿山提升机操作考试题库及答案
- Use nsenter to enter netns to capture packets
猜你喜欢
![[edge detection] eight direction Sobel image edge detection based on MATLAB [including Matlab source code 1865]](/img/c8/039e7fc983905ae74e2d945dc6fbea.jpg)
[edge detection] eight direction Sobel image edge detection based on MATLAB [including Matlab source code 1865]

5G 聯通網管設計思路

浏览器禁止缓存讲解

SQL SERVER Always on 监控脚本与一些听到的误解

Some problems in using message queue service in thinkphp6

Microsoft exposes another "scandal": watching VR porn in the office, "the father of hololens" is about to leave!

SQL server always on monitoring script and some misunderstandings

Browser cache forbidden explanation

工业互联网架构图

fastadmin使用PHPExcel导出表格数据到Excel中
随机推荐
Some problems in using message queue service in thinkphp6
Genius! Only use four integers to write a snake game!
Lambda表达式例五
On how T-level interactive development shines on our hands
June training (day 10) - bit operation
The R language coin package is applied to permutation tests for independence problems, one-way ANOVA and approximate k-sample permutation tests on the same data set, and comparing whether the mean val
微软再曝“丑闻”:在办公室看 VR 黄片,“HoloLens 之父”即将离职!
Mysql database (26): View
Google Earth Engine(GEE)——GPWv411:平均行政单位面积数据集
On the night of the joint commissioning, I beat up my colleagues
程序编译基本过程
成都自控开发_单片机程序的一般开发流程是怎样
Development of raspberry pie IO port driver
Matlab绘制曲线
张小白教你使用OGG实现Oracle 19C到MySQL 5.7的数据同步(2)
在thinkphp6中使用消息队列服务遇到的几个问题
【摸鱼神器】UI库秒变LowCode工具——列表篇(二)维护json的小工具
Learning notes on panoramic segmentation of point cloud
已经过去多长时间的方式显示时间
Why should the R & D effectiveness team of Internet companies be independent? When is independence?