当前位置: 首页 > news >正文

建立网站加强家园沟通和联系的做法有何利弊百度收录入口在哪里查询

建立网站加强家园沟通和联系的做法有何利弊,百度收录入口在哪里查询,快递物流网站建设开发具备哪些功能,wordpress自动推送工具代码目录 一、行为型模式 一句话概括行为型模式 1.1、迭代器模式 1.1.1、概述 1.1.2、案例实现 1.1.3、优缺点 1.1.4、使用场景 一、行为型模式 一句话概括行为型模式 行为型模式:类或对象间如何交互、如何划分职责,从而更好的完成任务. 1.1、迭代器…

目录

一、行为型模式

一句话概括行为型模式

1.1、迭代器模式

1.1.1、概述 

1.1.2、案例实现

1.1.3、优缺点

1.1.4、使用场景


一、行为型模式


一句话概括行为型模式

行为型模式:类或对象间如何交互、如何划分职责,从而更好的完成任务.

1.1、迭代器模式

1.1.1、概述 

提供一个聚合对象,内部通过迭代器来访问聚合对象中的一系列数据,而不暴露聚合对象的内部实现.

例如,现在有一个班级的学生(包装在一个 List 容器中的聚合元素),我需要按照学号拿到每一个学生,此时就需要把遍历这个班级的学生(List 容器)交给迭代器完成.

迭代器模式主要包含以下角色:

  • 抽象迭代器:定义访问和遍历聚合元素的接口,通常包含 hasNext()、next() 等方法.
  • 具体迭代器:实现抽象迭代器接口中定义的方法,完成聚合对象的遍历,记录遍历的当前位置.
  • 抽象聚合:定义存储、添加、删除聚合元素以及创建迭代器对象接口.
  • 具体聚合:实现抽象聚合类,返回一个具体的迭代器实例.

1.1.2、案例实现

实现上述学生案例.

/*** 学生类*/
public class Student {private String name;private int id;public Student() {}public Student(String name, int id) {this.name = name;this.id = id;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", id=" + id +'}';}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}
}
/*** 抽象迭代器: 学生迭代器接口*/
public interface StudentIterator {boolean hasNext();Student next();}
/*** 具体迭代器: 学生迭代器*/
public class StudentIteratorImpl implements StudentIterator{private List<Student> list;private int position;public StudentIteratorImpl(List<Student> list) {this.list = list;}@Overridepublic boolean hasNext() {return position < list.size();}@Overridepublic Student next() {Student current = list.get(position);position++;return current;}}
/*** 抽象聚合: 学生聚合接口*/
public interface StudentAggregation {void addStudent(Student student);void removeStudent(Student student);StudentIterator getStudentIterator();}
/*** 具体聚合: 学生聚合*/
public class StudentAggregationImpl implements StudentAggregation{private List<Student> list = new ArrayList<>();@Overridepublic void addStudent(Student student) {list.add(student);}@Overridepublic void removeStudent(Student student) {list.remove(student);}@Overridepublic StudentIterator getStudentIterator() {return new StudentIteratorImpl(list);}}
public class Client {public static void main(String[] args) {StudentAggregationImpl aggregation = new StudentAggregationImpl();aggregation.addStudent(new Student("曹操", 1));aggregation.addStudent(new Student("诸葛亮", 2));aggregation.addStudent(new Student("赵云", 3));StudentIterator studentIterator = aggregation.getStudentIterator();while(studentIterator.hasNext()) {Student student = studentIterator.next();System.out.println(student);}}}

执行结果如下:

1.1.3、优缺点

优点:

定义多种遍历方式:支持不同方式遍历一个聚合对象,可以在同一个聚合对象上顶一个多种遍历方式.

满足开闭原则:引入抽象层,增加新的聚合类和迭代器,都无需修改原有代码.

缺点:

增加了类的个数,一定程度上增加了系统复杂度.

1.1.4、使用场景

  1. 当需要为聚合对象提供多种遍历方式.
  2. 当需要为遍历不同的聚合结构提供一个统一的接口时.
  3. 当访问的聚合对象的内容无需要暴露其内部实现细节.

http://www.hkea.cn/news/41586/

相关文章:

  • 绵阳网站建设 经开区网络教学平台
  • wordpress阅读量没改7个湖北seo网站推广策略
  • 网站建设成功案例方案找培训机构的平台
  • 园林绿化网站建设百度关键词优化公司
  • 个人如何建设网站网络营销方式有哪些分类
  • 北京做百度网站建设电商平台如何推广运营
  • 电脑个人网站怎么做网络销售新手入门
  • 海口网站建设 小黄网络手机百度搜索
  • 太原百度网站建设网站应该如何进行优化
  • 烟台市做网站uc浏览网页版进入
  • 工程信息网站哪家做的较好提高工作效率心得体会
  • 建站平台入口徐州网站设计
  • 出口手工艺品网站建设方案站长统计app下载
  • 提升学历骗局武汉搜索引擎排名优化
  • wordpress+park主题上海全国关键词排名优化
  • 潍坊最早做网站的公司短链接生成网址
  • 东莞化工网站建设爱站网ip反域名查询
  • 做网站赚钱 2017哈尔滨关键词排名工具
  • 建设的网站首页微信怎么做推广
  • 建设网站导航百度信息流推广和搜索推广
  • 深圳室内设计公司招聘信息流广告优化
  • 旅游网站首页四种营销模式
  • 负责网站建设如何在百度发广告推广
  • 联通的网站是谁做的营销的主要目的有哪些
  • 衡阳微信网站地推的方法和技巧
  • 南阳做网站公司哪家好自动发外链工具
  • 潍坊网站制作最低价格网络营销案例有哪些
  • 做网站有谁做谷歌seo视频教程
  • 资深的网站推广完美日记网络营销策划书
  • 90设计网站免费素材网站seo培训