473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

inheritance vs composition 3 tier archiecture

I am starting to develop applications in the form of 3 tier really (4 tier) architecture at the moment.

I am still learning and at the moment I am developing a simple guest book.
With the power of this tiering system however I hope to increase the functionality of it soon quickly and easily.

Anyway I am sort of getting confused with my class structures.

I dont know whether it is better to use inheritance (subclasses) or composition (putting objects inside classes.

Here is an example of the dilema.

inheritance

1. DB access layer
[PHP]class db
{
var $link
var $query
function db($host ........)

function ......
}[/PHP]

2.business layer


[PHP]class comments extends db{

}[/PHP]

-------------------------------------------------------
OR

composition

1. DB access layer
[PHP]class db
{
var $link
var $query
function db($host ........)

function ......
}[/PHP]
2.comments layer

[PHP]class comments
{
function comments
{
$db = new db;
}
function add_comments
{
.........
}

}[/PHP]__________________________________________________

which do you think is better?

Thanks
Nov 16 '07 #1
2 1673
Atli
5,058 Expert 4TB
Hi.

I faced the same problem at one point. I started out by inheriting the Database class everywhere and that worked fine. Until I came across a situation where one of my classes needed to inherit another class.

At that point I stopped inheriting my Database class everywhere, which was always a mess to tell you the truth, and started to import it as a parameter in the constructor. Or, sometimes when I'm feeling lazy, I just create a global Database class I use everywhere.

So, the short answer is, don't inherit it. Create a instance of it inside your other classes.
Nov 17 '07 #2
Yes I see. The more I think about it composition seems the best option. Maybe I could use inheritance with classes on the same tier but when jumping between tiers its a no no as you said. I think by its very nature this architecture dosent want caller to know the implementation of classes on a lower tier. Just use it. Thats why I think that a db class should be called with in the constructor of a higher (logic) layer.

Anyone with good experience of this type of thing could you please give your opinion thanks.
Nov 17 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dave | last post by:
Hello all, The methodology of policy-based design states that one should inherit from policy classes and, of course, these classes must provide an agreed-upon public interface and semantics. ...
10
by: davidrubin | last post by:
Structural inheritance (inheriting implementation) is equivalent to composition in that a particular method must either call 'Base::foo' or invoke 'base.foo'. Apparantly, The Literature tells us to...
9
by: Code4u | last post by:
My colleagues and I have been discussing techniques for implementing interfaces in C++. We're looking for a mechanism similar to COM's QueryInterface, in which a certain types of objects can be...
60
by: Shawnk | last post by:
Some Sr. colleges and I have had an on going discussion relative to when and if C# will ever support 'true' multiple inheritance. Relevant to this, I wanted to query the C# community (the...
2
by: Wilson | last post by:
Hi, a very simple question. I am trying to understand inheritance using c++ and dont cee how i could use three classes to create an accounting program using inheritance. e.g one class containing...
6
by: Bart Simpson | last post by:
I remember reading on parashift recently, that "Composition is for code reuse, inheritance is for flexibility" see (http://www.parashift.com/c++-faq-lite/smalltalk.html#faq-30.4) This confused...
7
by: snewman18 | last post by:
In learning about design patterns, I've seen discussion about using inheritance when an object's relationship to another object is 'is-a' and composition when the relationship is 'has-a'. Since...
18
by: GD | last post by:
Please remove ability to multiple inheritance in Python 3000. Multiple inheritance is bad for design, rarely used and contains many problems for usual users. Every program can be designed only...
4
by: Pallav singh | last post by:
Hi All i have dout when to use Composition and Inheritance while designing Module in C++ Thanks Pallav
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.