تاریخ امروز:31 فروردین 1403
Composition در جاوا

Composition در جاوا

در جاوا یک متغیر می‌تواند نوع ساده ‌ای مانند int و double و … داشته باشد و یا اینکه از نوع Composition تعریف گردد. به این معنی که متغیر ما شی از کلاس باشد.

مثال زیر را ببینید:

class test {
    private int data2;
    private test2 data3;
    
    
}

class test2 {
    private int data;

    public test2(int data) {
        this.data = data;
    }

    public int getData() {
        return data;
    }

    public void setData(int data) {
        this.data = data;
    }
}

 

در اینجا

  private test2 data3;

یک Composition می‌باشد.

حالا کاربرد این چی هستش؟

در پست‌های قبلی درباره‌ی وراثت صحبت کردم. یک مثال برای وارثت می‌زنم.

برخی خصلت‌ها برای ماشین‌ها ثابت هستند. مثلا همه‌ی ماشین‌ها یک سرعت ماکزیمم دارن  و یا رنگ دارن.

برخی خصلت‌ها برای هر ماشین خاص هستش. مثلا برخی ماشین‌ها توربو شارژر دارن.

حالا این مثال رو بخوایم پیاده‌سازی بکنیم به این صورت در میادش:

class vehicle{
    private double maxSpeed;
    private String color;

    public vehicle(double maxSpeed, String color) {
        this.maxSpeed = maxSpeed;
        this.color = color;
    }

    public double getMaxSpeed() {
        return maxSpeed;
    }

    public void setMaxSpeed(double maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

class Benz extends vehicle{
    private boolean turbo;

    public Benz(double maxSpeed, String color, boolean turbo) {
        super(maxSpeed, color);
        this.turbo = turbo;
    }
}

این دقیقا مفهوم inheritance بودش.

حالا میام یه مثال دیگه میزنم:

فرض کنید شما یک کامپیوتر دارین. این کامپیوتر  مادربورد، مانیتور، سی‌پی‌یو و … هستش که هر کدوم از این اجزا از جزيیات دیگه‌ای برخوردار هستن.

برای پیاده سازی این موارد از Composition استفاده می‌کنیم.

class Computer{
    private Monitor monitor;
    private Motherboard motherboard;
    private  CPU cpu;

    public Computer(Monitor monitor, Motherboard motherboard, CPU cpu) {
        this.monitor = monitor;
        this.motherboard = motherboard;
        this.cpu = cpu;
    }

    public Monitor getMonitor() {
        return monitor;
    }

    public void setMonitor(Monitor monitor) {
        this.monitor = monitor;
    }

    public Motherboard getMotherboard() {
        return motherboard;
    }

    public void setMotherboard(Motherboard motherboard) {
        this.motherboard = motherboard;
    }

    public CPU getCpu() {
        return cpu;
    }

    public void setCpu(CPU cpu) {
        this.cpu = cpu;
    }
}

class Monitor{
    private String Brand;
    private String Resolution;

    public String getBrand() {
        return Brand;
    }

    public void setBrand(String brand) {
        Brand = brand;
    }

    public String getResolution() {
        return Resolution;
    }

    public void setResolution(String resolution) {
        Resolution = resolution;
    }
}

class Motherboard{
    private String model;
    private int ramSlot;
    private String bios;

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getRamSlot() {
        return ramSlot;
    }

    public void setRamSlot(int ramSlot) {
        this.ramSlot = ramSlot;
    }

    public String getBios() {
        return bios;
    }

    public void setBios(String bios) {
        this.bios = bios;
    }
}

class CPU{
    private int core;
    private String Model;

    public int getCore() {
        return core;
    }

    public void setCore(int core) {
        this.core = core;
    }

    public String getModel() {
        return Model;
    }

    public void setModel(String model) {
        Model = model;
    }
}

دقت داشته باشید که توی ارث بری ما کلاسمون زیر مجموعه‌ی کلاس دیگه‌ای میشد. ولی در اینجا خودش کلاس اصلی هستش.در واقع در صورتی که رابطه‌ی بین کلاس با کلاس‌های دیگه Has بودش باید از این روش استفاده کنیم.

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *