暑假在家待了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類,這部分代碼先這樣吧,到時候再加上後續功能.如果有大神給我指點一二當然感激不盡.