The Core Foundation package simplifies the implementation of the factory design pattern, enabling you to create flexible, maintainable, and scalable service classes effortlessly. Here's how you can leverage this feature to enhance your application's architecture.
Native Factory Design Pattern Support
The package provides native support for the factory design pattern. To use it, extend the CoreFoundation\Services\BaseService class in your service class.
You can define conditions for selecting the appropriate factory class using either a closure method or a custom method.
Closure method
This method sets a global condition for choosing the factory class.
class AppServiceProvider extends ServiceProvider
{
public function register()
{
UserService::setFactoryCondition(CustomUserService::class, function () {
return app()->isProduction(); // return true/false
});
}
}
Custom method
This method allows you to add dynamic conditions based on your business logic.
class AppServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton("custom.user.service", CustomUserConditionService::class); // binds "custom.user.service" key to service container.
UserService::setFactoryCondition(CustomUserService::class, "custom.user.service");
}
}
class CustomUserConditionService implements \CoreFoundation\Contracts\BaseFactoryConditionInterface
{
public function __construct(
//
) {
}
public function handle(): bool
{
// your custom logic
return true;
}
}