Skip to content

Instantly share code, notes, and snippets.

@gemmadlou
Last active June 29, 2017 15:39
Show Gist options
  • Select an option

  • Save gemmadlou/0fc430122edfced6b6a988e0f51adec0 to your computer and use it in GitHub Desktop.

Select an option

Save gemmadlou/0fc430122edfced6b6a988e0f51adec0 to your computer and use it in GitHub Desktop.
The Builder Pattern

The Builder Pattern

Why

  • You want encapsulation
  • You have too many parameters for your class constructor.
  • You want immutability

First Attempt

Add everything to the constuctor.

Benefits

We've got encapsulation.

We can validate everything once

We've got immutability

Problem

Too many parameters! You'll get lost creating the instance in the first place.

See example

Second Attempt

Create getters and setters.

### Benefit

We get help from intellisense.

Problem

But now we've lost encapsulation. We can include a couple of parameters and completely ignore the other parameters in our object.

And now we are no longer able to ensure the state of our object. It can be anything.

See example

Third Attempt - Builder Pattern

Make another class which interacts with our object, and through that object only, build the class that we need.

See below for example:

https://github.com/gemmadlou/Builder-Class-Example-PHP

<?php
class User {
private $name;
private $surname;
private $middlenames = [];
private $age;
private $gender;
private $mothersName;
private $fathersName;
private $phoneNumber;
private $mobileNumber;
private $emailAddress;
public function __construct($name, $surname, array $middlenames, $age, $gender, $mothersName, $fathersName, $phoneNumber, $mobileNumber, $emailAddress) {
$this->name = $name;
$this->surname = $surname;
$this->middlenames = $middlenames;
$this->age = $age;
$this->gender = $gender;
$this->mothersName = $mothersName;
$this->fathersName = $fathersName;
$this->phoneNumber = $phoneNumber;
$this->mobileNumber = $mobileNumber;
$this->emailAddress = $emailAddress;
}
public function getName() {
return $this->name;
}
public function getSurname() {
return $this->surname;
}
public function getMiddleNames() {
return $this->middlenames;
}
// etc
}
$user = new User(
'Brad',
'Pitt',
[
'William'
],
57,
'male',
'Gloria',
'William',
'+1 03328 343 236',
'+55 2311 338 274',
'willbradleypitt@gmail.com'
);
<?php
class User
{
private $name;
private $surname;
private $middlenames = [];
private $age;
private $gender;
private $mothersName;
private $fathersName;
private $phoneNumber;
private $mobileNumber;
private $emailAddress;
public function setName($name)
{
$this->name = $name;
}
public function setSurname($surname)
{
$this->surname = $surname;
}
public function setMiddleNames(array $names)
{
$this->middleNames = $names;
}
public function getName()
{
return $this->name;
}
public function getSurname()
{
return $this->surname;
}
public function getMiddleNames()
{
return $this->middlenames;
}
// etc
}
$user = new User();
$user->setName('Brad');
$user->setSurname('Pitt');
$user->setMiddleNames(['William']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment