2013년 9월 25일 수요일

흥미로운 구조...


흠 흥미로운 클래스구조를 발견함

//

package com.jetddo.study.test;

public interface Page {
 
 public void message();

}

//////////////////////////////////

package com.jetddo.study.test;

public class BasePage implements Page {
 
 protected static Page m_instance;
 
 public static Page getInstance(){
  
  if(m_instance == null){
   m_instance = new BasePage(); 
  }
  
  return m_instance;
 }
 
 public void message(){
  
  System.out.println("Base Page");
 }
}

////////////////////////////////////////

package com.jetddo.study.test;

public class ExecPage extends BasePage {
 
 public static Page getInstance(){
  
  if(m_instance == null){
   m_instance = new ExecPage(); 
  }
  
  return m_instance;
 }
 
 public void message(){
  
  System.out.println("Exec Page");
 }
}

//////////////////////////////////////
 
싱글톤 기법인거 같은데..

protected 로 선언하면서 

확장도 가능하게 변형한거 같다.

//

package com.jetddo.study.test;

import org.junit.Test;

public class TestMain {
 
 @Test
 public void test(){
  
  
  
  ExecPage.getInstance();
  
  BasePage.getInstance().message();
  
 }

}

//

m_Instance 가 static 인것을 이용해 클래스간의 변수를 공유하게 하는 방법이다.
클래스를 재정의 하고 싶다면 구현후 ExecPage.getInstance(); 이것만 추가하면 객체가 바뀐다.

javascript


javascript 에서 java 의 package 나 c 의 namespace 처럼
객체를 체계적으로 관리 할 수 있다.
//

if(typeof window.jetddo =='undefined') window.jetddo = {};
if (!jetddo.test) jetddo.test = {};

jetddo.test.Class = new (function(){
			
	this.initClass = function(htOptions){
		alert(JSON.stringify(arguments[0]));
	};
			
})();
		
jetddo.test.Class = {
				
		initClass : function(htOptions){

					
				alert(JSON.stringify(arguments[0]));
					
		}
				
};

//
위에 javascript 객체를 사용하는데 있어서 두가지 방법이 있다.

위에 있는 것은 Class 라는 객체를 정의하는데 있어서 function 으로 위임하는 것이다.

즉 일종의 생성자가 되는 것이다.

아래에 있는 것은 객체를 json 형태로 규정해서 각 변수를 정의 한다고 볼 수 있다.