如何在使用合成词时从合成词类中达到主要类别?

问题描述 投票:0回答:4

我正在使用类的组成。主类包含2个其他类。我的问题是,我怎样才能从组成类上达到我的主要班级?可能是什么方式?

例如。我们有2个设备的机器类。所有3个类的布尔状态都处于活动状态。我们可以打开设备。任务是:当两个设备都打开时,机器状态为活动。可以使用哪些方法来实现这种行为?

public class Machine {

    private boolean active;

    private Device1 device1;
    private Device2 device2;

    public Machine(Device1 device1, Device2 device2) {
        this.device1 = device1;
        this.device2 = device2;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public Device1 getDevice1() {
        return device1;
    }

    public Device2 getDevice2() {
        return device2;
    }

}

public class Device1 {

    private boolean active;

    public void turnOn(){
        // should check the state of another device and change machine state if necessary
        this.active = true;
    }

}

public class Device2 {

    private boolean active;

    public void turnOn(){
        this.active = true;
        // should check the state of another device and change machine state if necessary
    }

}

public class App {
    public static void main(String[] args) {

        Device1 device1 = new Device1();
        Device2 device2 = new Device2();

        Machine machine = new Machine(device1, device2);

        machine.getDevice1().turnOn();
        machine.getDevice2().turnOn();

        boolean active = machine.isActive();  // should be true
        System.out.println(active);

    }
}

两个设备都打开后,计算机的活动状态应该为true。如何实现呢?

java inheritance composition
4个回答
1
投票

如果我理解您的问题,机器内部的方法应该是这样的。

public boolean isActive() {
        this.active = device1.isActive() && device2.isActive()
        return this.active;
 }

您必须检查是否已打开所有定义的设备。应该清楚的是,每个设备都应具有描述关闭电源后是否处于活动状态的状态。


2
投票

最简单的解决方案是做这样的事情:

public boolean isActive() {
    if (device1 == null || device2 == null) return false;
    return device1.isActive() && device2.isActive();
}

您可能会考虑的一件事是为设备使用Abstract类或让设备扩展计算机。然后可以拥有计算机列表,并独立检查每个计算机的活动状态。


0
投票

一种方法可能是Machine类中不具有isActive布尔状态,而仅具有如下所示的isActive方法:

public boolean isActive(){
    return device1.isActive() && device2.isActive();
} 

0
投票

以下将是更好的设计。此设计假定一台机器可以具有一个或多个设备,并且每个设备都将引用该机器。

public class Machine {

    private boolean active;
    public isActive(){      
        return this.active;
    }

    public setActiveIfAllDevicesAreTurnedOn(){
        for(Device device: deviceList)
            if(!device.isActive())
                return;
        this.active=true;
    } 

    private List<Device> deviceList=New ArrayList<Device>();
    //public getter and setter of deviceList
    ...
 }

public class Device1 {
    Machine machine;
    //public getter and setter of machine
    ...

    private boolean active;
    public void turnOn(){
        // should check the state of other devices and change machine state if necessary
        this.active = true;
        this.machine.setActiveIfAllDevicesAreTurnedOn();
    }
    public boolean isActive(){
        return this.active;
    }
 }

 public class App {
    public static void main(String[] args) {

        Device1 device1 = new Device1();
        Device2 device2 = new Device2();        

        Machine machine = new Machine();
        machine.getDeviceList().add(device1);
        machine.getDeviceList().add(device2);

        device1.setMachine(machine);
        device2.setMachine(machine);

        machine.get(0).turnOn();
        machine.get(1).turnOn();

        boolean active = machine.isActive();  // should be true
        System.out.println(active);
    }
}

0
投票

以下将是更好的设计。此设计假定一台计算机可以具有一个或多个设备,并且每个设备都将引用该计算机。

public class Machine {

    private boolean active;

    private List<Device> deviceList=New ArrayList<Device>();
    //getter and setter of deviceList
    ...
 }

public class Device1 {
    Machine machine;
    //getter and setter of machine
    ...

    private boolean active;
    public void turnOn(){
        // should check the state of other devices and change machine state if necessary
        this.active = true;
        for(Device device: this.machine.getDeviceList())
            if(!device.isActive())
                return;
        this.machine.setActive(true);
    }
    public boolean isActive(){
        return this.active;
    }
 }

 public class App {
    public static void main(String[] args) {

        Device1 device1 = new Device1();
        Device2 device2 = new Device2();        

        Machine machine = new Machine();
        machine.getDeviceList().add(device1);
        machine.getDeviceList().add(device2);

        device1.setMachine(machine);
        device2.setMachine(machine);

        machine.getDevice1().turnOn();
        machine.getDevice2().turnOn();

        boolean active = machine.isActive();  // should be true
        System.out.println(active);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.