Skip to content

Commit

Permalink
creational Patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
alpgul committed May 29, 2023
1 parent dbe46d5 commit 87e7ed7
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 0 deletions.
96 changes: 96 additions & 0 deletions creational/abstract-factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Soyut fabrika arayüzü
class ArabaParcaFabrikasi {
olusturLastik() {}
olusturDireksiyon() {}
}

// Alt fabrika sınıfları
class AudiParcaFabrikasi extends ArabaParcaFabrikasi {
olusturLastik() {
return new AudiLastik();
}
olusturDireksiyon() {
return new AudiDireksiyon();
}
}

class BMWParcaFabrikasi extends ArabaParcaFabrikasi {
olusturLastik() {
return new BMWLastik();
}
olusturDireksiyon() {
return new BMWDireksiyon();
}
}

// Üst sınıf - Lastik
class Lastik {
constructor() {}
}

// Alt sınıflar - AudiLastik ve BMWLastik
class AudiLastik extends Lastik {
constructor() {
super();
console.log("Audi lastik üretildi.");
}
}

class BMWLastik extends Lastik {
constructor() {
super();
console.log("BMW lastik üretildi.");
}
}

// Üst sınıf - Direksiyon
class Direksiyon {
constructor() {}
}

// Alt sınıflar - AudiDireksiyon ve BMWDireksiyon
class AudiDireksiyon extends Direksiyon {
constructor() {
super();
console.log("Audi direksiyon üretildi.");
}
}

class BMWDireksiyon extends Direksiyon {
constructor() {
super();
console.log("BMW direksiyon üretildi.");
}
}

// İstemci
class ArabaParcaUretim {
constructor(fabrika) {
this.fabrika = fabrika;
}

lastikUret() {
const lastik = this.fabrika.olusturLastik();
// Lastik üzerinde işlemler yapabilir veya döndürebilir
return lastik;
}

direksiyonUret() {
const direksiyon = this.fabrika.olusturDireksiyon();
// Direksiyon üzerinde işlemler yapabilir veya döndürebilir
return direksiyon;
}
}

// İstemci tarafından fabrikanın belirlenmesi
const audiFabrika = new AudiParcaFabrikasi();
const audiParcaUretim = new ArabaParcaUretim(audiFabrika);

const audiLastik = audiParcaUretim.lastikUret(); // Audi lastik üretildi.
const audiDireksiyon = audiParcaUretim.direksiyonUret(); // Audi direksiyon üretildi.

const bmwFabrika = new BMWParcaFabrikasi();
const bmwParcaUretim = new ArabaParcaUretim(bmwFabrika);

const bmwLastik = bmwParcaUretim.lastikUret(); // BMW lastik üretildi.
const bmwDireksiyon = bmwParcaUretim.direksiyonUret(); // BMW direksiyon üret
94 changes: 94 additions & 0 deletions creational/builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
class Url {
constructor (protocol, username, password, hostname,
port, pathname, search, hash) {
this.protocol = protocol
this.username = username
this.password = password
this.hostname = hostname
this.port = port
this.pathname = pathname
this.search = search
this.hash = hash

this.validate()
}

validate () {
if (!this.protocol || !this.hostname) {
throw new Error('Must specify at least a ' +
'protocol and a hostname')
}
}

toString () {
let url = ''
url += `${this.protocol}://`
if (this.username && this.password) {
url += `${this.username}:${this.password}@`
}
url += this.hostname
if (this.port) {
url += this.port
}
if (this.pathname) {
url += this.pathname
}
if (this.search) {
url += `?${this.search}`
}
if (this.hash) {
url += `#${this.hash}`
}
return url
}
}
class UrlBuilder {
setProtocol (protocol) {
this.protocol = protocol
return this
}

setAuthentication (username, password) {
this.username = username
this.password = password
return this
}

setHostname (hostname) {
this.hostname = hostname
return this
}

setPort (port) {
this.port = port
return this
}

setPathname (pathname) {
this.pathname = pathname
return this
}

setSearch (search) {
this.search = search
return this
}

setHash (hash) {
this.hash = hash
return this
}

build () {
return new Url(this.protocol, this.username, this.password,
this.hostname, this.port, this.pathname, this.search,
this.hash)
}
}
const url = new UrlBuilder()
.setProtocol('https')
.setAuthentication('user', 'pass')
.setHostname('example.com')
.build()

console.log(url.toString())
43 changes: 43 additions & 0 deletions creational/factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Araba {
constructor(marka, model) {
this.marka = marka;
this.model = model;
}

tanit() {
console.log(`Bu araba ${this.marka} marka ve modeli ${this.model}.`);
}
}

// Alt sınıflar - SedanAraba ve SUVAraba
class SedanAraba extends Araba {
constructor(marka, model) {
super(marka, model);
}
}

class SUVAraba extends Araba {
constructor(marka, model) {
super(marka, model);
}
}

// Fabrika sınıfı
class ArabaFabrikasi {
static arabaOlustur(tur, marka, model) {
if (tur === "sedan") {
return new SedanAraba(marka, model);
} else if (tur === "suv") {
return new SUVAraba(marka, model);
} else {
throw new Error("Geçersiz araba türü.");
}
}
}

// İstemci tarafından alt sınıfın belirlenmesi
const sedanAraba = ArabaFabrikasi.arabaOlustur("sedan", "X Marka", "Model A");
sedanAraba.tanit(); // Bu araba X Marka marka ve modeli Model A.

const suvAraba = ArabaFabrikasi.arabaOlustur("suv", "Y Marka", "Model B");
suvAraba.tanit(); // Bu araba Y Marka marka ve modeli Model B.
29 changes: 29 additions & 0 deletions creational/prototype.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Üst sınıf - Araba
class Araba {
constructor(marka, model) {
this.marka = marka;
this.model = model;
}

tanit() {
console.log(`Bu araba ${this.marka} marka ve modeli ${this.model}.`);
}

clone() {
// Prototip nesnesinin kopyasını oluşturmak için Object.create kullanılır
return Object.create(this);
}
}

// İstemci tarafından oluşturulan bir Araba prototipi
const arabaPrototipi = new Araba("X Marka", "Model A");

// İstemci, prototip nesnesinden kopyalar oluşturabilir
const araba1 = arabaPrototipi.clone();
araba1.tanit(); // Bu araba X Marka marka ve modeli Model A.

const araba2 = arabaPrototipi.clone();
araba2.marka = "Y Marka";
araba2.model = "Model B";
araba2.tanit(); // Bu araba Y Marka marka ve modeli Model B.
console.log(araba1 == arabaPrototipi);
26 changes: 26 additions & 0 deletions creational/singleton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Singleton sınıfı - Araba
class Araba {
constructor(marka, model) {
if (Araba.instance) {
return Araba.instance;
}

this.marka = marka;
this.model = model;

Araba.instance = this;
}

tanit() {
console.log(`Bu araba ${this.marka} marka ve ${this.model} model.`);
}
}

// İstemci bir Araba örneği oluşturur
const araba1 = new Araba("X Marka", "Model A");
araba1.tanit(); // Bu araba X Marka marka ve Model A model.

// Başka bir örneği oluşturmayı denemek aynı örneği döndürecek
const araba2 = new Araba("Y Marka", "Model B");
araba2.tanit(); // Bu araba X Marka marka ve Model A model.
console.log(araba1 === araba2); // true

0 comments on commit 87e7ed7

Please sign in to comment.