내 안드로이드 프로젝트 구조에서 DDD를 사용하고 있습니다. 일부 도메인에서는 "public class profileBehavior {..}"와 같이이 폴더에 모든 화면 동작을 배치하기 위해 "behavior"라는 새 폴더를 만들어야합니다.
프로젝트 인쇄 : http://tinypic.com/view.php?pic=r1hppi&s=8#.VPDdSlPF_rc
시간이 지남에 따라 재사용을 목표로하는 개선으로 인해 다음과 같은 일반적인 메서드를 사용하여 "genericBehavior"를 만드는 것을 생각하고 있습니다.
public class GenericBehavior {
private static Context context;
private static View view;
public GenericBehavior(Context context) {
context = context;
}
public GenericBehavior(View view) {
view = view;
}
public static GenericBehavior with(final Context context) {
return new GenericBehavior(context);
}
public GenericBehavior where(final View view) {
return new GenericBehavior(view);
}
profileBehavior 클래스에서 with 및 where 메소드를 재사용하여 profileBehavior 컨텍스트 및보기를 설정하고 싶습니다.
처럼:
public class ProfileBehavior extends GenericBehavior {
public ProfileBehavior(Context context) {
super(context);
}
profileFragment에서 다음과 같이 사용하고 싶습니다.
ProfileBehavior
.with(getActivity())
.where(rootView)
.listenAttachOptions()
.doScrollStuff();
유형 매개 변수 및 인터페이스에 대해 읽었지만 그것에 대해 정말 혼란 스러웠습니다. 제 경우에는 행동 방식을 복제하거나 이에 대한 해결책이있는 것이 더 낫습니까?
세 가지 방법 (생각할 수있는)이 있습니다.
where()
및 with()
이 기본 클래스와이를 확장 할 수 있습니다.where()
및 with()
(비헤이비어 "기본"개체) 를 구현하는 개체를 만들고 "비헤이비어"클래스의 다른 인스턴스가 이러한 기본 개체의 인스턴스를 전용 멤버로 보유하도록 할 수 있습니다. 이러한 각 클래스는 모든 where()
및 with()
호출을이 기본 개체에 위임 합니다.# 3 구현의 예 :
class BaseBehavior implement Behavior {
public Behavior with(final Context context) {
return new BaseBehavior(context);
}
public Behavior where(final View view) {
return new BaseBehavior(view);
}
}
class AnotherBehavior implement Behavior {
BaseBehavior base;
AnotherBehavior(BaseBehavior base) {
this.base = base;
}
public Behavior with(final Context context) {
return base.with(context);
}
public Behavior where(final View view) {
return base.with(view);
}
}
일반적으로 구성은 상속보다 선호됩니다 (따라서 Java 8을 사용하지 않는 경우-# 1보다 # 3을 사용하는 것이 좋습니다). 몇 가지 이유를들 수 있습니다. 디버그 및 유지 관리가 더 쉬우므로 코드 덜 분리되고 계층 구조가 단순 해지며 하나의 클래스 만 상속 (확장) 할 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다