-ترکیب یه مفهوم پایه تو OOP به حساب میاد که این امکانو به ما میده که آبجکت هامون رو از یه آبجکت دیگه بسازیم. و خصوصیات و ویژگی های هردورو باهم ترکیب کنیم
ترکیب در اصل یه نوعی از ارتباط بین آبجکت هاس که توی اون یه آبجکت که آبجکت صاحب هست، تمام خصوصیات و ویژگی های یک یا چند آبجکت دیگه رو توی خودش جا میده، یعنی آبجکت صاحب از بقیه آبجکت های جزء به عنوان اجزاء خودش استفاده میکنه
یه مثال ازش ببینیم:
class VendingMachine {
constructor(products, coins) {
this.products = products;
this.coins = coins;
}
selectProduct(productCode) {
const product = this.products.find(p => p.code === productCode);
if (!product) {
throw new Error("Invalid product code");
}
return product;
}
insertCoin(coinValue) {
this.coins.push(coinValue);
}
returnCoins() {
const totalCoins = this.coins.reduce((sum, coin) => sum + coin, 0);
this.coins = [];
return totalCoins;
}
dispenseProduct(productCode) {
const product = this.selectProduct(productCode);
const price = product.price;
const totalCoins = this.coins.reduce((sum, coin) => sum + coin, 0);
if (totalCoins < price) {
throw new Error("Insufficient funds");
}
const change = totalCoins - price;
this.coins = [];
console.log(`Here is your ${product.name}.`);
if (change > 0) {
console.log(`Your change is ${change} coins.`);
}
}
}
class Product {
constructor(code, name, price) {
this.code = code;
this.name = name;
this.price = price;
}
}
class Coin {
constructor(value) {
this.value = value;
}
}
// Create products and coins
const products = [
new Product("A1", "Snickers", 100),
new Product("B2", "Coke", 80),
new Product("C3", "Chips", 60),
];
const coins = [
new Coin(50),
new Coin(20),
new Coin(10),
];
// Create vending machine
const vendingMachine = new VendingMachine(products, coins);
// Simulate usage
vendingMachine.insertCoin(50);
vendingMachine.insertCoin(50);
vendingMachine.selectProduct("B2");
vendingMachine.dispenseProduct("B2");
console.log("Remaining coins:", vendingMachine.returnCoins());
-یک واسط در OOP مجموعهای از متدهارو تعریف میکنه که یه کلاس یا شیء باید اون مجموعه رو پیاده سازی کنه. واسط ها این قابلیت رو به ما میدن که بدون نیاز به وراثت، بین آبجکت هامون رفتار های مشابه ایجاد کنیم، دقت کنین توی زبان های مختلف ممکنه مفهومش متفاوت باشه، مثلا ما توی Javascript اگه بخایم از واسط ها استفاده کنیم باید بریم Typescript و اینترفیس تعریف کنیم که توی مثال باهم میبینیم
مثال استفاده از واسط ها:
interface Shape {
draw();
getArea();
}
class Circle implements Shape {
constructor(radius) {
this.radius = radius;
}
draw() {
console.log("Drawing a circle with radius", this.radius);
}
getArea() {
return Math.PI * this.radius * this.radius;
}
}
class Square implements Shape {
constructor(sideLength) {
this.sideLength = sideLength;
}
draw() {
console.log("Drawing a square with side length", this.sideLength);
}
getArea() {
return this.sideLength * this.sideLength;
}
}
// Create shapes
const circle = new Circle(5);
const square = new Square(10);
// Draw and calculate area
circle.draw();
console.log("Circle area:", circle.getArea());
square.draw();
console.log("Square area:", square.getArea());
#oop #oop_8
Please open Telegram to view this post
VIEW IN TELEGRAM
🔥11⚡5❤🔥2