← Back to libraryQuestion 298 of 468
🏗️Framework DesignAdvanced

Design Patterns in Frameworks

📌 Definition:

Beyond Page Object, frameworks apply classic design patterns: Factory (create drivers/pages), Singleton (single ConfigReader), Builder/Fluent (construct test data/requests), Strategy (swap wait/browser behavior), and Facade (simplify complex flows).

📖 Detailed Explanation:

FACTORY centralizes object creation — a DriverFactory returns the right WebDriver by config, a PageFactory-style creator returns page objects. SINGLETON ensures one instance of shared resources (ConfigReader, ReportManager) — used carefully, as singletons can hinder parallelism if they hold mutable state. BUILDER/FLUENT constructs complex objects readably (new UserBuilder().withRole('admin').build()) and enables fluent page chaining (loginPage.enterUser(x).enterPass(y).submit()). STRATEGY lets you swap algorithms (different wait or locator strategies). Applying patterns appropriately makes the framework extensible and readable; knowing which pattern solves which problem is a senior interview differentiator.

🔑 Key Points:
  • Factory: create drivers/pages by config
  • Singleton: one ConfigReader/ReportManager (beware mutable state + parallelism)
  • Builder/Fluent: readable test-data + page-method chaining
  • Strategy: swap wait/browser/locator behavior
🌍 Real-World Example:

new UserBuilder().withEmail(unique()).withRole('admin').build() (Builder) creates test users readably with defaults, while a DriverFactory (Factory) returns Chrome/Firefox/Edge based on config — patterns making the framework clean and extensible.

🎯 Scenario-Based Interview Question:

Which design pattern would you use to create the right WebDriver based on a config value, and which for building complex test-data objects?