AY2425S1-CS2103-F13-1 / tp

https://ay2425s1-cs2103-f13-1.github.io/tp/
MIT License
0 stars 5 forks source link

Context sharing across models #109

Open seanlim opened 1 day ago

seanlim commented 1 day ago

The current way we have split our models makes it very hard to share data across different components from address and transaction. We might possibly want to add a generalised model that implements both address.model and transaction.model.

seanlim commented 1 day ago

The way I'm thinking of doing this is similar to the way it is being done for Storage:

Interface + Implementation of separate models:

Add an interface to the current model abstraction chain, similar to what's being done for storage:

/**
 * Represents a storage for {@link AddressBook}.
 */
public interface AddressBookStorage {
...
}

/**
 * A class to access AddressBook data stored as a json file on the hard disk.
 */
public class JsonAddressBookStorage implements AddressBookStorage {
...
}
/**
 * Represents a storage for {@link UserPrefs}.
 */
public interface UserPrefsStorage {
...
}
/**
 * A class to access UserPrefs stored in the hard disk as a json file
 */
public class JsonUserPrefsStorage implements UserPrefsStorage {
...
}

Merged Storage object

We use these interfaces to create a new Mother-of-all Model interface, which we implement as ModelManager, which can be initialised with instances of AddressBookModel and TransactionBookModel.

Here is how Storage does it:

/**
 * API of the Storage component
 */
public interface Storage extends AddressBookStorage, UserPrefsStorage {
...
}
public class StorageManager implements Storage {
    // Initialise with instance of each storage
    private AddressBookStorage addressBookStorage;
    private UserPrefsStorage userPrefsStorage;

    // Expose all methods from all storages
    @Override
    public Optional<UserPrefs> readUserPrefs() throws DataLoadingException {
        return userPrefsStorage.readUserPrefs();
    }
    @Override
    public Optional<ReadOnlyAddressBook> readAddressBook(Path filePath) throws DataLoadingException {
        return addressBookStorage.readAddressBook(filePath);
    }
...
}