검색결과 리스트
Java/DesignPattern에 해당되는 글 4건
- 2011/02/08 inheritance and composition on Java
- 2010/11/25 행동패턴 - 책임 연쇄 (chain of responsibility)
- 2009/10/15 Creational Pattern
- 2009/08/19 Fundamental Design Patterns
글
Java/DesignPattern 2011/02/08 11:20inheritance and composition on Java
예전에 디자인 패턴 공부하기에 앞서 머리속에 잘 정리되어 있어야할 필수 요소로서
상속, 합성에 관한 정리가 필요했다.
GoF 에 보면 두번째 객체 지향 설계의 원칙으로
상속을 남발하지 말고 합성에 좀 더 의존하여 설계할 때 더 reusable하다는 결론을 내기위한 첫구문이었다;;(?)
아래처럼 정리했었는데 . . . GoF에 생성패턴, 행동패턴만 살펴 보고 거의 1년여째 미루고 있다;;;
공부 좀 해야하는데;
상속?
단점 :
'Java > DesignPattern' 카테고리의 다른 글
| inheritance and composition on Java (0) | 2011/02/08 |
|---|---|
| 행동패턴 - 책임 연쇄 (chain of responsibility) (0) | 2010/11/25 |
| Creational Pattern (0) | 2009/10/15 |
| Fundamental Design Patterns (0) | 2009/08/19 |
트랙백
댓글
글
Java/DesignPattern 2010/11/25 02:28행동패턴 - 책임 연쇄 (chain of responsibility)
'Java > DesignPattern' 카테고리의 다른 글
| inheritance and composition on Java (0) | 2011/02/08 |
|---|---|
| 행동패턴 - 책임 연쇄 (chain of responsibility) (0) | 2010/11/25 |
| Creational Pattern (0) | 2009/10/15 |
| Fundamental Design Patterns (0) | 2009/08/19 |
태그
책임 연쇄 패턴트랙백
댓글
글
Java/DesignPattern 2009/10/15 14:00Creational Pattern
Creational Pattern 관련 자료들 - 개요, 장단점, 확장성/재활용성 측면, 타 패턴과의 관계
Abstract Factory Pattern은 상태에 따라 어떤 객체를 생성하여 반환할 지에 포커스가 맞춰진 패턴
구체적 클래스를 정의하지 않고도 서로 관련성이 있거나
독립적인 여러 객체의 군을 생성하기 위한 인터페이스 제공
정의: 추상적인 부품을 조립해서 추상적인 제품을 만드는 추상적인 공장 같은 패턴
: 클라이언트에서 구상 클래스를 지정하지 않으면서도 일군의 객체를 생성할 수 있도록 하는 패턴
활용:
1. 생성되고 구성되고 표현되는 방식과 무관하게 시스템을 독립적으로 만들고자 할 때
2. 하나 이상의 제품군들 중 하나를 선택해서 시스템을 설정해야 하고 한번 구성한 제품을 다른 것으로 대체 가능할 때
3. 관련된 객체군을 함께 사용해서 시스템을 설계하고, 이 제품이 갖는 제약사항을 따라
야 할 때
4. 제품에 대한 클래스 라이브러리를 제공하고, 그들의 구현이 아닌 인터페이스를 표현하
고 싶을 때
결과:
1.구체적인 클래스를 분리한다 –
추성클래스가 정의한 인터페이스를 통해서만 클라이언트 프로그램을 작성한다
2. 제품군을 쉽게 대체할 수 있도록 한다
3. 제품간의 일관성을 증진한다
4. 새로운 종류의 제품을 제공하기 어렵다 –
새로운 제품에 대해 추상 팩토리를 확장하는 것이 어렵다
필요하므로 확장성이 떨어진다고 볼수 있다.
-Builder
(생성)과정은 변하지 않는다.
공정, 부품(operation)만 변한다. -> 다른 표현을 만들 수 있게 한다.
Builder는 객체의 단계별 생성에 중점을 준다. 생성한 제품을 반환.
Abstract Factory 패턴은 제품의 유사군을 생성해달라고 요청만 한다. 즉시 각 부품을 반환.
Builder Pattern
복잡한 객체를 생성하는 방법과 표현하는 방법을 정의하는 클래스를 별도로 분리해
서로 다른 표현이라도 이를 생성할 수 있는 동일한 구축 공정을 제공할 수 있다
정의: 구조를 가진 인스턴스를 쌓아 올리는 패턴 : 제품을 여러 단계로 나눠서 만들 수 있도록 제품 생산 단계를 캡슐화하고 싶을때 사용
활용:
1. 복합 객체의 생성 알고리즘이 이를 합성하는 요소 객체들이 무엇인지
이들의 조립방법에 독립적일 때 – RTF reader
2. 합성할 객체들의 표현이 서로 다르더라도 구축 공정이 이를 지원해야 할 때
결과:
1. 제품에 대한 내부 표현을 다양하게 변화할 수 있다
2. 생성과 표현에 필요한 코드를 분리한다
3. 복합객체를 생성하는 공정을 더 세밀히 나눌 수 있다 –
director의 buildPart()를 통해
관련 패턴: Abstract Factory 패턴이 복잡한 객체를 생성해야 하는 경우 Builder 패턴과 비슷한데, Builder는 복잡한 객체의 단계별 생성에 중점을 두고, Abstract Factory는 제품의
유사군들이 존재하는 경우 유연한 설계에 중점을 둔다.
Builder 패턴은 생성의 마지막 단계에서 생성한 제품을 반환하고 Abstract Factory 패턴에서는 만드는 즉시 제품을 반환한다. 이는 Abstract Factory 패턴에서 만드는 제품은 꼭
모여야만 의미있는 것이 아니라 하나만으로도 의미가 있기 때문이다
Builder패턴의 유용성
복잡한 객체를 생성하는 데 있어 그 객체를 구성하는 부분 부분을 생성한 후 그것을 조합해도 객체의 생성이 가능할 때
서로 다른 표현 방식을 가지는 객체를 동일한 방식으로 생성하고 싶을 때
Builder 패턴의 장단점
객체의 내부 구조를 변경시킬 필요가 있을 경우 기존 소스코드에는 영향을 주지 않고, 새로운 하위 클래스를 추가 정의 할 수 있다.
Builder와 Director 클래스는 서로 독립적으로 수정되거나 재사용될 수 있다.
객체 생성 과정을 좀더 세밀하게 제어할 수 있다.
새로운 종류의 객체 생성을 추가하기는 쉬우나, 객체를 구성하는 각 부분들을 새롭게 추가하기는 어렵다.
구현 관련 사항
설계시 모든 하위 클래스들이 동일한 인터페이스를 가질 수 있도록 만든다.
Builder 패턴에 의해 생성되는 객체들은 서로 어떤 관계도 가질 필요가 없다
⇒ 하위 클래스를 정의하는 이유가 서로 다른 종류의 객체를 생성하기 위함.
최상의 클래스가 반드시 Abstract Base Class로 정의될 필요가 없다.
예) Translator 클래스에서 각 멤버 함수에 대해 기본적인 구현을 해준다면,
그 하위 클래스들이 멤버함수를 구현할 필요성이 줄어든다.
The factory method pattern is an object-oriented design pattern. Like other creational patterns,
it deals with the problem of creating objects (products) without specifying the exact class of object that will be created
이 세가지 패턴의 공통점은 객체를 실제로 생성하는 부분과 사용하는 부분을 따로 둠으로써
객체를 실제로 사용하는 Client 들이 쉽게 복잡한 객체를 사용 할 수 있게 해준다.
(Abstract Factory와 경쟁관계인 패턴이다 왜일까도 알아보자.)
Prototype 패턴은 실행 시간(Run-Timr)에도 생성할 객체의 종류를 추가 또는 삭제할 수 있다.
(단) Clone( ) 멤버 함수를 구현해야 한다.
'Java > DesignPattern' 카테고리의 다른 글
| inheritance and composition on Java (0) | 2011/02/08 |
|---|---|
| 행동패턴 - 책임 연쇄 (chain of responsibility) (0) | 2010/11/25 |
| Creational Pattern (0) | 2009/10/15 |
| Fundamental Design Patterns (0) | 2009/08/19 |
트랙백
댓글
글
Java/DesignPattern 2009/08/19 00:05Fundamental Design Patterns
- 이 것은 프로그램이 거대해지고 복잡해질 수록 효과를 발휘할 것이다.
- 또한 동적으로 객체의 행위를 변경하기 용이할 것이다.
- 또한 프로그램이 fron-end가 아닌 framework이고 API interface를 제공하는 입장이라면 Delegation은 단순히 효과 적인 기법이 아닌 확장 (테스팅 포함)을 위한 필수 조건일 수도 있다.
void f();
void g();
}
class A implements I {
public void f() { System.out.println("A: doing f()"); }
public void g() { System.out.println("A: doing g()"); }
}
class B implements I {
public void f() { System.out.println("B: doing f()"); }
public void g() { System.out.println("B: doing g()"); }
}
class C implements I {
// delegation
I i = new A();
public void f() { i.f(); }
public void g() { i.g(); }
// normal attributes
void toA() { i = new A(); }
void toB() { i = new B(); }
}
public class Main {
public static void main(String[] args) {
C c = new C();
c.f(); // output: A: doing f()
c.g(); // output: A: doing g()
c.toB();
c.f(); // output: B: doing f()
c.g(); // output: B: doing g()
}
}
class Cart {
private final List items;
public Cart(List items) { this.items = items; }
public List getItems() { return items; }
public int total() { /* return sum of the prices */ }
}
class ImmutableCart {
private final List items;
public ImmutableCart(List items) {
this.items = Collections.unmodifiableList(new ArrayList(items));
}
public List getItems() {
return items;
}
public int total() { /* return sum of the prices */ }
}
출처 : http://en.wikipedia.org/wiki/Immutable_pattern 위키피디아
In java language programming, interfaces with no methods are known as marker interfaces. Marker interfaces are Serializable, Clonable, SingleThreadModel, Event listener. Marker Interfaces are implemented by the classes or their super classes in order to add some functionality.
e.g. Suppose you want to persist (save) the state of an object then you have to implement the Serializable interface otherwise the compiler will throw an error. To make more clearly understand the concept of marker interface you should go through one more example.
Suppose the interface Clonable is neither implemented by a class named Myclass nor it's any super class, then a call to the method clone() on Myclass's object will give an error. This means, to add this functionality one should implement the Clonable interface. While the Clonable is an empty interface but it provides an important functionality.

import java.util.*;
interface Image {
public void displayImage();
}
//on System A
class RealImage implements Image {
private String filename;
public RealImage(String filename) {
this.filename = filename;
loadImageFromDisk();
}
private void loadImageFromDisk() {
System.out.println("Loading " + filename);
}
public void displayImage() {
System.out.println("Displaying " + filename);
}
}
//on System B
class ProxyImage implements Image {
private String filename;
private Image image;
public ProxyImage(String filename) {
this.filename = filename;
}
public void displayImage() {
image = new RealImage(filename);
image.displayImage();
}
}
class ProxyExample {
public static void main(String[] args) {
Image image1 = new ProxyImage("HiRes_10MB_Photo1");
Image image2 = new ProxyImage("HiRes_10MB_Photo2");
image1.displayImage(); // loading necessary
image2.displayImage(); // loading necessary
}
}
출처 : http://en.wikipedia.org/wiki/Proxy_pattern 위키피디아
>>Proxy패턴은 Decorator패턴과 유사하여 혼동되기도 합니다.
하지만 두가지 패턴은 서로 다른 목적을 가진 패턴들입니다.
*Proxy패턴은 기존에 구현되어있는 기능(객체)에 보다 효율적으로 접근, 원격 제어, 접근 권한 제어하는것 등이 목적입니다.
*Decorator패턴은 구현되어 있는 기능에 새로운 기능의 추가를 쉽게하는 것이 주요 목적입니다.
'Java > DesignPattern' 카테고리의 다른 글
| inheritance and composition on Java (0) | 2011/02/08 |
|---|---|
| 행동패턴 - 책임 연쇄 (chain of responsibility) (0) | 2010/11/25 |
| Creational Pattern (0) | 2009/10/15 |
| Fundamental Design Patterns (0) | 2009/08/19 |
RECENT COMMENT