티스토리 뷰

Programming/DDD

Specification Pattern

Albothyl 2019. 7. 14. 20:28

복잡한 Domain Logic을 좀 더 이해하기 쉽게 "AND", "OR", "NOT" 의 논리 연산으로 추상화 한다.

복잡한 Logic을 하나 하나 풀어 각각의 Specification으로 정의하고, 각 Specification을 위 "AND", "OR", "NOT" 논리연산으로 연결한다.

public interface Specification {
	Specification and(Specification other);
	Specification or(Specification other);
	Specification not();
	boolean isSatisfiedBy(Object candidate);
}
public abstract class AbstractSpecification implements Specification { 
	public Specification and(Specification other) { 
		return new AndSpecification(this, other); 
	}

	public Specification or(Specification other) { 
		return new OrSpecification(this, other); 
	}

	public Specification not() { 
		return new NotSpecification (this); 
	}
}
public class AndSpecification extends AbstractSpecification { 
	private Specification some; 
	private Specification other; 

	public AndSpecification(Specification some, Specification other) { 
		some = some; 
		other = other; 
	}

	public boolean isSatisfiedBy(Object candidate) { 
		return some.isSatisfiedBy(candidate) && other.isSatisfiedBy(candidate); 
	}
}
public class OrSpecif ication extends AbstractSpecif ication { 
	private Specification some; 
	private Specification other; 

	public OrSpecification(Specification some, Specification other) { 
		some = some; 
		other = other; 
	}

	public boolean isSatisfiedBy(Object candidate) { 
		return some.isSatisfiedBy(candidate) || other.isSatisfiedBy(candidate); 
	}
}
public class NotSpecification extends AbstractSpecification { 
	private Specification wrapped; 

	public NotSpecification(Specification some) { 
		wrapped = some; 
	}

	public boolean isSatisfiedBy(Object candidate) { 
		return !wrapped.isSatisfiedBy(candidate); 
	}
}

java에서 predicate로 이미 구현되어 있더라...

'Programming > DDD' 카테고리의 다른 글

Bounded Context  (0) 2019.08.03
분석 패턴  (0) 2019.08.03
유연한 설계  (0) 2019.08.03
Layered Architecture  (1) 2019.08.03
DDD 구성요소  (0) 2019.08.03
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함