暑假在家待了8天,回来就不出意外的开始了我新的代码生活,这个学期任务是文件管理和数据挖掘,据说挺有挑战的哦,不过我喜欢,有难度才能学到更多东西嘛.
回家后基本上没看什么书,就在回家的火车上看了几个设计模式以及设计模式沉思录的第二章,参照着书上的内容,稍微写了一点关于文件目录管理的代码,由于初学,就凭着这两本书动手写吧,自我感觉有点乱,以后再作调整吧.
package com.crazyj.Model;
import java.util.List;
public abstract class Node {
private Node parent = null;
private long size;
private String name;
public String getName() {
return this.name;
}
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
public List<Node> getChild() {
throw new UnsupportedOperationException("对象不支持此功能");
}
public abstract void printStruct(String preStr);
public void addChild(Node child) {
throw new UnsupportedOperationException("对象不支持此功能");
}
public void removeChild(Node child) {
throw new UnsupportedOperationException("对象不支持此功能");
}
public Node getChild(int index) {
throw new UnsupportedOperationException("对象不支持此功能");
}
public void Accept(Visitor v) {
throw new UnsupportedOperationException("对象不支持此功能");
}
public static void destroy(Node node) {
if (node.isWritable()) {
//delete note;
} else {
System.err.println(node.getName() + " cannot be deleted.");
}
}
protected boolean isWritable() {
return false;
}
public void StreamOut() {
}
}
package com.crazyj.Model;public class File extends Node{private String name = "";public String getName() {return name;}public File(String name) {this.name = name;}public void printStruct(String preStr) {System.out.println(preStr + "-" + this.getName());}public void Accept(Visitor v) {v.visit(this);}}
package com.crazyj.Model;
import java.util.ArrayList;
import java.util.List;
public class Directory extends Node {
private List<Node> childNode = null;
private String name = "";
public String getName() {
return name;
}
public Directory(String name) {
this.name = name;
}
public long getSize() {
long total = 0;
Node child;
for (int i = 0; getChild(i) != null; ++i) {
child = getChild(i);
total += child.getSize();
}
return total;
}
public void addChild(Node child) {
if (childNode == null) {
childNode = new ArrayList<Node>();
}
childNode.add(child);
child.setParent(this);
}
public void removeChild(Node child) {
if(childNode != null) {
int idx = childNode.indexOf(child);
if(idx != -1) {
for(Node n : child.getChild()) {
n.setParent(this);
childNode.add(n);
}
childNode.remove(idx);
}
}
}
public List<Node> getChild() {
return childNode;
}
public Node getChild(int index) {
if (childNode != null) {
if(index >=0 && index < childNode.size()) {
return childNode.get(index);
}
}
return null;
}
public void printStruct(String preStr) {
System.out.println(preStr + "+" + this.getName());
if(this.childNode != null) {
preStr += " ";
for(Node n : childNode) {
n.printStruct(preStr);
}
}
}
public void Accept(Visitor v) {
v.visit(this);
}
}
package com.crazyj.Tools;public class SubStr {public String SubPath(String path) {return path.substring(Head(path).length()+1);}public String Head(String path) {String[] names = path.split("/");return names[0];}}
上一个工具类,用于mkdir的路径的剪切
package com.crazyj.Model;
import com.crazyj.Tools.SubStr;
public class Client {
SubStr ss = new SubStr();
@SuppressWarnings("unused")
public void mkdir(Directory current, String path) {
String subPath = ss.SubPath(path);
if (subPath.equals("")) {
current.addChild(new Directory(path));
} else {
String name = ss.Head(path);
Node node = Find(name, current);
if (node != null) {
Directory child = (Directory) node;
if(child != null) {
mkdir(child, subPath);
} else {
System.err.println(child.getName() + " is not a directory.");
}
} else {
System.err.println(name + " nonexistent.");
}
}
}
public Node Find(String name, Directory current) {
for(Node child : current.getChild()) {
if(name.equals(child.getName())) {
return child;
}
}
return null;
}
public void Ls(Node node) {
SuffixPrinterVisitor spv = new SuffixPrinterVisitor();
for(int i = 0; node.getChild(i) != null; ++i) {
Node child = node.getChild(i);
child.Accept(spv);
System.out.println("");
}
}
/*public void Cat (Node node) {
Link l;
if((File) node != null) {
} else if ((Directory) node != null) {
} else if (l == (Link) node) {
Cat(l.getSubject());
}
}*/
}
package com.crazyj.Model;
public class Link extends Node{
private Node subject;
public Node getSubject() {
return subject;
}
public Node getChild(int n) {
return subject.getChild(n);
}
@Override
public void printStruct(String preStr) {
// TODO Auto-generated method stub
System.out.println(preStr + "+" + this.getName());
}
public void Accept(Visitor v) {
v.visit(this);
}
}
package com.crazyj.Model;
public class Visitor {
public void visit(File file) {
}
public void visit(Directory directory) {
System.err.println("Can't cat a directory.");
}
public void visit(Link link) {
link.getSubject().Accept(this);
}
}
package com.crazyj.Model;
public class SuffixPrinterVisitor extends Visitor{
public SuffixPrinterVisitor() {
}
public void visit(File file) {
}
public void visit(Directory directory) {
System.out.println("/");
}
public void visit(Link link) {
System.out.println("@");
}
}
总体用到了composite,proxy,visitor,template method,由于现在只是先实现单用户环境,singleton和mediator还没有用上,渐渐完善渐渐学习吧,
由于streamIn和streamout两个方法暂时没有写,老师让我先看操作系统的文件管理和java的file类,这部分代码先这样吧,到时候再加上后续功能.如果有大神给我指点一二当然感激不尽.