Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what kind of relationship is there between a common wall and the rooms that located next to it?

I want to know whats the Relationship between a common wall (that are located In an adjoining room ) and the rooms.
As i know the relationship between a room and its walls is Composition not Aggregation (am i right ?)

And according to the definition of Composition the contained object can't be shared between two containers, whereas in aggregation it is possible.

now i am confused that whats the best modeling approach to represent the relationship between a common wall and the rooms located next to it ?

It would be highly Appreciated if you could provide your advices with some code.

|--------|--------|

Approch1:

(wall class ---- room class) /Composition

Approach2:

wall class ----- room class /Aggregation

Approch3:

we have a wall class and a Common wall class , Common wall class inherits from wall class

adjoining room class ---- (1) Common wall class /Aggregation
adjoining room class ---- (6) wall class / composition

Approach4: I am a developer not a designer :) so this is my idea :

class Room 
{
  private wall _firstwall  ;
  private wall _secondtwall;
  private wall _thirdwall  ; 
  private wall _commonwall ;

public Room( CommonWall commonwall)
 {
 _firstwall=new Wall();
 _secondtwall=new Wall();
 _thirdwall=new Wall();
 _commonwall=commonwall;
 }

}

Class CommonWall:Wall
{
 //...
}

// in somewher :

 static void main()
{
  Wall _commonWall=new Wall();
  Room room1=new Room(_commonWall);
  Room room2=new Room(_commonWall);
  Room [] adjacentRoom =new Room[2]{room1,room2};
}

Edit 1: I think this is a clear question but just for more clarification :

The point of the question is to find out whats the best pattern or approach to model a relationship for an object that is a component of two other objects in the same time.

and about my example : waht i mean by a "room" ?,surely i mean an enclosed square room with 4 walls and one door.but in this case one of these walls is a common wall and is shared between two adjacent rooms.

like image 393
siamak Avatar asked Jun 27 '12 23:06

siamak


2 Answers

The answer to your question about the room and the wall is the answer to this question: "Can the wall exist without the room?"

I believe your scenario is using aggregation. Can the wall exist without the room? Sure it can. One could destroy the room by destroying three walls, but that remaining wall stands on its own. I think we're down to semantics now. This answer can change depending on how you view the walls in your scenario.

This link shows a concise way to think about it:

  1. A "owns" B = Composition : B has no meaning or purpose in the system without A
  2. A "uses" B = Aggregation : B exists independently (conceptually) from A

Same here:

Aggregation implies a relationship where the child can exist independently of the parent. Example: Class (parent) and Student (child). Delete the Class and the Students still exist.

Composition implies a relationship where the child cannot exist independent of the parent. Example: House (parent) and Room (child). Rooms don't exist separate to a House

From wikipedia:

Aggregation differs from ordinary composition in that it does not imply ownership. In composition, when the owning object is destroyed, so are the contained objects. In aggregation, this is not necessarily true. For example, a university owns various departments (e.g., chemistry), and each department has a number of professors. If the university closes, the departments will no longer exist, but the professors in those departments will continue to exist. Therefore, a University can be seen as a composition of departments, whereas departments have an aggregation of professors. In addition, a Professor could work in more than one department, but a department could not be part of more than one university.

like image 65
Bob Horn Avatar answered Oct 21 '22 13:10

Bob Horn


There is many to many relationship between Room and Wall. (one wall can be used in two rooms and one room can have many walls.

Instead of having a class like "CommonWall", I would suggest resolve this many to many with with Mapping object and two 1-Many relationships.

public class Room 
{
public List<Wall> Walls{get;set;}

}

public class Wall
{
decimal length;
decimal width;
public List<Room> Rooms{get;set;}
}

public class RoomWallMapping
{
public int MappingID;
public Wall {get;set;}
public Room{get;set;}
}
like image 21
Anand Avatar answered Oct 21 '22 12:10

Anand