How to avoid multiple creation of the same object reference?

Imagine that I have several classes that look like this:

class Car {
    private Image carImage;

    public Car(int imageIndex) {
        switch (imageIndex) {
            case 1: carImage = generateCarImage(1); break;
            # and so forth
        }
    }
}

class Audi extends Car {
    private int numberOfSeats;
    public Audi(int imageIndex, int numberOfSeats) {
        super(imageIndex);
        this.numberOfSeats = numberOfSeats;
    }
}

Now imagine that I am creating several Audi using the same image:

Audi car1 = new Audi(1,2);
Audi car2 = new Audi(1,3);

Will car1 and car2 distribute the same object? I suppose not, but is there a way I can do this? I ask because I want to avoid generating and saving the same image twice.

EDIT:

Will these two audio links be to the same car, for example. An image is generated and saved only once, and any changes in one affect the other?

class Car {
    private Image carImage;
    public Car(int imageIndex) {
        switch (imageIndex) {
            case 1: # carImage = readfile(1.jpeg)
            # and so forth
        }
    }
}

class Audi{
    private int numberOfSeats;
    private Car car;
    public Audi(Car car, int numberOfSeats) {
        this.car = car;
        this.numberOfSeats = numberOfSeats;
    }
}

Car car = new Car(1);
Audi audi1 = new Audi(car,2);
Audi audi2 = new Audi(car,2);

EDIT 2:

, . , , , .

, (PartsInfo ), ( case-). , PartsInfo ( , Car.getPartsInfo()) ( Car PartInfo).

hashmap , , , .

, - "audi-a4-2003" PartsInfo "audi-a4-2003" , , , .., "audi-a4-2004".

Class PartsInfo {
    // lots of stuff I'd rather not create nor save multiple times
}

Class PartsInfoFactory {
    private static HashMap<String, PartsInfo> partsInfoMap = new HashMap<String, PartsInfo>();

    public static getPartsInfo(String id) {
        if (!partsInfoMap.containsKey(id)) {
            generatePartsInfo(id);
        }
        return partsInfoMap(id)
    }

    private static generatePartsInfo(String id) {
        // Do stuff I don't want to do twice for same ID
        partsInfoMap.put(id)
    }
}

Class Car {
    private Color color;
    private String id;
    // Notice that PartsInfo is not stored here

    public Car(Color color, String id) {
        this.color = color;
        this.id = id;
    }

    public PartsInfo getPartsInfo() {
        return PartsInfoFactory.getPartsInfo(id);
    }
}
+4
7

car1 car2 ?

. . Java . , Audi Audi Car.

?

.

, .

. , . , WeakHashMap. , : WeakHashMap WeakReference?

+5

- : , generateCarImage:

class Car {
  private final Image image;

  Car(Image image) {
    this.image = image;
  }
}

class Audi extends Car {
  Audi(Image image, int numDoors) {
    super(image);
    // ...
  }
}

, image - . , , , , :

Image image = generateCarImage(1);
Audi car1 = new Audi(image, 4);
Audi car2 = new Audi(image, 2);

, generateCarImage, , , . .

+4

, . , , .

+3

Java , ( , . Java , ).

Java , , .

, car1 car2 , .

, , . - Car, , , :

Simple chart

Car, , Audi?

. Car factory Car, , .

+2

, , , ; : , .

, : . OO- Java, () factory, , , , .

+2

, . , , enum , .

, , , switch.

, enum, , , , .

class CarImageDirectory
{
    // Created at Run-time

    public static final Image Audi = new Image("Audi");
    public static final Image Toyota = new Image("Toyota");
    // ..etc
}

enum CarImage
{
  // Created at Compile-time

  Audi
  {
    @Override public Image image () { return CarImageDirectory.Audi; }
  },
  Toyota
  {
    @Override public Image image () { return CarImageDirectory.Toyota; }
  }; // ..etc

  public abstract Image image ();
}

CarImage :

CarImage A = CarImage.Audi;
CarImage B = CarImage.Audi;

if (A == B) System.out.println("A and B are both Audi");

Car, :

class Car
{
    private CarImage carImg;
    public Car (CarImage carImg) { this.carImg = carImg; }
    public Image getImage () { return carImg.image(); }
    public CarImage getCarImage () { return carImg; }
}

class AudiCar extends Car
{
    private int numOfSeats;

    public AudiCar (int numOfSeats)
    {
        super(CarImage.Audi);
        this.numOfSeats = numOfSeats;
    }
}

class ToyotaCar extends Car
{
    private int numOfSeats;

    public ToyotaCar (int numOfSeats)
    {
        super(CarImage.Toyota);
        this.numOfSeats = numOfSeats;
    }
}

CarImage switch:

CarImage A = CarImage.Audi;

switch(A)
{
  case CarImage.Audi:
    System.out.println("This car is Audi");
    break;

  case CarImage.Toyota:
    System.out.println("This car is Toyota");
    break;

  default:
}
+2

Have you looked at the "flyweight" template? This can reduce the creation of objects for you. Technically, this reduces the amount of memory, but if creating an object is expensive and there is a lot of reuse, you can use it in situations where the startup time is not a problem, for example, when starting server applications.

In any case, optimize only if you know about a performance issue.

Hope this helps!

+1
source

All Articles