Tag Archives: Factory Method

Factory Method Pattern in JavaScript

Written by Garry Lachman (Admin). Filed under JavaScript. Tagged , , , . .

From Wikipedia: “The factory method pattern is an object-oriented design pattern to implement the concept of factories. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The creation of an object often requires complex processes not appropriate to include within a composing object. The object’s creation may lead to a significant duplication of code, may require information not accessible to the composing object, may not provide a sufficient level of abstraction, or may otherwise not be part of the composing object’s concerns. The factory method design pattern handles these problems by defining a separate method for creating the objects, which subclasses can then override to specify the derived type of product that will be created.”

2 Classes:

1
2
3
4
5
6
7
8
9
10
11
<script language="javascript" type="text/javascript">
// Class 1
function class_one() {
    this.init = function() {}
}

// Class 1
function class_two() {
    this.init = function() {}
}
</script>

Factory Method:

1
2
3
4
5
6
7
8
9
10
11
12
<script language="javascript" type="text/javascript">
function factory_method(){
}
// now add static function to get the instance
factory_method.get = function(_type){
    if (_type == 1) {
        return new class_one();
    } else {
        return new class_two();
    }
}
</script>

Usage:

1
2
3
4
<script language="javascript" type="text/javascript">
var _instance = factory_method.get(1);
_instance.init();
</script>

Have fun ;)

Share

Factory Design Pattern in PHP5

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , , , , . .

Factory Method return you the specific object by params he gets.
Lets make an example… we got 2 different classes for students that extends StudentAbstract class,
now we can make factory class that return the class by param.

StudentAbstract.php

1
2
3
4
abstract class StudentAbstract {
    abstract function getFirstName();
    abstract function getLastName();
}

FirstGradeStudent.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
include_once('StudentAbstract.php');

class FirstGradeStudent extends StudentAbstract {
    private $firstName;
    private $lastName;

    private function __construct() {
        $this->firstName = "Garry";
        $this->lastName = "Lachman";
    }

    public function getFirstName()  {
        return $this->firstName;
    }

    public function getLastName()   {
        return $this->lastName;
    }
}

SecondGradeStudent.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
include_once('StudentAbstract.php');

class SecondGradeStudent extends StudentAbstract    {
    private $firstName;
    private $lastName;

    private function __construct() {
        $this->firstName = "Polani";
        $this->lastName = "Almoni";
    }

    public function getFirstName()  {
        return $this->firstName;
    }

    public function getLastName()   {
        return $this->lastName;
    }
}

GetStudentFactory.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
include_once("FirstGradeStudent.php");
include_once("SecondGradeStudent.php");

class GetStudentFactory {
    public function getStudent($grade)  {
        $student = NULL;
        switch ($grade) {
            case "first":
                $student = new FirstGradeStudent();
            break;

            case "second";
                $student = new SecondGradeStudent();
            break;

            default:
                $student = new FirstGradeStudent();
            break;
        }
        return $student;
    }
}

Test.php

1
2
3
4
5
6
7
8
9
include_once("GetStudentFactory.php");

$factory = new GetStudentFactory();

$firstGrade = $factory->getStudent("first");
// return FirstGradeStudent class

$secondGrade = $factory->getStudent("second");
// return SecondGradeStudent class

Have Fun ;)
Garry Lachman

Share