所谓模板
来源:互联网 发布:html css js项目实战 编辑:程序博客网 时间:2024/05/20 21:23
1.模板听起来很学术,实际上就是一个事物里面包含了其他的事物。
对于模板的使用场景也很普遍:比如一个人他喜欢读书,各种书都喜欢,这个时候书籍是一个对象,
那么如果一个人喜欢的书籍有几百本,甚至上千本,这个时候如果直接让他把所有书籍列出一个单子来会很长,这时候我们可以对这些书籍进行分类:
比如:
文学相关:
《母亲》 《童年》 《在人间》 《麦田里的守望者》 《飘》(上中下) 《堂·吉诃德》...
历史相关:
《二十四史》
《史记》
传记相关:
《毛泽东转》
《邓小平转》
《华盛顿转》
对于程序来讲,所有的书籍都是一个书籍类对象,但是对于展现的时候,我们可以首先选择用户关注的类目,然后根据他关注的类目再去找相关类目下的书籍。
于是我们可以把类目抽象为一个对象,这个对象就是一个模板,你可以给他取名字:文学、历史、传记。
而对于每一个类目下的书籍我们可以根据用户的需求去建立关联关系:如上所述。
因此:对于模板关系而言,简单的来说我们要给 类目->书籍,建立一个模板关系,那么我只需要三张表:
1.模板表
2.书籍表
3.模板书籍关联表
类关系如下:Book 对象
package com.template; import java.math.BigDecimal;/** * ClassName:书籍 <br/> * Date: 2017年6月8日 下午3:38:03 <br/> * @author ztd * @version * @see */public class Book { public Book(int id, String name, String author, BigDecimal price, String publishingHouse, String publishDate) { super(); this.id = id; this.name = name; this.author = author; this.price = price; this.publishingHouse = publishingHouse; this.publishDate = publishDate; } /** * id:主键 */ private int id; /** * name:名称 */ private String name; /** * author:作者 */ private String author; /** * price:价格 */ private BigDecimal price; /** * publishingHouse:出版社 */ private String publishingHouse; /** * publishDate:出版时间 */ private String publishDate; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public String getPublishingHouse() { return publishingHouse; } public void setPublishingHouse(String publishingHouse) { this.publishingHouse = publishingHouse; } public String getPublishDate() { return publishDate; } public void setPublishDate(String publishDate) { this.publishDate = publishDate; } @Override public String toString() { return new StringBuffer().append("id : " + id + " name :" + name + " author : " + author + " price : " + price + " publishDate : " + publishDate).toString(); }}
Template 对象:
package com.template; import java.util.List;/** * 模板 * @author ztd * @version * date: 2017年6月8日 下午3:40:42 <br/> */public class Template<T> { /** * id:主键 */ private int id; /** * name:模板名称 */ private String name; /** * type:内容类型:0代表书籍,1代表杂志... */ private int type; private List<T> content; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getType() { return type; } public void setType(int type) { this.type = type; } public List<T> getContent() { return content; } public void setContent(List<T> content) { this.content = content; } @Override public String toString() { StringBuffer sContent = new StringBuffer(); for (T c : content) { sContent.append(c).append("\n"); } StringBuffer s = new StringBuffer(); s.append("模板名称:\n"+ this.getName() + "\n") .append("模板类型:\n"+ this.getName() + "\n") .append("模板内容:\n").append(sContent); return s.toString(); }}
假如我们读者关注的不止是书籍了,他还关注杂志,这个时候,我们的模板里面依然可以存放杂志,因为它是泛型的,我们只需要在 type 里面增加一个对于杂志的处理即可,同时增加一个杂志的表和模板跟杂志关联的关系表就行了。
对象如下:
Magazine 对象:
package com.template; import java.math.BigDecimal;/** * ClassName: 杂志 * @author ztd * @version * date: 2017年6月8日 下午4:00:32 <br/> */public class Magazine { public Magazine(int id, String name, String author, BigDecimal price, String publishingHouse, String publishDate) { super(); this.id = id; this.name = name; this.author = author; this.price = price; this.publishingHouse = publishingHouse; this.publishDate = publishDate; } /** * id:主键 */ private int id; /** * name:名称 */ private String name; /** * author:作者 */ private String author; /** * price:价格 */ private BigDecimal price; /** * publishingHouse:出版社 */ private String publishingHouse; /** * publishDate:出版时间 */ private String publishDate; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public BigDecimal getPrice() { return price; } public void setPrice(BigDecimal price) { this.price = price; } public String getPublishingHouse() { return publishingHouse; } public void setPublishingHouse(String publishingHouse) { this.publishingHouse = publishingHouse; } public String getPublishDate() { return publishDate; } public void setPublishDate(String publishDate) { this.publishDate = publishDate; } @Override public String toString() { return new StringBuffer().append("id : " + id + " name :" + name + " author : " + author + " price : " + price + " publishDate : " + publishDate).toString(); }}
测试类:TestTemplate
/** * Project Name:test * File Name:TestBook.java * Package Name:com.template * Date:2017年6月8日下午3:42:06 * */ package com.template; import java.math.BigDecimal;import java.util.ArrayList;import java.util.List;/** * ClassName:TestBook <br/> * Date: 2017年6月8日 下午3:42:06 <br/> * @author ztd * @version * @see */public class TestTemplate { public static void main(String[] args) { Book book1 = new Book(1, "《毛泽东转》", "毛泽东", new BigDecimal(50.00), "工业出版社", "2010-01-01"); Book book2 = new Book(1, "《邓小平转》", "邓小平", new BigDecimal(50.00), "工业出版社", "2010-01-01"); Book book3 = new Book(1, "《华盛顿转》", "华盛顿", new BigDecimal(50.00), "工业出版社", "2010-01-01"); Template<Book> biography = new Template<Book>(); biography.setId(1); biography.setType(0); biography.setName("传记"); List<Book> books = new ArrayList<Book>(); books.add(book3); books.add(book2); books.add(book1); biography.setContent(books); Magazine magazine1 = new Magazine(1, "《读者》", "读者", new BigDecimal(50.00), "工业出版社", "2010-01-01"); Magazine magazine2 = new Magazine(1, "《青年文摘》", "青年", new BigDecimal(50.00), "工业出版社", "2010-01-01"); Magazine magazine3 = new Magazine(1, "《时代周刊》", "时代", new BigDecimal(50.00), "工业出版社", "2010-01-01"); System.out.println(biography); Template<Magazine> magazineTemplate = new Template<Magazine>(); magazineTemplate.setId(1); magazineTemplate.setType(1); magazineTemplate.setName("杂志"); List<Magazine> magazines = new ArrayList<Magazine>(); magazines.add(magazine1); magazines.add(magazine2); magazines.add(magazine3); magazineTemplate.setContent(magazines); System.out.println(magazineTemplate); }}
总结:说到底,模板是存放对象集合的地方,然后呢,为了能够存储他们的对应关系,我们需要在数据库中除了建立相应的对象表结构之外,再建立一张存放他们关联关系的表即可。
一句话:模板就是关联关系的一种简化的表达。
- 所谓模板
- 所谓老公 所谓老婆
- 所谓科学,所谓假设
- 所谓时光,所谓爱
- 所谓时光,所谓爱
- 所谓企业文化
- 所谓kjava
- 所谓亲戚
- 所谓顾问
- 所谓价值
- 所谓企业文化
- 无,所谓
- 所谓美女
- 所谓名言......
- 所谓男人
- 所谓spring
- 不知所谓
- 所谓伊人
- ios文字转为16进制
- 剑指offer:第42题反转单词顺序VS左旋转字符串
- 现代视频播放器原理
- zookeeper简介
- Java中的equals和hashCode方法详解
- 所谓模板
- Excel 导出
- Linux 文件系统注册register_filesystem与注销unregister_filesystem源码详解
- 安装完Linux后,如何启动新添加的网卡
- 恢复使用win32 disk imager 制作的启动盘
- crond和crontab详解
- hihoCoder #1014 Trie树
- Docker环境 dockerUI redis mysql zookeeper安装使用
- 通过Statement接口实现数据库的更新操作