Reputation: 2296
While I was designing an Android application I've created some classes related between them.
I am sure this kind of pattern/relationship is popular and I would like to know more about it for properly naming the classes and learn more about the pattern (to improve the design)
I have a class, Book, with some basic attributes: title, author and synopses.
The books are stored in files, one file per book, and there is a class that manage the file operations load/save/delete. I've called this class BookManager.
The MainActivity has a BookManager for operations like ask for available books, retrieve a book, save a modified book, etc...
What is the name of this kind of relationship between objects?
Upvotes: 2
Views: 67
Reputation: 8528
Your BookManager seems like a Data Mapper, which, as Martin Fowler wrote about it, is:
The Data Mapper is a layer of software that separates the in-memory objects from the database. Its responsibility is to transfer data between the two and also to isolate them from each other. With Data Mapper the in-memory objects needn't know even that there's a database present; they need no SQL interface code, and certainly no knowledge of the database schema. (The database schema is always ignorant of the objects that use it.) Since it's a form of Mapper (473), Data Mapper itself is even unknown to the domain layer.
And about your MainActivity, activities in android are more like application controllers, which also as Martin Fowler explains it as:
A centralized point for handling screen navigation and the flow of an application.
Update:
about your question
I did not know of Data Mapper. Is it not very similar to the MVC pattern?
The answer is no, Data Mapper has nothing to do with MVC. Data Mapper is technically where your SQL lives, and it separates your data retrieving process from your domain logic, for instance, if you need to insert a row into a table you will have a method called save
or insert
, but the data mapper would not care less how the data been handled, processed or manipulated by your business logic, it just takes the arguments you passed to it and insert it into your storage, also in a good design, your data mapper should not care what type of DBMS you are using,
Example rudimentary code:
abstract class DataBase implements SomeStroageInterfaces{
protected $host;
protected $username;
protected $password;
protected $databaseName;
protected $isConnected;
public function __construct($host, $username, $password, $databaseName) {
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->databaseName = $databaseName;
}
abstract public function connect();
abstract public function createConnectionString();
public function isConnected(){
return $this->isConnected;
}
}
//concrete class example
class MySql extends DataBase{
public function connect($connectionString = NULL) {
if(is_null($connectionString)){
//create connection string
}else{
//connect using connection string
}
}
public function select() {
//some insert implementation;
}
public function from() {
//some update implementati
}
/*some other implementations and abstract methods implementation too*/
so your mapper would look like:
class TestMapper{
private $dbh;
public function __construct(DataBase $dbh){
$this->dbh = $dbh;
}
public function selectTest($arg1){
$dbh->select($arg1)->from('tableName');
}
your data mapper here has no clue about what dbms you are using, it simply selects $arg1
from tableName
using the $dbh
you send it to it.
However, you can also use any relevant object to handle your queries, the main point is, your storage interaction with your application is done through your data mapper.
Note: my syntax is using PHP, and it is just faster for me to write in it than java.
Upvotes: 1
Reputation: 9488
I think that Adapter Pattern is appropriate here:
class Book
{
public readonly string Title;
// ctor, serialization, other data...
}
interface IFileSystem
{
void Save(string filename, object objToSerialize);
}
interface IBookSaver
{
void SaveBook(Book book);
}
class FileSystemBookSaver : IBookSaver
{
private readonly IFileSystem adaptee;
public FileSystemBookRepository(IFileSystem adaptee)
{
this.adaptee = adaptee;
}
public void SaveBook(Book book)
{
adaptee.Save(book.Title, book);
}
}
FileSystemBookSaver
adapter can be used to save books (add other operations if you need and change class name) using file system.
Upvotes: 0