목록전체 글 (413)
오답노트
파사드 패턴 파사드는 건물의 앞쪽 정면이라는 뜻을 가진다. 여러 개의 객체와 실제 사용하는 서브 객체의 사이에 복잡한 의존관계가 있을 때, 중간에 파사드라는 객체를 두고, 여기서 제공하는 interface만을 활용하여 기능을 사용하는 방식이다. 패턴 적용 전 클래스 public class Ftp { private String host; private int port; private String path; public Ftp(String host, int port, String path){ this.host = host; this.port = port; this.path = path; } public void connect(){ System.out.println("FTP HOST : " + host + "..
옵저버 패턴 옵저버 패턴은 변화가 일어 났을 때, 미리 등록된 다른 클래스에 통보해주는 패턴을 구현한 것이다. 많이 보이는 곳은 event listener에서 해당 패턴을 사용하고 있다. 클래스 package observer; public class Button { private String name; private IButtonListener buttonListener; public Button(String name){ this.name = name; } public void click(String message){ buttonListener.clickEvent(message); } public void addListener(IButtonListener iButtonListener){ this.butt..
프록시 패턴 최초에 인스턴스를 캐싱해 놓고 그 이후에 다시 호출 될 때, 캐싱된 인스턴스를 사용해 시간을 절약하는 패턴이다. Class public interface IBrowser { public Html show(); } public class BrowserProxy implements IBrowser{ private String url; private Html html; public BrowserProxy(String url){ this.url = url; } @Override public Html show() { if(html == null){ this.html = new Html(url); System.out.println("BrowserProxy loading html from : " + ur..
어댑터 패턴 서로 다른 두 클래스를 연결하는 클래스를 만들어 주는 패턴이다. A 인터페이스를 사용하는 클래스 (이하 A 클래스)에 B 인터페이스를 사용하는 클래스(이하 B 클래스)를 연결하고 싶다면 A 인터페이스를 사용하는 클래스를 만들고 생성자 인자로 B 인터페이스를 받는 클래스를 만들어 A 인터페이스를 Override하여 사용하면된다. 인터페이스 public interface Electronic110V { void poewrOn(); } public interface Electronic220V { void connect(); } 인터페이스를 사용하는 클래스 public class AirConditioner implements Electronic220V{ @Override public void conn..
Critical Section 과 Semaphore Critical Section은 두 개 이상의 thread가 동시에 접근하는 경우 문제가 생길 수 있기 떄문에 동시에 접근할 수 없는 영역 Semapore는 특별한 형태의 시스템 객체이며 get/release 두 개의 기능이 있다. 한 순간 오직 하나의 thread만이 semaphore를 얻을 수 있고, 나머지 thread들은 대기(blocking) 상태가 된다. Semaphore를 얻은 thread 만이 critical section에 들어갈 수 있다. Synchronized class Bank{ private int money = 10000; public synchronized void saveMoney(int save) { int m = getMo..
Thread 프로그램이 실행되면 OS로 부터 메모리를 할당받아 프로세스 상태가 된다. thread 하나의 프로세스는 하나 이상의 thread를 가지게 되고, 실제 작업을 수행하는 단위는 thread이다. Multi - Thread 쓰레드가 동시에 수행되는 프로그래밍이다. 이는 여러 작업이 동시에 실행되는 효과가 있다. 하지만 쓰레드가 모두 공유하는 데이터에 대해서 동기화를 구현하지 않으면 오류가 발생할 수 있다. Thread 객체 상속 class MyThread extends Thread{ public void run() { int i; for(i = 1; i < 201 ; i++) { System.out.print(i+"\t"); } } } public class ThreadTest { public s..

Decorator Pattern 자바의 입출력 스트림은 모두 데코레이터 패턴으로 만들어졌다. 상속보다 유연한 구현방식을 가지고 있다. 1. abstract 객체를 상속 받는 자식 객체 2. 1번의 abstract 객체를 상속 받는 abstract 자식 객체
직렬화 인스턴스의 상태를 그대로 파일 저장하거나 네트웍으로 전송하고 이를 다시 복원하는 방식이다. 자바에서는 보조 스트림을 활용하여 직렬화를 제공한다. 스트림을 활용한 직렬화 import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; class Person implements Serializable{ String name; transient String job; // 직렬화 안됨 public Person() {} public ..
입력 스트림과 출력 스트림 입력 스트림 : 대상으로부터 자료를 읽어 들이는 스트림 (ex. FileInputStream, FileReader, BufferedInputStream, BufferedReader) 출력 스트림 : 대상으로 자료를 출력하는 스트림 (ex. FileOutputStream, FileWriter, BufferedOutputStream, BufferedWriter) import java.io.IOException; import java.io.InputStreamReader; public class SystemInTest { public static void main(String[] args) { // TODO Auto-generated method stub System.out.prin..
사용자 정의 예외 처리 Exception 클래스에서 상속을 받아 throws로 받거나 catch의 인자로 사용할 수 있다. public class PassWordTest { private String password; public String getPassword() { return password; } public void setPassword(String password) throws PassWordException { if(password == null) { throw new PassWordException("비밀번호는 null일 수 없습니다."); } else if(password.length() < 5) { throw new PassWordException("비밀번호는 5자 이상이어야합니다.")..