konstantin_doncov
konstantin_doncov

Reputation: 2879

Use two completely different classes as one

I have very stupid question about design patterns: let's say we have two classes Post and Product, for each of them we have different table in the DB, and they have nothing in common with each other, so we can't create base class for them. Some Posts even contains Products. And here's what we should do with them:

  1. Somehow store Post and Product instances in the DB, pack them in one array(using C++, if it matters) when user requests news feed from the next item, send it to the client, and receive and unpack on the client side(using Java).
  2. Next, we have to show both Post and Product in the one list(such as news feed on the Facebook).
  3. Also, we can share Post or Product with our friends using chat. So we can send Post or Product as an attachment of the message(consequently, we should to store id of sent Post or Product in the column attached_item of the messages table in the DB on the server side).

So, what design pattern would be best here? How should I implement the Post and Product classes?

Upvotes: 1

Views: 54

Answers (2)

Bentaye
Bentaye

Reputation: 9756

It is a very broad question, but here is a skeleton of what you could you, just to give you some ideas:

// An interface containing methods specific to objects you can list
interface Listable {}

// An interface containing methods specific to objects you can share
interface Shareable {}

// An interface containing methods specific to objects you can send
interface Sendable {}

class Post implements Listable, Shareable, Sendable {
    List<Product> products;
}

class Product implements Listable, Shareable, Sendable {
}


class ListManager {
    public void addToList(Listable element) { }
}

class ShareManager {
    public void share(Shareable element) { }
}

class SendManager {
    public void send(Sendable element) { }
}

You could then use Post and Product interchangeably this way:

Post post = new Post();
Product product = new Product();

ListManager listManager = new ListManager();
listManager.addToList(post);
listManager.addToList(product);

ShareManager shareManager = new ShareManager();
shareManager.share(post);
shareManager.share(product);

SendManager sendManager = new SendManager();
sendManager.send(post);
sendManager.send(product);

Regarding the database representation, as suggested fusiled in his comment, just stick them in 2 separate tables. With a mapping table in between to link the products to their post.

EDIT Regarding the issue with the MESSAGES table

You could add a new mapping table MESSAGE_ATTACHED_ITEM with columns messageId, postId, productId. Only set a value to the relevant colum when attaching an item to a message

Or an other option would be to have an ATTACHED_ITEM table with an id only. And have Post and Product tables to have a foreign key to this table Id. you can then stick this attachedItemId into your attached_item column

Upvotes: 1

fusiled
fusiled

Reputation: 309

I think the solution could be simpler than you think. Why don't you ust use a common Java-like interface and hide the implementation details?

Just implement a common interface with the methods you need. Supposing this common interface is called EntityInterface:

public class Post implements EntityInterface {};
public class Product implements EntityInterface {};

Then when you want to handle these classes, you treat them as an EntityInterface object:

EntityInterface myNewPost = new Post();
EntityInterface myNewProduct = new Product();
//Now you see myNewProduct and myNewPost as EntityInterface objects

These code fragments are in Java, but use virtual functions in C++ and you get the same.

Upvotes: 1

Related Questions