2013-10-10 4 views
-1

복제본과 함께 프로토 타입을 사용하는 대신 클래스 개체를 사용할 수 있다고 읽었습니다. 하지만 그 대신 클래스 객체를 사용한다는 것이 무슨 뜻인지 이해하지 못합니까? 어떤 사람이 프로토 타입 패턴 대신 클래스 객체를 사용한다는 것을 이해한다면 누구나 예제를 줄 수 있습니까?프로토 타입 대신 Java 클래스 객체가 사용됩니까?

+3

우리는 여기에 자바 나 자바 스크립트에 대해 말을하는거야? –

+2

질문이 명확하지 않습니다. 명확히하십시오. –

+2

읽은 기사에 대한 링크가 있습니까? 프로토 타이핑은 객체 계층 구조를 생성 할 때 솔루션이며 관련된 연산은 비싸거나 실현 가능하지 않습니다. 게임 상태의 예를 들어 봅시다 - 2 시간 동안 게임을하고 상태를 저장했다고합시다. 다시 시작할 때 2 시간 정도의 모든 작업을 실행하는 것보다 이전에 저장 한 상태를 복제하는 것이 더 쉽습니다. 따라서 읽은 내용을 참고하면 더 나은 대답을 얻을 수 있습니다. –

답변

1

Java 5부터는 java.lang.Class이 자체 유형에 대해 일반적입니다. 이것에 의해, 클래스는 형태 보증 된 방법으로 인스턴스를 생성 할 수 있습니다.

interface Calculator { 
    void setA(int a); 
    void setB(int b); 
    int compute(); 
    Calculator copy(); 
} 
class Adder implements Calculator { 
    private int a,b; 
    public void setA(int a) {this.a=a;} 
    public void setB(int b) {this.b=b;} 
    public int compute() {return a+b;} 
    public Calculator copy() { 
     Adder res = new Adder(); 
     res.a = a; 
     res.b = b; 
     return res; 
    } 
} 
class Multiplier implements Calculator { 
    private int a,b; 
    public void setA(int a) {this.a=a;} 
    public void setB(int b) {this.b=b;} 
    public int compute() {return a*b;} 
    public Calculator copy() { 
     Multiplier res = new Multiplier(); 
     res.a = a; 
     res.b = b; 
     return res; 
    } 
} 
class Test { 
    static void computeWithPrototype(Calculator proto) { 
     Calculator calc = proto.copy(); 
     calc.setA(123); 
     calc.setB(321); 
     System.out.println(calc.compute()); 
    } 
    public static void main(String[] args) throws Exception { 
     computeWithPrototype(new Adder()); 
     computeWithPrototype(new Multiplier()); 
    } 
} 

Demo of the above approach on ideone : 당신이 복제와 프로토 타입을 사용하는 경우

, 당신은 같은 것을 할.

당신은이 같은 대신 copy 방법의 Class<T>로 다시 쓸 수 있습니다 :

interface Calculator { 
    void setA(int a); 
    void setB(int b); 
    int compute(); 
} 
class Adder implements Calculator { 
    private int a,b; 
    public void setA(int a) {this.a=a;} 
    public void setB(int b) {this.b=b;} 
    public int compute() {return a+b;} 
} 
class Multiplier implements Calculator { 
    private int a,b; 
    public void setA(int a) {this.a=a;} 
    public void setB(int b) {this.b=b;} 
    public int compute() {return a*b;} 
} 
class Test { 
    static <T extends Calculator> void computeWithClass(Class<T> calcClass) 
    throws Exception { 
     Calculator calc = calcClass.newInstance(); 
     calc.setA(123); 
     calc.setB(321); 
     System.out.println(calc.compute()); 
    } 
    public static void main(String[] args) throws Exception { 
     computeWithClass(Adder.class); 
     computeWithClass(Multiplier.class); 
    } 
} 

Demo of the second approach on ideone.

0

자바에서는 객체를 생성 할 때 참조 메모리가 있습니다. 따라서 변수에 객체를 할당하려고하면 참조 메모리를 전달합니다.

예 :

당신이 모두 수정이 메모리 참조에 속하는 속성을 수정하여 이러한 이유로
Person a = new Person(); a.setName("Person abc"); 
Person b = a; b.setName("Person yzw"); 
System.out.print(a.getName()); 
System.out.print(b.getName()); 

. 인쇄 예정 : "yzw yzw";

그래서 당신은 그것이 발생하는 것을 원하므로 Cloneable를 인터페이스를 사용하지 않는 경우 :

public class Person implements Cloneable{ 

    protected Object clone() throws CloneNotSupportedException { 
     return super.clone(); 
    } 
} 

그래서 당신은 클론() 메서드를 호출 할 때이 두 개의 서로 다른 목적이있을 것이다. 예 :

Person a = new Person(); 
a.setName("Person abc"); 
Person b = (Person)a.clone(); 
System.out.print(a.getName()); 
System.out.print(b.getName()); 

인쇄 할 항목 : "abc yzw";