文章目录
1.引出迭代器模式
2.迭代器模式解决院系结构展示问题 1.基本介绍 2.原理类图 3.类图 4.代码实现 1.Department.java 存储信息的对象 2.College.java 被迭代的类型接口 3.ComputerCollege.java 被迭代的具体实现类,存储数据并将其在创建迭代器的时候传进去 4.ComputerCollegeIterator.java 被实现的具体类的迭代器 5.InfoCollege.java 被迭代的具体类型 6.InfoColleageIterator.java 具体的迭代器 7.OutputImpl.java 统一输出信息的类 8.Client.java 客户端
5.注意事项和细节
3.迭代器模式在ArrayList的应用
观察者模式 1.引出观察者模式 1.天气预报项目需求 2.普通方案 3.普通方案问题分析
2.观察者模式解决天气预报 1.原理分析 2.类图 3.代码实现 1.Observer.java 观察者接口,可以更新自己的信息 2.CurrentConditions.java 具体的观察者 3.Subject.java 信息发布者的接口 4.WeatherData.java 具体的信息发布者,一旦更新信息就会更新观察者的信息 5.Client.java 6.结果
3.观察者模式在JDK的Observable类中的使用
1.引出迭代器模式
1.展示院系结构
2.传统方式
2.迭代器模式解决院系结构展示问题
1.基本介绍
2.原理类图
3.类图
4.代码实现
1.Department.java 存储信息的对象
package com. sun ;
public class Department {
private String name;
private String desc;
public Department ( String name, String desc) {
this . name = name;
this . desc = desc;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getDesc ( ) {
return desc;
}
public void setDesc ( String desc) {
this . desc = desc;
}
}
2.College.java 被迭代的类型接口
package com. sun ;
import java. util. Iterator ;
public interface College {
String getName ( ) ;
void addDepartment ( String name, String desc) ;
Iterator createIterator ( ) ;
}
3.ComputerCollege.java 被迭代的具体实现类,存储数据并将其在创建迭代器的时候传进去
package com. sun ;
import java. util. Iterator ;
public class ComputerCollege implements College {
Department [ ] departments;
int numOfDepartment = 0 ;
public ComputerCollege ( ) {
departments = new Department [ 3 ] ;
addDepartment ( "java" , "java" ) ;
addDepartment ( "python" , "python" ) ;
addDepartment ( "go" , "go" ) ;
}
@Override
public String getName ( ) {
return "计算机学院" ;
}
@Override
public void addDepartment ( String name, String desc) {
Department department = new Department ( name, desc) ;
departments[ numOfDepartment++ ] = department;
}
@Override
public Iterator createIterator ( ) {
return new ComputerCollegeIterator ( departments) ;
}
}
4.ComputerCollegeIterator.java 被实现的具体类的迭代器
package com. sun ;
import java. util. Iterator ;
public class ComputerCollegeIterator implements Iterator {
Department [ ] departments;
int position = 0 ;
public ComputerCollegeIterator ( Department [ ] departments) {
this . departments = departments;
}
@Override
public boolean hasNext ( ) {
if ( position >= departments. length || departments[ position] == null ) {
return false ;
}
return true ;
}
@Override
public Object next ( ) {
Department department = departments[ position] ;
position += 1 ;
return department;
}
@Override
public void remove ( ) {
Iterator . super . remove ( ) ;
}
}
5.InfoCollege.java 被迭代的具体类型
package com. sun ;
import java. util. Iterator ;
import java. util. LinkedList ;
import java. util. List ;
public class InfoCollege implements College {
List < Department > departmentList;
public InfoCollege ( ) {
departmentList = new LinkedList < > ( ) ;
addDepartment ( "信息安全" , "信息安全" ) ;
addDepartment ( "网络安全" , "网络安全" ) ;
addDepartment ( "服务器安全" , "服务器安全" ) ;
}
@Override
public String getName ( ) {
return "信息工程学院" ;
}
@Override
public void addDepartment ( String name, String desc) {
Department department = new Department ( name, desc) ;
departmentList. add ( department) ;
}
@Override
public Iterator createIterator ( ) {
return new InfoColleageIterator ( departmentList) ;
}
}
6.InfoColleageIterator.java 具体的迭代器
package com. sun ;
import java. util. Iterator ;
import java. util. List ;
public class InfoColleageIterator implements Iterator {
List < Department > departments;
int index = - 1 ;
public InfoColleageIterator ( List < Department > departments) {
this . departments = departments;
}
@Override
public boolean hasNext ( ) {
if ( index >= departments. size ( ) - 1 ) {
return false ;
}
index += 1 ;
return true ;
}
@Override
public Object next ( ) {
return departments. get ( index) ;
}
@Override
public void remove ( ) {
Iterator . super . remove ( ) ;
}
}
7.OutputImpl.java 统一输出信息的类
package com. sun ;
import java. util. Iterator ;
import java. util. List ;
public class OutputImpl {
List < College > collegeList;
public OutputImpl ( List < College > collegeList) {
this . collegeList = collegeList;
}
public void printCollege ( ) {
for ( College college : collegeList) {
System . out. println ( "=====" + college. getName ( ) + "=====" ) ;
Iterator iterator = college. createIterator ( ) ;
printDepartment ( iterator) ;
}
}
public void printDepartment ( Iterator iterator) {
while ( iterator. hasNext ( ) ) {
Department next = ( Department ) iterator. next ( ) ;
System . out. println ( next. getName ( ) ) ;
}
}
}
8.Client.java 客户端
package com. sun ;
import java. util. LinkedList ;
import java. util. List ;
public class Client {
public static void main ( String [ ] args) {
List < College > collegeList = new LinkedList < > ( ) ;
collegeList. add ( new ComputerCollege ( ) ) ;
collegeList. add ( new InfoCollege ( ) ) ;
OutputImpl output = new OutputImpl ( collegeList) ;
output. printCollege ( ) ;
}
}
5.注意事项和细节
3.迭代器模式在ArrayList的应用
观察者模式
1.引出观察者模式
1.天气预报项目需求
2.普通方案
3.普通方案问题分析
2.观察者模式解决天气预报
1.原理分析
2.类图
3.代码实现
1.Observer.java 观察者接口,可以更新自己的信息
package com. sun ;
public interface Observer {
void update ( float temperature, float pressure, float humidity) ;
}
2.CurrentConditions.java 具体的观察者
package com. sun ;
public class CurrentConditions implements Observer {
private float temperature;
private float pressure;
private float humidity;
@Override
public void update ( float temperature, float pressure, float humidity) {
this . temperature = temperature;
this . humidity = humidity;
this . pressure = pressure;
display ( ) ;
}
public void display ( ) {
System . out. println ( "Today's temperature: " + temperature) ;
System . out. println ( "Today's pressure: " + pressure) ;
System . out. println ( "Today's humidity: " + humidity) ;
}
}
3.Subject.java 信息发布者的接口
package com. sun ;
public interface Subject {
void registerObserver ( Observer o) ;
void removeObserver ( Observer o) ;
void notifyObservers ( ) ;
}
4.WeatherData.java 具体的信息发布者,一旦更新信息就会更新观察者的信息
package com. sun ;
import java. util. ArrayList ;
import java. util. Iterator ;
import java. util. List ;
public class WeatherData implements Subject {
private float temperature;
private float pressure;
private float humidity;
private List < Observer > observerList;
public WeatherData ( ) {
observerList = new ArrayList < > ( ) ;
}
public void setData ( float temperature, float pressure, float humidity) {
this . humidity = humidity;
this . pressure = pressure;
this . temperature = temperature;
changeData ( ) ;
}
public void changeData ( ) {
notifyObservers ( ) ;
}
@Override
public void registerObserver ( Observer o) {
observerList. add ( o) ;
}
@Override
public void removeObserver ( Observer o) {
observerList. remove ( o) ;
}
@Override
public void notifyObservers ( ) {
Iterator < Observer > iterator = observerList. iterator ( ) ;
while ( iterator. hasNext ( ) ) {
Observer next = iterator. next ( ) ;
next. update ( temperature, pressure, humidity) ;
}
}
}
5.Client.java
package com. sun ;
public class Client {
public static void main ( String [ ] args) {
WeatherData weatherData = new WeatherData ( ) ;
CurrentConditions currentConditions = new CurrentConditions ( ) ;
weatherData. registerObserver ( currentConditions) ;
weatherData. setData ( 200 , 200 , 200 ) ;
weatherData. setData ( 100 , 100 , 100 ) ;
}
}
6.结果
3.观察者模式在JDK的Observable类中的使用