Factory metod abstrakt turlar yoki interfeyslar orqali mahsulot ob'ektlarini qaytaradigan usullarni yaratish orqali aniqlash mumkin. Bu subklasslarda yaratilgan mahsulot turlarini bekor qilishga imkon beradi.
Kontseptual misol
Ushbu misol Factory Methodining tuzilishini, ya'ni qaysi sinflardan iboratligini, ushbu sinflar qanday rollarni bajarishini va ular bir-biri bilan o'zaro aloqalarini ko'rsatadi.
index.ts: Pattern tuzilishiga misol
/**
* Creator klassi ob'ektni qaytarishi kerak bo'lgan zavod usulini e'lon qiladi
* Sifatli mahsulot. Ijodkor subklasslari odatda buni amalga oshirishni ta'minlaydi
* usul. */
abstract class Creator {
/**
* Shuni esda tutingki, ishlab chiqaruvchi standart factory usulni amalga *oshirishni ham ta'minlaydi.*/
public abstract factoryMethod(): Product;
/**
* Shuni ham ta'kidlash kerakki, nomga qaramasdan, Yaratuvchining asosiy vazifasi mahsulotni ishlab chiqarish emas. Odatda zavod usuli bilan qaytarilgan mahsulot ob'ektlariga asoslangan ba'zi bir asosiy biznes mantig'ini o'z ichiga oladi. Pastki sinflar ushbu biznes mantig'ini bilvosita o'zgartirishi mumkin, zavod usulini qayta belgilaydi va undan boshqa turdagi mahsulotni qaytaradi. */
public someOperation(): string {
// Ob'ektni olish uchun zavod usulini chaqiramiz-mahsulot.
const product = this.factoryMethod();
// Keyinchalik, biz ushbu mahsulot bilan ishlaymiz.
return `Creator: The same creator's code has just worked with ${product.operation()}`;
}
}
/**
* Muayyan ijodkorlar ishlab chiqarilgan mahsulot turini o'zgartirish uchun factory methodini qayta belgilaydilar. */
class ConcreteCreator1 extends Creator {
/**
* Shuni esda tutingki, usulning imzosi hali ham turdan foydalanadi
mavhum mahsulot, aslida ma'lum bir mahsulot usulidan qaytib kelgan bo'lsa-da. Shunday qilib, ishlab chiqaruvchi muayyan mahsulot sinflaridan mustaqil bo'lib qolishi mumkin. */
public factoryMethod(): Product {
return new ConcreteProduct1();
}
}
class ConcreteCreator2 extends Creator {
public factoryMethod(): Product {
return new ConcreteProduct2();
}
}
/**
* Mahsulot interfeysi barcha maxsus mahsulotlarni bajarishi kerak bo'lgan operatsiyalarni e'lon qiladi. */
interface Product {
operation(): string;
}
/**
* Muayyan mahsulotlar mahsulot interfeysining turli xil dasturlarini taqdim etadi.
*/
class ConcreteProduct1 implements Product {
public operation(): string {
return '{Result of the ConcreteProduct1}';
}
}
class ConcreteProduct2 implements Product {
public operation(): string {
return '{Result of the ConcreteProduct2}';
}
}
/**
* Mijoz kodi o'zining asosiy interfeysi orqali bo'lsa-da, muayyan yaratuvchining nusxasi bilan ishlaydi. Mijoz asosiy interfeys orqali yaratuvchisi bilan ishlashni davom ettirsa-da, uni yaratuvchining har qanday kichik sinfiga o'tkazishingiz mumkin. */
function clientCode(creator: Creator) {
// ...
console.log('Client: I\'m not aware of the creator\'s class, but it still works.');
console.log(creator.someOperation());
// ...
}
/**
* Ilova konfiguratsiya yoki atrof-muhitga qarab yaratuvchining turini tanlaydi.
*/
console.log('App: Launched with the ConcreteCreator1.');
clientCode(new ConcreteCreator1());
console.log('');
console.log('App: Launched with the ConcreteCreator2.');
clientCode(new ConcreteCreator2());
Output.txt: Результат выполнения
App: Launched with the ConcreteCreator1.
Client: I'm not aware of the creator's class, but it still works.
Creator: The same creator's code has just worked with {Result of the ConcreteProduct1}
App: Launched with the ConcreteCreator2.
Client: I'm not aware of the creator's class, but it still works.
Creator: The same creator's code has just worked with {Result of the ConcreteProduct2}
Do'stlaringiz bilan baham: |