'Programming/Design Pattern'에 해당되는 글 6건

  1. 2013.10.18 Factory - Factory Method, Abstract Factory
  2. 2013.10.18 Decorator
  3. 2013.10.18 Observer
  4. 2013.10.18 Iterator
  5. 2013.10.18 Strategy
  6. 2013.10.18 Singleton
Programming/Design Pattern2013. 10. 18. 15:18

1. 팩토리 메소드 패턴

추상클래스 Creator의 구상클래스인 ConcreteCreator가 결국 Product 인터페이스의 
구상클래스인 ConcreteProduct를 이용해 Product를 생산해 낸다.

객체의 생성을 서브클래스에 맡긴다.
Main() {
Creator newFactory = new ConreteCreator();
Product newProduct = new newFactory.factoryMethod();
}

 




2. 추상 팩토리 패턴
Factory 인터페이스를 통해 제품A와 제품 B를 받을 수 있다. 이때, Factory의 구상 클래스가 
어느것이냐에 따라 이를 구성하고 있는 제품의 종류가 정해 진다.

 

3. 팩토리 메소드 패턴과 추상 팩토리 패턴
- 추상 팩토리 패턴: 객체의 구성(Composition)을 통해 제품을 생성 
-> 제품군을 만들때 사용 가능
- 팩토리 메소드 패턴: 클래스를 써서 제품을 생성





Posted by Brian B. Lee
Programming/Design Pattern2013. 10. 18. 13:40

데코레이터 패턴

- 기본 추상구성요소를 가지고 기능을 추가 및 수정


생성자

FileInputStream(File file)

FilterInputStream(InputStream in)

LineNumerInputStream(InputStream in)


Ex) InputStream in = 

LineNumerInputStream(new BufferedInputStream(new FileInputStream(file)))

1. FileInputStream = 데코레이터로 포장될 구성요소 -> 파일을 읽어들임

2. BufferedInputStream = 구상데코레이터 -> 속도향상(buffer사용), 한줄씩 읽어들이는 readLine() 메소드 제공

3. LineNumberInputStream = 구상데코레이터 -> 행번호를 붙여줌


ex) 자바 I/O


Posted by Brian B. Lee
Programming/Design Pattern2013. 10. 18. 12:58

옵저버패턴

- 관리 받을 클래스들이 Oserver 인터페이스를 구현하며 이때 Subject 한곳에서 이들을 관리하고 이벤트 같은 사건이 있을 때 모든 객체의 update() 메소드를 호출 함


Posted by Brian B. Lee
Programming/Design Pattern2013. 10. 18. 12:54

아이터레이터 패턴

• Iterator(반복자)의 역할
• ConcreteIterator(구체적인 반복자) 역할
• Aggregate(집합체)의 역할
• ConcreteAggregate(구체적인 집합체)의 역할

ConcreteAggregate 클래스에서 구현해야할 메소드 createIterator()
Public Iterator createIterator() 
return new ConcreteIterrator(this)
}

Main 함수에서 불러들인 Iterator
Iterator it = concreteAggregate.createiterator();







Posted by Brian B. Lee
Programming/Design Pattern2013. 10. 18. 12:44

스트러티지 패턴 =>

위임 덕택에 알고리즘의 교체, 동적인 교체가 가능해 진다.
Public Context(Strategy strartgy
this.strategy = strategy 
  }
Context con1 = new Context(new ConcreteStrategy1());
Context con2 = new Context(new ConcreteStrategy2());




Posted by Brian B. Lee
Programming/Design Pattern2013. 10. 18. 12:37

싱글톤 패턴

1. public class Singleton { => 동기화 해야 하기 때문에 속도가 느림

  private static Singleton uniqueInstance;

  // 기타 인스턴스 변수

  private Singleton() {}

  public static synchronized Singleton getInstance() {

    if(uniqueInstance == null) {

    uniqueInstance = new Singleton();

    }

    return uniqueInstance;

  }

  // 기타 메소드

}


2. public class Singleton { => 처음부터 메모리를 소비하고 속도를 높일 수 있음

  private static Singleton uniqueInstance = new Singleton();

  // 기타 인스턴스 변수

  private Singleton() {}

  public static Singleton getInstance() {

  return uniqueInstance;

  }

  // 기타 메소드

}


3. public class Singleton { => 속도와 메모리 두 마리 토끼 잡기

  private volatile static Singleton uniqueInstance;

  // 기타 인스턴스 변수

  // volatile은 언제나 최신값을 가짐(두가지 특징)

  // 1. Thread에 변수가 공유되면 각자의 Caching 값을 같고 있다 업데이트 하지만 volatile 변수의 경우

  //     Caching 하지 않고 바로 메인모메로 영역의 변수로 접근한다.

  // 2. 동기화를 지원하여 동시에 여러게의 Thread가 접근할 수 없다.

  private Singleton() {}

  public static Singleton getInstance() {

  if(uniqueInstance == null) { <= 처음에만 동기화하고 다음에는 접근하지 않음

    synchronized (Singleton.class) {

       if(uniqueInstance == null) { <= Double-checking locking

  uniqueInstance = new Singleton();

    }

    }

  }

  return uniqueInstance;

  }

  // 기타 메소드

}

Posted by Brian B. Lee