KWIC 软件系统行为型设计模式应用 -- 观察者模式

Alphabetizer.java:

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;


public class Alphabetizer implements KWICListener{
	private TextLines textlines=null;
	@Override
	public void handleEvent(KWICEvent event) {
		// TODO Auto-generated method stub
		if(event instanceof InsertToTextLinesEvent){
			textlines=((CircularShifter) event.getSource()).getTextLines();
			Collections.sort(textlines.getLineList());
	
			try {
				Outputer.println(textlines);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
}

CircularShifter.java:

import java.util.ArrayList;


public class CircularShifter implements KWICListener{
	
	private TextLines textlines=null;
	
	@Override
	public void handleEvent(KWICEvent event) {
		// TODO Auto-generated method stub
		if(event instanceof InsertToTextLineEvent){
			TextLine textline=((Inputer) event.getSource()).getTextLine();
			
			int number_of_lines=textline.numberOfLines();
			ArrayList<ArrayList> exlist=new ArrayList<ArrayList>(0);
		    ArrayList<String> inlist=new ArrayList<String>(0);
		    for(int i=0;i<number_of_lines;i++){
		        for(int j=0;j<textline.numberOfWords(i);j++){
		        	if(j==0)  {
		        		inlist.add(textline.getLine(i));		        		
		        	}
		        	else {
		        		inlist.add(textline.shiftwords(i));
		        	}
		        }
		        exlist.add(inlist);
		        inlist=new ArrayList<String>(0);
		    }
		    textlines=new TextLines();
		    for(int i=0;i<number_of_lines;i++){
		        for(int j=0;j<exlist.get(i).size();j++){
		        	textlines.addLine((String)exlist.get(i).get(j));
		        	
		        }
		    }
		    InsertToTextLinesEvent ittles=new InsertToTextLinesEvent(this);
		    EventManager.broadcast(ittles);
		}
	}

	public TextLines getTextLines(){
		return textlines;
	}
}

EventManager.java:

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.EventListener;

public class EventManager {
	// 监听器列表
	private static List<KWICListener> listenerList = new ArrayList<KWICListener>();
	
	// 监听器注册方法
	public static void addListener(KWICListener listener) {
		listenerList.add(listener);
	}
	// 监听器注销方法
	public static void removeListener(KWICListener listener) {
		listenerList.remove(listener);
	}
	// 事件广播方法
	public static void broadcast(KWICEvent event) {
		for(KWICListener listener : listenerList)
			listener.handleEvent(event);
	}
}

Inputer.java:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


public class Inputer {
	private TextLine textline=null;

	
	public Inputer(FileReader fr) throws IOException {
		// TODO Auto-generated constructor stub
		input(fr);
	}

	public void input(FileReader fr) throws IOException{
		BufferedReader br=new BufferedReader(fr);
		textline=new TextLine();
		while(br.ready()){
			textline.addLine(br.readLine());
			InsertToTextLineEvent ittle=new InsertToTextLineEvent(this);
			EventManager.broadcast(ittle);
		}
	}
	
	public TextLine getTextLine(){
		return textline;
	}
}

InsertToTextLineEvent.java:


public class InsertToTextLineEvent extends KWICEvent{

	public InsertToTextLineEvent(Object source) {
		super(source);
		// TODO Auto-generated constructor stub
		
	}

}

InsertToTextLinesEvent.java:


public class InsertToTextLinesEvent extends KWICEvent{

	public InsertToTextLinesEvent(Object source) {
		super(source);
		// TODO Auto-generated constructor stub
	}

}

KWIC.java

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class KWIC {
	public static void main(String args[]) throws IOException{
		EventManager.addListener(new CircularShifter());
		EventManager.addListener(new Alphabetizer());
		FileReader fr=new FileReader("input.txt");
		Inputer inputer=new Inputer(fr);
	}
}

KWICEvent.java

import java.util.EventObject;


public class KWICEvent extends EventObject{

	public KWICEvent(Object source) {
		super(source);
		// TODO Auto-generated constructor stub
	}
}

KWICListener.java:

import java.util.EventListener;


public interface KWICListener extends EventListener{
	
	public void handleEvent(KWICEvent event);
}

Outputer.java:

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;


public class Outputer {
	public static void println(TextLines textlines)throws IOException{
		FileWriter fw=new FileWriter("output.txt");
	    BufferedWriter bw=new BufferedWriter(fw);
	    for(int i=0;i<textlines.numberOfLines();i++){
	        bw.write(textlines.getLine(i));
	        bw.newLine();
	    }
	    bw.close();
	}
}

TextLine.java:

import java.util.ArrayList;

/**
 * data structure 
 * @author SaMichael
 *
 */
public class TextLine {
	private ArrayList<String> lines=null;
    public TextLine(){
        lines=new ArrayList<String>();
    }
    public String getLine(int index){
        return lines.get(index);
    } 
    public ArrayList<String> getLineList(){
    	return lines;
    }
    public void addLine(String line){
        lines.add(line);
    }
    public void addLine(int index,String line){
        lines.add(index,line);
    }
    public int numberOfLines(){
        return lines.size();
    }
    public int numberOfWords(int index){
        return lines.get(index).split(" ").length;
    }
    public  String  shiftwords(int line_index){
        String line=this.getLine(line_index);
        int temp_index=line.indexOf(' ');
        String temp1="";
        String temp2="";
        if(temp_index!=-1){
            temp1=line.substring(0,temp_index);
            temp2=line.substring(temp_index+1);
            lines.set(line_index,temp2+" "+temp1);
            return temp2+" "+temp1;
        }
        else return null;
    }
}

TextLines.java:

import java.util.ArrayList;

/**
 * data structure 
 * @author SaMichael
 *
 */
public class TextLines {
	private ArrayList<String> lines=null;
    public TextLines(){
        lines=new ArrayList<String>();
    }
    public String getLine(int index){
        return lines.get(index);
    } 
    public ArrayList<String> getLineList(){
    	return lines;
    }
    public void addLine(String line){
        lines.add(line);
    }
    public void addLine(int index,String line){
        lines.add(index,line);
    }
    public int numberOfLines(){
        return lines.size();
    }
    public int numberOfWords(int index){
        return lines.get(index).split(" ").length;
    }
}

UML类图:
在这里插入图片描述
input文件:
在这里插入图片描述
output文件在程序运行后显示的结果:
在这里插入图片描述

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

X-jazz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值