Validating a variable before calling the super constructor

So, I am writing code that includes the extension of the class that I wrote earlier, in which the files are created were named using a constructor that takes a name and size of type long. In this source class, I checked inside the constructor that the entered file name contains one ".". but does not require a specific extension in the file. For this new class that I am writing, I need the extension name ".mp3". However, my compiler does not like checking against a super constructor.

This is my current code:

public class Song extends DigitalMedia{

private String artist;
private String album;
private String name;
private long size;

public Song(String aName, long aSize, String aArtist, String aAlbum){
    super(aName, aSize);
    setArtist(aArtist);
    setAlbum(aAlbum);
}

Is there a way to verify that "aName" contains ".mp3" before I create this constructor?

+4
4

, , super:

public Song(String aName, long aSize, String aArtist, String aAlbum){
    super(validateName(aName), aSize);
    setArtist(aArtist);
    setAlbum(aAlbum);
}

private static String validateName(String name) {
    if (whatever) {
        throw new Whatever();
    }
    return name;
}
+3

, constructor, , states ().

A.java A.java. A.java , , . , , A.java .

, , else, .

, constructor? , , , finalize() this. , , System.gc() , finalize() .

. , , ( getInstance()), Song. . , , .

+2

.

MediaFormat:

interface MediaFormat { }

A MusicFormat, MediaFormat, , :

enum MusicFormat implements MediaFormat {
    MP3("mp3");

    private final String format;

    MusicFormat(String format) {
        this.format = format;
    }

    @Override
    public String toString() {
        return format;
    }
}

DigitalMedia MediaFormat:

class DigitalMedia {
    private final MediaFormat format;
    private final String name;

    public DigitalMedia(String name, MediaFormat format) {
        this.name = name;
        this.format = format;
    }
}

Song MusicFormat:

class Song {
    public Song(String name, MusicFormat format) {
        super(name, format);
    }
}

, MusicFormat, . String, name + "." + format

+1
source

In terms of inheritance, a subclass should not be more strict than a superclass.

But if you want to limit the creation of a subclass, you can make a constructor privateand provide a factory method that does the checking first.

public class Song extends DigitalMedia {

    private String artist;
    private String album;
    private String name;
    private long size;

    private Song(String aName, long aSize, String aArtist, String aAlbum) {
        super(aName, aSize);
        setArtist(aArtist);
        setAlbum(aAlbum);
    }

    public static Song makeSong(String aName, long aSize, String aArtist, String aAlbum) {
        //... validation code
        return new Song(aName, aSize, aArtist, aAlbum);
    }
    ...
}

Instead of restricting the type itself, you use encapsulation to force the invariant.

0
source

Source: https://habr.com/ru/post/1669472/


All Articles