장점
개발자간에 원활한 소통
소프트웨어 구조 파악이 용이
재사용을 통한 개발 시간을 단축
설계 변경 요청에 대한 유연한 처리
단점
객체지향 설계 / 구현
초기 투자비용에 대한 부담
생성 패턴 : 객체를 생성하는 것과 관련된 패턴
싱글톤 패턴, 빌더 패턴, 팩토리메서드 패턴
구조 패턴 :
프로그램 내의 자료구조나 인터페이스 구조 등을 설계하는데 활용 될 수 있는 패턴,
복잡한 구조를 개발하기 쉽게 만들어 주고, 유지 보수 하기 쉽게 만들어 준다.
어댑터 패턴, 데코레이터 패턴, 파사드 패턴, 프록시 패턴
행위 패턴 :
반복적으로 사용되는 객체들의 상호 작용을 패턴화한 것으로 클래스들
간에 책임을 분산하는 방법을제공
템플릿 메서드 패턴
Iterator
옵저버 패턴
스트래티지 패턴 (전략)
● try 블록에는 예외가 발생할 가능성이 있는 코드를 작성하고 try 블록 안에서 예외가 발생하는 경우 catch 블록이 수행됨
코드로 알아보기
package ch08;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
publicclassFileExceptionHanding{
publicstaticvoidmain(String[] args){
// 파일 <-- IO<--!!!
FileInputStream fis = null;
// try {// fis = new FileInputStream("temp");// <-- 예외가 발생할수있다라고 알려준다// } catch (FileNotFoundException f) {// System.out.println("temp 라는 파일이 없습니다");// }// try-catch-finallytry {
fis = new FileInputStream("test1.txt");
return;
} catch (FileNotFoundException e) {
// e.printStackTrace();
System.out.println("파일명 확인해주세용!! 힛");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 반드시 수행되는 코드// 심지어 return 키워드를 만나더라도 실행이된다 !! 아주 강력한 녀석
System.out.println("finally 실행");
}
System.out.println("비정상 종료 되지않음");
}// end of main
}// end class
package ch08;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
publicclassFileExceptionHanding2{
publicstaticvoidmain(String[] args){
Myfile file = new Myfile();
try {
file.inputData("하이~~");
} catch (FileNotFoundException e) {
System.out.println("파일이 없네요");
}
}// end of main
}// end classclassMyfile{
// throws 던진다-->// 누군가가 내가만든 Myfile 를 사용 할때// inputData 오류가 날수 있으니 예외처리 흐름은 사용 하는 사람이// 알아서 구현해!!publicvoidinputData(String str)throws FileNotFoundException {
FileInputStream fis;
fis = new FileInputStream("test1.txt");
}
}
package ch08;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
publicclassMyException{
publicstaticvoidmain(String[] args){
TxtFileInputManager inputManager = new TxtFileInputManager("test.txt");
try {
String result = inputManager.readTxrFileData();
System.out.println("result " + result);
} catch (IOException e) {
e.printStackTrace();
}
}// end main
}// end classclassTxtFileInputManager{
// 외부 파일을 내 메모리 상으로 가져올수 있는 녀석private FileInputStream fis;
private String fileName;
publicTxtFileInputManager(String fileName){
this.fileName = fileName;
}
public String readTxrFileData()throws IOException {
// IOException 부모!// FileNorFoundException 보다 부모
fis = new FileInputStream(fileName);
Properties prop = new Properties();
prop.load(fis);
// KEY=VALUE <-- 데이터를 읽어주는녀석// name=홍길동 --> 홍길동 --> 추출
String name = prop.getProperty("name");
return name;
}
}
예제풀이
package ch08;
publicclassMainTest2{
publicstaticvoidmain(String[] args){
// 예외 처리 구문작성// 10 /0 연산 시키고 예외 처리까지 작성해주세여// try// 클래스 설계int ten = 10;
int zero = 0;
try {
int result = ten / zero;
// int result = zero/ten;
System.out.println("수식 결과는 : " + result);
} catch (Exception e) {
System.out.println("수식이 맞지 않습니다");
} finally {
System.out.println("finally 실행");
}
System.out.println("비정상 종료 되지않았습니다");
}// end of main
}// end of class
package ch08;
publicclassMainTest2{
publicstaticvoidmain(String[] args){
// 예외 처리 구문작성// 10 /0 연산 시키고 예외 처리까지 작성해주세여// try// 클래스 설계int ten = 10;
int zero = 0;
try {
//int result = ten / zero;int result = zero/ten;
System.out.println("수식 결과는 : " + result);
} catch (Exception e) {
System.out.println("수식이 맞지 않습니다");
} finally {
System.out.println("finally 실행");
}
System.out.println("비정상 종료 되지않았습니다");
}// end of main
}// end of class