Class Design Template

This template helps you design well-structured, maintainable classes following object-oriented design principles. It covers encapsulation, inheritance, composition, and design patterns for robust software architecture.

Sample Prompt

Design a User class for an e-commerce application:
- Properties: id, email, profile, preferences, order history
- Methods: authentication, profile updates, password management
- Inheritance: extend from base Person class
- Composition: include Address and PaymentMethod objects
- Design patterns: implement Observer pattern for notifications
- Validation and error handling for all operations

What This Template Covers

  • Object-oriented design principles including encapsulation, inheritance, and polymorphism
  • Design pattern implementation for common software architecture problems
  • Class relationships using composition, aggregation, and inheritance appropriately
  • Method design with proper parameter handling and return types
  • Data validation and error handling within class methods
  • Testing considerations for unit testing and mock object creation

Best Practices

  1. Follow single responsibility principle with focused class purposes
  2. Use composition over inheritance when appropriate for flexibility
  3. Implement proper encapsulation with private/protected access modifiers
  4. Design for testability with dependency injection and clear interfaces
  5. Handle errors gracefully with appropriate exception handling
  6. Document class contracts with clear method signatures and behavior
  7. Consider thread safety for classes used in concurrent environments
  8. Apply design patterns judiciously without over-engineering

When to Use This Template

  • Designing new classes and object hierarchies
  • Refactoring existing code to improve structure
  • Implementing design patterns in software architecture
  • Creating reusable components and libraries
  • Teaching object-oriented programming concepts

The Script

peep class-design