'specification pattern'에 해당되는 글 2건

  1. sjava-config 1.3 2009/10/23
  2. Specification Pattern (2) 2009/09/16

sjava-config 1.3

from My Project 2009/10/23 17:30
ConfigHandler의 생성자 부분에 대한 변경이 있었습니다.
그리고, getValue의 조건체크를 Specification Pattern을 통해서 적용하였습니다.
사용법은 http://www.sjava.net/121를 참고하시면 됩니다.

바이너리 & 소스



Specification Pattern

from Ooad 2009/09/16 15:24

위키피디아에 한 마디로 "Recombinable business logic in a boolean fashion" 이라고 나와 있습니다.

위키피디아의 한 마디는, 비지니스 로직 처리를 하기 위해서 if~else를 보통 많이 쓰는데, 그 if~else를 제거하고 비지니스 로직을 확장에 쉽게 Composition하는 패턴이라고 설명을 하는 것 이겠지요??
그리고, 로직이 다른 클래스가 하위 클래스로 점진적으로 추가 된다면, Strategy Pattern과 유사한 형태가 될 것 같습니다. 하지만, Strategy Pattern은 데이타에 대한 처리나 체크를 Concrete Strategy에서 하고 Specification Pattern은 모델의 Provider 클래스가 처리를 대행하는 형태라는게 다른 것 같습니다.

아래 링크도 참고하시면 좋을 듯 합니다.
http://en.wikipedia.org/wiki/Specification_pattern#C.23
http://decoder.tistory.com/877

아래는 예제 코드 입니다...

package net.sjava.patterns.specification;

/**
 *
 * @author mcsong@gmail.com
 * @since 2009. 9. 16.
 */
public class User {
 /**
  * name
  */
 private String name;
 
 /**
  * city
  */
 private String city;
 /**
  * address
  */
 private String address;
 /**
  * company
  */
 private String companyName;
 
 /**
  * @return the name
  */
 public String getName() {
  return name;
 }
 /**
  * @param name the name to set
  */
 public void setName(String name) {
  this.name = name;
 }
 /**
  * @return the city
  */
 public String getCity() {
  return city;
 }
 /**
  * @param city the city to set
  */
 public void setCity(String city) {
  this.city = city;
 }
 /**
  * @return the address
  */
 public String getAddress() {
  return address;
 }
 /**
  * @param address the address to set
  */
 public void setAddress(String address) {
  this.address = address;
 }
 /**
  * @return the company
  */
 public String getCompanyName() {
  return companyName;
 }
 /**
  * @param company the company to set
  */
 public void setCompanyName(String company) {
  this.companyName = company;
 }
}

 

package net.sjava.patterns.specification;

/**
 *  
 * @author mcsong@gmail.com
 * @since 2009. 9. 16.
 */
public interface IUserSpecification {
 /**
  *
  * @param user
  * @return
  */
 public boolean isSatisfied(User user);
}

 

package net.sjava.patterns.specification;

/**
 *
 * @author mcsong@gmail.com
 * @since 2009. 9. 16.
 */
public abstract class AbstractUserSpecification implements IUserSpecification {
 
 @Override
 public boolean isSatisfied(User member) {
  // TODO Auto-generated method stub
  return true;
 }
}

 

package net.sjava.patterns.specification;

/**
 *
 * @author mcsong@gmail.com
 * @since 2009. 9. 16.
 */
public class CompanySpecification extends AbstractUserSpecification {

 /**
  *
  */
    private String companyName;
   
    /**
     *
     * @param companyName
     */
    public CompanySpecification(String companyName)
    {
        this.companyName = companyName;
    }
 
 @Override
 public boolean isSatisfied(User user) {
  // TODO Auto-generated method stub
  if(user == null)
   return false;
  
  return user.getCompanyName().equals(this.companyName);
 }
}

 

package net.sjava.patterns.specification;

import java.util.ArrayList;

/**
 *
 * @author mcsong@gmail.com
 * @since 2009. 9. 16.
 */
public class UserProvider {
 
 /**
  *
  */
 private static ArrayList<User> users = new ArrayList<User>();
 
 /**
  *
  */
 private static java.util.concurrent.atomic.AtomicBoolean isLoaded = null;
 
 
 /**
  *
  */
 private UserProvider() {
  if(isLoaded == null) {
   isLoaded = new java.util.concurrent.atomic.AtomicBoolean();
   UserProvider.isLoaded.set(true);
   UserProvider.provideUsers();
    
  }
 }

 /**
  *
  * @return
  */
 public static UserProvider provider() {
  return new UserProvider();  
 }
 
 /**
  * dummy data 입력
  */
 public static void provideUsers() {
  
  for(int i=0; i < 10; i++) {
   User user = new User();
   user.setName(String.valueOf(i));
   user.setCity("서울");
   user.setAddress(String.valueOf(i) + " 번지");
   if(i < 5)
    user.setCompanyName("회사");
   else
    user.setCompanyName("백수");
   
   users.add(user);
  }
 }
 
 /**
  *
  * @param user
  * @return
  */
 public ArrayList<User> getSpecificationList(IUserSpecification specification) {
  
  ArrayList<User> list = new ArrayList<User>();
 
  for(int i=0; i < users.size(); i++) {
   if(specification.isSatisfied(users.get(i)))
    list.add(users.get(i));
  }
        return list;
    }

}

 

import java.util.ArrayList;
import net.sjava.patterns.specification.User;
import net.sjava.patterns.specification.UserProvider;
import net.sjava.patterns.specification.IUserSpecification;
import net.sjava.patterns.specification.CompanySpecification;


public class SpecificationPatternTest {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  ArrayList<User> users = null;
  IUserSpecification spec = null;
  
  spec = new CompanySpecification("회사");
  users = UserProvider.provider().getSpecificationList(spec);
  
  for(int i=0; i < users.size(); i++) {
   System.out.println(i + users.get(i).getCompanyName());
  }
  
  spec = new CompanySpecification("백수");
  users = UserProvider.provider().getSpecificationList(spec);
  
  for(int i=0; i < users.size(); i++) {
   System.out.println(i + users.get(i).getCompanyName());
  }
 }
}

'Ooad' 카테고리의 다른 글

Singleton Pattern  (0) 2010/04/29
Null Object Pattern  (2) 2009/09/17
Specification Pattern  (2) 2009/09/16
디자인 패턴 그리고 그 이후..  (0) 2009/09/16
OOP에 대한 내용들..  (0) 2009/07/06