본문 바로가기

공부/응용 SW

6월12일 - properties + singleton

Properties Class

->Key와 Value구분해서 저장할 수 있다

->Key와 Value구분기호 = 또는 : 추천

 

key : value

자료 분리

tokenizer보다 더 편하게 사용할 수 있게 만든 것 .

 

package oop0612;

import java.io.FileInputStream;
import java.util.Iterator;
import java.util.Properties;

public class Test01_Properties {

	public static void main(String[] args) {
		// Properties 클래스
		//commend.properties 라는 메모장만들어서 데이터저장
		
		try{
			String fileName="D:/java0514/workspace/basicJava/src/oop0612/command.properties";
			FileInputStream fis=new FileInputStream(fileName);
			//pr변수에 command.properties 메모장에 있는 자료 입력하기
			Properties pr=new Properties(); 
			//command.properties 파일 가져오기
			//iterator은 가져오려는 파일에 커서를 대서 next, previous해서 가져올수있다.
			pr.load(fis); 
			
			Iterator iter=pr.keySet().iterator(); //key를 set으로 형변환한걸 iterator로 또 형변환시키기
			while(iter.hasNext()){
				//hashmap
				String key=(String) iter.next(); //=앞의 문자열을  메모장으로 자동으로 인식 
				String value=pr.getProperty(key); //=뒤를 자동으로 인식해서 출력
				System.out.println(value);
			}//while end
			
			
			
			
 		}catch(Exception e){
			System.out.println("실패:"+e);
		}//try end
		
	}//main() end
}//class end

 

라이브러리 Library

 

자바소스프로그램 .java

--  컴파일(번역)

자바목적프로그램 .class

 

.class 파일들 압축 -> .jar (library)

파일을 배포할 때엔 .jar 로 ~ !

 

라이브러리 생성하기 

해당프로젝트(ex. basicjava) 우클릭-> export -> java -> ruannable jar .. 

launch configuration 시작파일

export destination 종료파일

Singleton

 

새로운 프로젝트 singletonpattern을 만들었다. 

 

class Mountain{
	String name; 
	int height;    
	public Mountain(){}
}//class end

public class Test01_Mountain {

	public static void main(String[] args) {
		// 일반 클래스
		// 객체지향프로그램 Object Oriented Program
		
		// 자료형 레퍼런스변수 = 객체
		Mountain one=new Mountain();
		Mountain two=new Mountain(); 
 		
		// 주소값이 서로 다름
		System.out.println(one.hashCode());//366712642
		System.out.println(two.hashCode());//1829164700
		
		// 서로 다른 객체이다
		System.out.println(one); //kr.co.singleton1.Mountain@15db9742
		System.out.println(two); //kr.co.singleton1.Mountain@6d06d69c
		
		// three에 two의 값이 담긴다
		Mountain three=two;
		System.out.println(three.hashCode()); //1829164700
		System.out.println(three); //kr.co.singleton1.Mountain@6d06d69c
		
		

 

싱글톤클래스 설계하기

 

싱글톤클래스=매니저클래스

 

1) 싱글톤 클래스 만들기

Singleton.java

package kr.co.singleton1;

//싱글톤 클래스 설계하기
public class Singleton {
	//static int b; //static 주소값 공유continue되는 것, reset(stack공간)
	//void disp(){int a} //지역변수 -> stack
	
	//1) 외부에서 생성자 함수를 호출할 수 없다
	//->하지만 객체를 단 하나는 생성할 수 있게 해야한다.  
	//private로 하면 이 클래스 안에서만 사용가능하기때문에 다른 클래스에선 에러
	private Singleton(){} 
	
	//2) private으로 선언되어 있어도 내부클래스에서는 
	//-> 생성자 호출이 가능하다는 것을 이용한다.
	//-> 클래스에 고정되어 정적 필드로 자기 자신의 객체를 생성하였는데
	//-> 여기에도 private를 붙여서 외부클래스의 접근을 막는다.
	private static Singleton single=new Singleton(); //레퍼런스변수 single
	
	//3) 이 객체를 외부에서 접근할 수 있게 메소드를 만들어주어야 한다. 
	//외부에서
	static Singleton getSingle(){
		return single;
	}
	
}//class end

 

 

SingletonMainTest.java

		//singleton 파일에 생성자함수 private으로 막아놓으면 에러난다.
		//그러므로 new연산자로 객체를 생성할 수 없다. 
		//Singleton obj=new Singleton(); 
		
		//static 접근 -> 클래스명.static변수
		//                -> 클래스면.static함수()
		//stack공간에 같이 설계된 객체라 같은 값나옴 
		Singleton obj1=Singleton.getSingle();
		System.out.println(obj1); //kr.co.singleton1.Singleton@15db9742
		
		Singleton obj2=Singleton.getSingle();
		System.out.println(obj2); //kr.co.singleton1.Singleton@15db9742
		
		if(obj1==obj2){
			System.out.println("같은 객체");
		}else{
			System.out.println("다른 객체");
		}//if end
        
        //같은 객체

 

2) 싱글톤 클래스 연습

 

LeeSoonShin.java

//싱글톤 클래스 설계
public class LeeSoonShin {
	private String name;
	private int age;
	private String address;
	
	//1) 객체를 외부에서 만들 수 없도록 생성자 함수를 private속성으로 막아버리기
	private LeeSoonShin() {}
	private LeeSoonShin(String name, int age, String address) {
		this.name = name;
		this.age = age;
		this.address = address;
	}
	
	//getter, setter 생성(외부 연결 통로)
	//getter, setter는 public으로 열어준다
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public void info(){
		System.out.println(name);
		System.out.println(age);
		System.out.println(address);
	}
	
	//2) 내부에서 객체하나를 만들어서 상수화 
	// static final
	private static final LeeSoonShin lss=new LeeSoonShin("이순신",50,"조선");

	//3) 2)에서 선언된 레퍼런스변수(lss)에 대한 getter함수 만들기
	public static LeeSoonShin getLss() {
		return lss;
	}
	
}//class end

 

MainTest.java

//싱글톤 클래스는 new연산자로 객체생성할 수 없다.
//LeeSoonShin lss=new LeeSoonShin(); //에러
		
LeeSoonShin lss=LeeSoonShin.getLss();
lss.info();
System.out.println(lss);

출력결과
이순신
50
조선
kr.co.singleton2.LeeSoonShin@15db9742

'공부 > 응용 SW' 카테고리의 다른 글

6월20일 - SW활용 Oracle  (0) 2019.06.20
6월19일 - SW활용 : DB  (0) 2019.06.19
6월18일 - SW활용 네트워크  (0) 2019.06.18
6월17일 - SW활용 운영체제 + 네트워크(thread)  (0) 2019.06.17
6월13일 - DOS명령어 만들기  (0) 2019.06.13