Prototype Design Pattern

1. 위젯 테마/스타일 복제

dart

class ButtonStyle implements Cloneable<ButtonStyle> {
  final Color backgroundColor;
  final Color textColor;
  final double borderRadius;
  final EdgeInsets padding;
  
  ButtonStyle({
    required this.backgroundColor,
    required this.textColor,
    required this.borderRadius,
    required this.padding,
  });
  
  @override
  ButtonStyle clone() {
    return ButtonStyle(
      backgroundColor: backgroundColor,
      textColor: textColor,
      borderRadius: borderRadius,
      padding: padding,
    );
  }
  
  // 특정 속성만 변경하는 메서드
  ButtonStyle copyWith({
    Color? backgroundColor,
    Color? textColor,
    double? borderRadius,
    EdgeInsets? padding,
  }) {
    return ButtonStyle(
      backgroundColor: backgroundColor ?? this.backgroundColor,
      textColor: textColor ?? this.textColor,
      borderRadius: borderRadius ?? this.borderRadius,
      padding: padding ?? this.padding,
    );
  }
}

// 사용법
ButtonStyle primaryStyle = ButtonStyle(
  backgroundColor: Colors.blue,
  textColor: Colors.white,
  borderRadius: 8.0,
  padding: EdgeInsets.all(16),
);

// 기본 스타일을 복제해서 변형
ButtonStyle secondaryStyle = primaryStyle.copyWith(
  backgroundColor: Colors.grey,
);
ButtonStyle dangerStyle = primaryStyle.copyWith(
  backgroundColor: Colors.red,
);

2. 게임 아이템/캐릭터 복제

dart

3. 복잡한 UI 설정 복제

dart

4. 폼 필드 설정 복제

dart

플러터에서 언제 사용하면 좋은가?

  1. 테마/스타일 시스템: 기본 스타일을 만들고 변형된 버전들을 생성

  2. 게임 오브젝트: 같은 타입의 캐릭터/아이템을 대량 생성

  3. 폼 설정: 비슷한 입력 필드들의 설정을 재사용

  4. 설정/구성 객체: 복잡한 설정을 복사해서 일부만 수정

  5. 위젯 빌더: 비슷한 위젯들을 템플릿 기반으로 생성

Last updated