總網頁瀏覽量

2012年2月22日 星期三

JAVA設計模式


複合(composition)或聚合(aggregation) ---   has-a
即以既有的classes合成新的class
將某個class物件置於另一個class
新的classes可由任意數目 任意型別的他種物件組成

繼承
父類別 --- base class, super class, parent class
子類別 --- derived class, inherited class, sub class, child class
子類別完全覆寫父類別 is-a
子類別加入新介面元素 is-like-a

陣列宣告
1. Integer[] a = new Integer[3];
2. Integer[] a = new Integer[] {new Integer(1), new Integer(2), 3,};
3. Integer[] a = {new Integer(1), new Integer(2), 3,};
//初值列最後的逗號可有可無


委任(delegation)
介於繼承與合成之間,將成員物件置於新建立的class(類似合成),但同時又將該成員的所有methods都放到新的class中,成為新的class介面的一部分(類似繼承)

向上轉型(upcasting)
derived class中可以使用base class的所有函式,所以任何可發送給base class的訊息,也都可以發送給derived class

多型(Polymorphism)
介面與實作分離,

工廠模式
用已產生其他其他類別的類別

抽象類別(abstract class)
用來表示介面,沒有專屬的實作內容,含有0個或1個以上的abstract method的類別稱為abstract class,且不可產生物件,繼承abstract classderived class需要實作abstract method

介面(interface)
可以內含資料成員,但這些資料成員都會自然成為staticfinal,介面只提供形式,不含實作內容,介面可允許設計出可向上轉型至多個基礎型別的class,以達到多重繼承的變形,且不可產生物件

Strategy設計模式
依據所傳入的引數物件不同,而有不同行為展現的method

Adapter
撰寫程式碼來接收既有的介面,並且產生你所需的介面



class Member{
private int mid;
private String name;
private Car car;
public Member(int mid, String name){
this.mid = mid;
this.name = name;
}
public String getMemberInfo() {
return "編號" + this.mid + ", 姓名" + this.name;
}
public void setCar(Car car){
this.car = car;
}
public Car getCar(){
return this.car;
}
}
class Car{
private Member member;
private String name;
public Car(String name){
this.name = name;
}
public String getCarInfo(){
return "車的名稱 " + this.name;
}
public void setMember(Member member){
this.member = member;
}
public Member getMember(){
return this.member;
}
}

public class Hello {
public static void main(String args[]) {
Member mem = new Member(1, "kent");
Car car = new Car("BMW");
mem.setCar(car);
car.setMember(mem);
System.out.println(mem.getMemberInfo());
System.out.println(mem.getCar().getCarInfo());
}
}







class Node{
                private String data;
                private Node next;
                public Node(String data){
                                this.data = data;
                }
                public void setNext(Node next){
                                this.next = next;
                }
                public Node getNext(){
                                return this.next;
                }
                public String getData(){
                                return this.data;
                }
                public void addNode(Node newNode){
                                if(this.next == null){
                                                this.next = newNode;
                                }else{
                                                this.next.addNode(newNode);
                                }
                }
                public void printNode(){
                                System.out.println(this.data);
                                if(this.next != null){
                                                this.next.printNode();
                                }
                }
                public boolean containsNode(String data){
                                if(this.data.equals(data)){
                                                return true;
                                }else{
                                                if(this.next != null){
                                                                return this.next.containsNode(data);
                                                }else{
                                                                return false;
                                                }
                                }
                }
                public void removeNode(Node previous, String data){
                                if(this.data.equals(data)){
                                                previous.next = this.next;
                                }else{
                                                this.next.removeNode(this, data);
                                }
                }
}

class Link{
                private Node root;
                public void add(String data){
                                if(data == null){
                                                return;
                                }
                                Node newNode = new Node(data);
                                if(this.root == null){
                                                this.root = newNode;
                                }else{
                                                this.root.addNode(newNode);
                                }
                }
                public void print(){
                                if(this.root != null){
                                                this.root.printNode();
                                }
                }
                public boolean contains(String data){
                                if(data == null || this.root == null){
                                                return false;
                                }
                                return this.root.containsNode(data);
                }
                public void remove(String data){
                                if(this.contains(data)){
                                                if(this.root.getData().equals(data)){
                                                                this.root = this.root.getNext();
                                                }else{
                                                                this.root.getNext().removeNode(this.root, data);
                                                }
                                }
                }
}

public class Hello{
                public static void main(String args[]){
                                Link all = new Link();
                                all.add("A");
                                all.add("B");
                                all.add("C");
                                all.remove("B");
                                all.remove("A");
                                all.print();
                                System.out.println(all.contains("A"));
                                System.out.println(all.contains("X"));
                               
                }
}