public class Adaptee {
public void specificRequest() {
System.out.println("Adaptee.speificRequest()");
}
}
public interface Target {
public void request();
}
public class Adapter extends Adaptee implements Target {
public void request() {
this.specificRequest(); // 다중상속
}
}
public class Client {
public static void main(String[] args) {
Target target = new Adapter();
target.request();
}
}
public class Adaptee {
public void specificRequest() {
System.out.println("Adaptee.speificRequest()");
}
}
public interface Target {
public void request();
}
public class Adapter implements Target {
Adaptee adaptee = new Adaptee(); // 객체를 만들고
public void request() {
adaptee.specificRequest(); // 객체를 연결한다.
}
}
public class Client {
public static void main(String[] args) {
Target target = new Adapter();
target.request();
}
}
import java.util.ArrayList;
import java.util.Collection;
/**
*
* Node
*
* Tree 사용 노드
*
* @author 장선웅
*
*/
public class Node {
String name = null; // 이름
String value = null; // 값
Node parent = null; // 부모노드
// 이펙티브 자바 항목 34(p205) : 인터페이스 타입으로 객체를 참조하라.
// 자식노드들.
Collection<Node> childs = new ArrayList<Node>();
Node(Node parent, String attributeName, String attributeValue) {
this.parent = parent;
if(parent != null)
parent.addChilde(this);
name = attributeName;
value = attributeValue;
}
void addChilde(Node child) {
childs.add(child);
}
}
import java.util.Collection;
import java.util.HashSet;
/**
* SimpleTree
*
* 간단한 트리
*
* @author 장선웅
*
*/
public class Tree {
Node root = null;
Tree(String rootName, String rootValue) {
createRootNode(rootName, rootValue);
}
/**
* 노드의 앞부분을 출력한다
*
* ex) <NAME>TEXT
*
* @param node 부모노드
* @param depth 트리깊이
* @return 자식노드
*/
static Collection<Node> print(Node node, int depth) {
for(int i=0; i<depth; i++) {
System.out.print("\t");
}
System.out.println("<" + node.name + ">");
if (!(node.value instanceof String) || !(node.value.equals(""))) {
for(int i=0; i<=depth; i++) {
System.out.print("\t");
}
System.out.println(node.value);
}
return node.childs;
}
/**
* 노드의 뒷부분을 출력한다.
*
* ex) </NAME>
*
* @param node 부모노드
* @param depth 트리깊이
* @return 자식노드
*/
static Collection<Node> printEnd(Node node, int depth) {
for(int i=0; i<depth; i++) {
System.out.print("\t");
}
System.out.println("</" + node.name + ">");
return node.childs;
}
/**
* 트리를 출력한다.
* @param root root노드
* @param depth root의 깊이
*/
static void printAll(Node root, int depth) {
Collection<Node> childnodes = null;
childnodes = print(root, depth);
if(childnodes.size() > 0)
++depth;
for(Node child: childnodes) {
printAll(child, depth);
}
if(childnodes.size() > 0)
--depth;
printEnd(root, depth);
}
/**
* root노드를 생성한다.
*
* @param attributeName root노드 이름
* @param attributeValue root노드 값
*/
void createRootNode(String attributeName, String attributeValue) {
root = new Node(null, attributeName, attributeValue);
}
/**
* 자식노드를 생성한다.
*
* @param parent 자식노드의 부모
* @param attributeName 자식노드의 이름
* @param attributeValue 자식노드의 값
* @return 생성된 자식노드
*/
static Node createChildNode(Node parent, String attributeName, String attributeValue) {
return new Node(parent, attributeName, attributeValue);
}
Node getRoot() {
return root;
}
/**
* 스터디 회원목록을 출력한다.
*
* @param args
*/
public static void main(String[] args) {
Tree tree = new Tree("스터디", "");
Node root = tree.getRoot();
Node oracle = tree.createChildNode(root, "그룹", "대용량데이터베이스");
Node ora1 = tree.createChildNode(oracle, "멤버", "많음..;");
Node java = tree.createChildNode(root, "그룹", "자바웹개발");
Node member1 = tree.createChildNode(java, "멤버", "유진우");
Node member1a = tree.createChildNode(member1, "디자인패턴", "템플릿 메소드");
Node member2 = tree.createChildNode(java, "멤버", "장선웅");
Node member2a = tree.createChildNode(member2, "디자인패턴", "어뎁터 & 퍼사드");
Node member2b = tree.createChildNode(member2, "이펙티브자바", "C구분 바꾸기");
Node member3 = tree.createChildNode(java, "멤버", "장선웅");
Node member3a = tree.createChildNode(member3, "디자인패턴", "어뎁터 & 퍼사드");
Node member3b = tree.createChildNode(member3, "이펙티브자바", "C구분 바꾸기");
tree.printAll(tree.root, 0);
}
}
<스터디>
<그룹>
대용량데이터베이스
<멤버>
많음..;
</멤버>
</그룹>
<그룹>
자바웹개발
<멤버>
유진우
<디자인패턴>
템플릿 메소드
</디자인패턴>
</멤버>
<멤버>
장선웅
<디자인패턴>
어뎁터 & 퍼사드
</디자인패턴>
<이펙티브자바>
C구분 바꾸기
</이펙티브자바>
</멤버>
<멤버>
장선웅
<디자인패턴>
어뎁터 & 퍼사드
</디자인패턴>
<이펙티브자바>
C구분 바꾸기
</이펙티브자바>
</멤버>
</그룹>
</스터디>
import java.util.Collection;
import java.util.HashSet;
/**
* SimpleTree
*
* 간단한 트리
*
* @author 장선웅
*
*/
public class Tree {
Node root = null;
Tree(String rootName, String rootValue) {
createRootNode(rootName, rootValue);
}
/**
* 노드의 앞부분을 출력한다
*
* ex) <NAME>TEXT
*
* @param node 부모노드
* @param depth 트리깊이
* @return 자식노드
*/
static Collection<Node> print(Node node, int depth) {
for(int i=0; i<depth; i++) {
System.out.print("\t");
}
System.out.println("<" + node.name + ">");
if (!(node.value instanceof String) || !(node.value.equals(""))) {
for(int i=0; i<=depth; i++) {
System.out.print("\t");
}
System.out.println(node.value);
}
return node.childs;
}
/**
* 노드의 뒷부분을 출력한다.
*
* ex) </NAME>
*
* @param node 부모노드
* @param depth 트리깊이
* @return 자식노드
*/
static Collection<Node> printEnd(Node node, int depth) {
for(int i=0; i<depth; i++) {
System.out.print("\t");
}
System.out.println("</" + node.name + ">");
return node.childs;
}
/**
* 트리를 출력한다.
* @param root root노드
* @param depth root의 깊이
*/
static void printAll(Node root, int depth) {
Collection<Node> childnodes = null;
childnodes = print(root, depth);
if(childnodes.size() > 0)
++depth;
for(Node child: childnodes) {
printAll(child, depth);
}
if(childnodes.size() > 0)
--depth;
printEnd(root, depth);
}
/**
* root노드를 생성한다.
*
* @param attributeName root노드 이름
* @param attributeValue root노드 값
*/
void createRootNode(String attributeName, String attributeValue) {
root = new Node(null, attributeName, attributeValue);
}
/**
* 자식노드를 생성한다.
*
* @param parent 자식노드의 부모
* @param attributeName 자식노드의 이름
* @param attributeValue 자식노드의 값
* @return 생성된 자식노드
*/
static Node createChildNode(Node parent, String attributeName, String attributeValue) {
return new Node(parent, attributeName, attributeValue);
}
Node getRoot() {
return root;
}
// 간단한 퍼사드 패턴을 이용해서 복잡한 행동을 일련의 메소드/클래스로 변환시킨다.
public void addDatas() {
Node root = getRoot();
Node oracle = createChildNode(root, "그룹", "대용량데이터베이스");
Node ora1 = createChildNode(oracle, "멤버", "많음..;");
Node java = createChildNode(root, "그룹", "자바웹개발");
Node member1 = createChildNode(java, "멤버", "유진우");
Node member1a = createChildNode(member1, "디자인패턴", "템플릿 메소드");
Node member2 = createChildNode(java, "멤버", "장선웅");
Node member2a = createChildNode(member2, "디자인패턴", "어뎁터 & 퍼사드");
Node member2b = createChildNode(member2, "이펙티브자바", "C구분 바꾸기");
Node member3 = createChildNode(java, "멤버", "장선웅");
Node member3a = createChildNode(member3, "디자인패턴", "어뎁터 & 퍼사드");
Node member3b = createChildNode(member3, "이펙티브자바", "C구분 바꾸기");
}
/**
* 스터디 회원목록을 출력한다.
*
* @param args
*/
public static void main(String[] args) {
Tree tree = new Tree("스터디", "");
tree.addDatas(); // 정의한 메소드 호출
printAll(tree.getRoot(), 0);
}
}
"정말 쉽죠?"