473,788 Members | 2,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

inheritance vs composition 3 tier archiecture

24 New Member
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 1689
Atli
5,058 Recognized Expert Expert
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
traineeirishprogrammer
24 New Member
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
3930
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. I would like to understand better why inheritance, rather than containment, is espoused. The only things I can see that inheritance provides beyond containment is the possibility of polymorphism and access to protected members. Since neither of...
10
2666
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 prefer composition over inheritance. Can anyone provide some reasons why this is the case (based on "real-world" experience)? For example, is structural inheritance more difficult to maintain? More difficult to test? Have a larger impact on...
9
2316
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 queried, at run-time, for a particular interface and if it is supported, a pointer or reference to that interface passed to the caller. Two possible implementations came up, multiple inheritance and composition. Using MI the class multiply inherits...
60
4942
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 'target' programming community herein) to get some community input and verify (or not) the following two statements. Few programmers (3 to7%) UNDERSTAND 'Strategic Functional Migration
2
2354
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 members for both, and then a class for checking accounts and a class for savings. Finally could you just clarify that this is the correct use of i nheritance because i am thoroughly confused.
6
3828
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 me somewhat as I have always thought you get code reuse "for free" with inheritance. Am I missing something?. Will someone care to explain ??
7
3071
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 this is all new and I'm still learning, I was hoping someone can give me some pointers on best practices on applying these ideas. If my logic is incorrect on anything, please don't hesitate to tell me where I'm wrong - I'm completely open to any...
18
1691
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 with single inheritance. I also published this request at http://bugs.python.org/issue2667
4
2478
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
10177
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10118
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9969
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5403
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.