Skip to content

Instantly share code, notes, and snippets.

View resume.json
{
"meta": { "theme": "classy" },
"basics": {
"name": "Arıl Bozoluk",
"label": "Software Engineering Manager",
"picture": "https://media.licdn.com/dms/image/C5603AQEuhtGb4uXoQg/profile-displayphoto-shrink_200_200/0?e=1572480000&v=beta&t=h8JHbAznqLKsr2XK9FTM6FO6pcv2zm71ADcoorW9g7o",
"email": "arilbozoluk@hotmail.com",
"phone": "(546) 223 17 23",
"website": "",
"location": {
View singleton.md

To create a singleton, make the constructor private, disable cloning, disable extension and create a static variable to house the instance

final class President {
    private static $instance;

    private function __construct() {
        // Hide the constructor
    }
    
    public static function getInstance() : President {
View prototype.md

In PHP, it can be easily done using clone

class Sheep {
    protected $name;
    protected $category;

    public function __construct(string $name, string $category = 'Mountain Sheep') {
        $this->name = $name;
        $this->category = $category;
View builder.md

Having said that let me add a bit about what telescoping constructor anti-pattern is. At one point or the other we have all seen a constructor like below:

public function __construct($size, $cheese = true, $pepperoni = true, $tomato = false, $lettuce = true) {
}

As you can see; the number of constructor parameters can quickly get out of hand and it might become difficult to understand the arrangement of parameters. Plus this parameter list could keep on growing if you would want to add more options in future. This is called telescoping constructor anti-pattern.

Programmatic Example

View abstract-factory.md

Translating the door example above. First of all we have our Door interface and some implementation for it

interface Door {
    public function getDescription();
}

class WoodenDoor implements Door {
    public function getDescription() {
        echo 'I am a wooden door';
View factory-method.md

Taking our hiring manager example above. First of all we have an interviewer interface and some implementations for it

interface Interviewer {
    public function askQuestions();
}

class Developer implements Interviewer {
    public function askQuestions() {
        echo 'Asking about design patterns!';
View simple-factory.md

Programmatic Example

First of all we have a door interface and the implementation

interface Door {
    public function getWidth() : float;
    public function getHeight() : float;
}

class WoodenDoor implements Door {
View extended-list.py
from collections import MutableSequence
import marshal
from operator import add, div, mul, neg, mod, sub
from operator import ge, gt, le, lt, eq
from itertools import imap
'''
Extended list class
- fast lookups by maintaining a dictionary with the values
- numeric operations between 2 instances are possible (like Octave matrices)
View csharpdeneme
using System;
List<string> myStrings = new List<string> { "ali", "veli", "ayşe" };
foreach(var myString in myStrings)
{
Console.Writeline(mystring);
}
You can’t perform that action at this time.