I would like to see how is Builder pattern used in real world applications/APIs. The examples I found are all pizzas, cakes, cars et cetera (plus the parser example from the GoF
Actually a very good real-life example is Active Record QueryBuilder example.
You can dig into the Laravel Eloquent module and check those Query builder classes...
A quick example of how it could look:
interface SqlQueryBuilder
{
public function select(string $table, array $fields): SqlQueryBuilder;
public function where(string $field, string $value, string $operator = '='): SqlQueryBuilder;
public function limit(int $start, int $offset): SqlQueryBuilder;
// ... other methods
public function getSQL(): string;
}
class MysqlQueryBuilder implements SqlQueryBuilder
{
// ...
}
class PostgresQueryBuilder extends MysqlQueryBuilder
{
// ...
}