<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programmer&#039;s Notes &#187; OOP</title>
	<atom:link href="http://programmersnotes.info/tag/oop/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmersnotes.info</link>
	<description>Notes on the web-development and artificial intelligence.</description>
	<lastBuildDate>Mon, 18 Jul 2011 13:20:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Requirements and Use Cases. SMS Notification System.</title>
		<link>http://programmersnotes.info/2010/03/18/requirements-use-cases-sms-notification/</link>
		<comments>http://programmersnotes.info/2010/03/18/requirements-use-cases-sms-notification/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 06:00:56 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
				<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[experiece]]></category>
		<category><![CDATA[modelling]]></category>
		<category><![CDATA[sms-notify]]></category>
		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=342</guid>
		<description><![CDATA[Background I&#8217;ve graduated in March 2010 and I don&#8217;t need to visit university any more. However, I enjoyed studying there and I know what challenges my fellow students-programmers have. That&#8217;s why I continue the job started this autumn – computer club, we call it “Geeks&#8217; Club”. Sure, we&#8217;re not true geeks there, but we tend [...]


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<h2><a name="background">Background</a></h2>
<p>I&#8217;ve graduated in March 2010 and I don&#8217;t need to visit university any more. However, I enjoyed studying there and I know what challenges my fellow students-programmers have. That&#8217;s why I continue the job started this autumn – computer club, we call it “Geeks&#8217; Club”. Sure, we&#8217;re not true geeks there, but we tend to be <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  So I&#8217;m helping students who&#8217;re interested in web-technologies and in object-oriented programming. I explain them things that are not covered in the standard university course. Things, that are more practical and they can be paid money for.</p>
<p><span id="more-342"></span></p>
<p>Here I&#8217;ll be covering both OOP and web-dev, but we&#8217;ll start from design hence OOP. The story began when some time passed after we started learning OOP at the club meetings and I decided that there is enough theory and separate examples, it&#8217;s time to build something practical that will be challenging for me too.<br />
I thought of different systems, but finally decided to pick up the SMS notification one. It requires both user interface, database, API (my friends want to communicate to it from standalone Java and C++ applications) and overall application design. In addition, I&#8217;d like such system to be deployed at our university and it is interesting for me to create one.</p>
<h2><a name="intro">Introduction</a></h2>
<p>This is going to be a series of articles, I&#8217;ll describe the progress and challenges we come across as we proceed with development. I think this kind of posts will be interesting for those who study OOP and want to apply what they&#8217;ve learned to some practical things. I&#8217;ll try to guide my students through the process using the iterative development methodology.</p>
<p>We have meetings once a week (on Saturdays), so I&#8217;ll post once or twice a week, depending on amount of work done on weekends and my free time during the week.</p>
<p>I think, this series will contain around 10 posts. We&#8217;re going to share the work done as an open-source project. There is nothing to share at the moment, so I&#8217;ll give the link when we code something <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2><a name="overview">System overview</a></h2>
<h3><a name="vision">Vision</a></h3>
<p>Obviously, the main purpose of the system is to send SMS notifications to students or tutors. These notifications may be quite different – from the timetable changes to some announcements or even warnings (e.g. “Due to the weather conditions, today is a day-off for all. Stay at home and have a good time”).</p>
<p>Actually the system isn&#8217;t that complex. Basically all it needs to do is to enable administrator pick a group of users, enter the message and send it through the SMS gateway. The tricky things start from “pick a group of users”. We want this to be as flexible as possible so we could use this system not only for university, but any other organization.</p>
<h3><a name="vision">University structure</a></h3>
<p>This is obvious to anyone who studied at the university in Ukraine, Russia and former USSR, but I&#8217;m not sure the structure is the same abroad. So we have the university. There are faculties (e.g. Computer Science, Philology etc). These faculties have specialties. For example, Computer Science faculty has 2 specialties – “Intellectual Systems of Decision-Making” (mine) and “Computer Engineering”. (For those interested about the difference, first one deals more with AI technologies while the latter – with system programming, networks etc).</p>
<p>Next level is “course”. We call it so, but actually that is the year of study. I know, there are special words for the 1st year students, for the 2nd etc. Here we just say “I&#8217;m on the first course”. That means “I&#8217;m studying the first year”.</p>
<p>All students that study the same specialty and are on the same course are called “Stream”. This is literal translation and I&#8217;m sure there is some more correct term. I&#8217;d be thankful if someone posts it in comments.</p>
<p>There are 2-4 groups in the “stream”. In our university they are names as follows: 101, 102, 103 etc. First number is course. In this case it is the first one. Second number is the faculty code. 0 is for computer science. And the third number is the group number. Thus 102 is the second group of first-year  students studying Computer Science. I realize that not all universities name groups like this, so we don&#8217;t want to stick to such naming in our product.</p>
<p>Now let&#8217;s go through the tutors and staff. There is a rector of a university. There are deans for every faculty. And all tutors are grouped by departments. Usually, departments relate to the faculty, but that&#8217;s not a rule. For example, on our faculty we have Department of Higher Mathematics, Department of Intellectual systems ans some others. One tutor usually belongs to one department.</p>
<p>In addition, there are different clubs for students like our “Geeks&#8217; Club”, translators&#8217; “Pickwick Club” etc. Some of these clubs (like ours) have departments (Web-development, OOP, Java, C++, Delphi, Administration etc). </p>
<p>We want to be able to filter users using all the above groups plus some more user-specific filters like age or gender.<br />
Let&#8217;s go through the planned features now.</p>
<h3><a name="userperspective">User perspective</a></h3>
<p>First of all, user can log in. User should enter his phone number and password. We&#8217;ll possibly add additional security for administrators. If administrator logs in, he enters his phone and password, but then a dynamic password is sent to his phone. And he completes login procedure only after he enters this dynamic password. We&#8217;re not sure if we&#8217;ll implement this. Your comments are welcome!</p>
<p>Next, user can edit his profile. He&#8217;ll have the following fields there:</p>
<ul>
<li>Phone</li>
<li>Email</li>
<li>Password (and confirmation)</li>
<li>Name, surname, patronymic</li>
<li>Gender</li>
<li>Birthday</li>
<li>ICQ</li>
<li>Faculty</li>
<li>Course (1st year, 2nd year etc)</li>
<li>Group</li>
<li>Clubs user is member of</li>
<li>Any tags user assigns to himself (e.g. “musician”)</li>
</ul>
<p>Plus user should check the option “receive SMS”.</p>
<p>However, user can&#8217;t edit phone directly because we&#8217;ll have to confirm it. So instead of giving him the field in the profile editing form, we&#8217;d rather give him a button or a link which will bring him a from where he puts new phone number and password. After that a dynamic password is sent to his phone via SMS and he enters this dynamic password and phone is updated.</p>
<p>Finally, user may register. When registering, he enters nearly the same data as when editing profile, but there is a dynamic password sent to him in order to confirm the phone number.<br />
All these screens are shown below.</p>
<div id="attachment_346" class="wp-caption aligncenter" style="width: 563px"><a href="http://programmersnotes.info/wp-content/uploads/2010/03/UserArea.png"><img src="http://programmersnotes.info/wp-content/uploads/2010/03/UserArea.png" alt="User area screens" title="UserArea" width="553" height="702" class="size-full wp-image-346" /></a><p class="wp-caption-text">User area screens</p></div>
<h3><a name="groupadmin">Group Administrator perspective</a></h3>
<p>Group administrator role may be assigned to the club&#8217;s leader, to the group monitor (or it&#8217;s better to say headman? What sound more “English”), to the tutor or headmaster. Basically everyone who needs to send notifications will have this role.</p>
<p>So, this type of users can do the same as the simple user plus searching users, sending email notifications and viewing statistics.<br />
Each group administrator will be limited to the groups he may view/notify. For example, headmaster can notify anyone. Tutor may notify all groups of students he tutors in. Group monitor may notify all students within his/her group.</p>
<p>Listing users and searching through them is done in the same screen. In the main part of the screen there is a list of users that are in the groups available to the group administrator. In the left side there is a “narrow search” panel which lists all filters that can be applied to the current set of users. Clicking the filter option (e.g. “Geeks&#8217; Club”) lists all users that fall into the current filter conditions. And it also refreshes the filtering options. For example, if there is no girls in the “Geeks&#8217; Club” then there is no point to show these option in the filter. This is harder to code, but it will bring excellent user experience.</p>
<p>In the right hand side there will be the “saved searches” panel. When you applied some searching criteria, you may save it with some label so you can quickly access this search in the future. Were going to create standard searches in addition to filtering to simplify the system&#8217;s usage.</p>
<p>This works as following. For example, you can view all Computer Faculty. But you need only girls from the Geeks&#8217; Club. So you filter by gender and then by club. And save this search as “Girls-geeks” <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  This immediately appears in the right “saved searches” panel.</p>
<p>These saved searched may be group-specific and user-specific. This basically means that when group admin saves search, he may select if he wants it to be private or he wants to share this search with other group administrators, who have access to the same groups as he does. We think, this will help users creating the searches that they really need. Sure, saved searched may be easily removed.</p>
<p>In the right side there will be also a “Send notification” panel. It will contain only textarea field and a “send” button. When you have your group ready, you type the message an click “send”. It asks for confirmation, displaying how much it would cost and sends it over.</p>
<p>Another page in the Group administrator&#8217;s  perspective is statistics. Here he&#8217;ll see how much messages he sent during the last day/week/month/overall and how much did it cost.</p>
<p>As we progress with development, we may add some settings, that will be the third page then. Here&#8217;s the filtering screen:</p>
<div id="attachment_344" class="wp-caption aligncenter" style="width: 568px"><a href="http://programmersnotes.info/wp-content/uploads/2010/03/FilterUsers.png"><img src="http://programmersnotes.info/wp-content/uploads/2010/03/FilterUsers.png" alt="Filter users screen" title="FilterUsers" width="558" height="370" class="size-full wp-image-344" /></a><p class="wp-caption-text">Filter users screen</p></div>
<p>Statistics screen is pretty straightforward, so we&#8217;ll omit it here.</p>
<h3><a name="administrator">Administrator&#8217;s perspective</a></h3>
<p>Administrator is almost god here <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  He can manage users, assign rights to the group administrators, set limits of SMS count, view logs and overall statistics.<br />
Users management screen will be nearly the same as filtering, with the only difference that when clicking the row with user&#8217;s you&#8217;ll see the profile screen with additional options:</p>
<ul>
<li>ban/unban</li>
<li>delete</li>
<li>phone editing does not require confirmation</li>
<li>administrator of (group selection)</li>
<li>notifications limit</li>
</ul>
<p>Logs and statistics screens are quite simple too.</p>
<h2><a name="usecase">Use cases</a></h2>
<p>After analyzing the above overview, we come up with the following use case diagram. It is pretty self-explanatory and written after the description above, so it is useful only as a summary.</p>
<div id="attachment_345" class="wp-caption aligncenter" style="width: 451px"><a href="http://programmersnotes.info/wp-content/uploads/2010/03/UseCaseModel.png"><img src="http://programmersnotes.info/wp-content/uploads/2010/03/UseCaseModel.png" alt="SMS Notification Use Cases" title="UseCaseModel" width="441" height="697" class="size-full wp-image-345" /></a><p class="wp-caption-text">SMS Notification Use Cases</p></div>
<p>Following the iterative development methodology, we should describe all usecases now, but I find this quite useless. After describing all usecases briefly, we should select 10% critical usecases and describe them in full. Full description includes the sequence of actions taken in the main success scenario and all alternative scenarios that may happen (e.g. DB error, connection failure, no users found etc). This is useful sometimes, but I think that for such simple application as this one, we ma skip this step either.</p>
<p>What is really useful about the iterative development at this stage is that it recommends picking 10% of the most critical usecases and start implementing them in full. By “critical” I usually mean those which may result in essential DB structure or application design changes. In our case there are 14 usecases.10% is 1.4 usecase. Let&#8217;s pick only one. The most critical usecase, that affects all the application design and DB structure is the user filtering. The success of an application depend on the stability and usability of this feature. So we should code it efficiently and then move to the other usecases.</p>
<p>I like this approach cause it is much more practical than designing the system from the interface. Changing interfaces and adopting them to core is much more simple and has less risk than doing vice versa.</p>
<p>So we decided to start with user filtering. The first thing we should create is the database structure. This will be covered in the tomorrow&#8217;s post.</p>
<p>I&#8217;d like to learn your opinion on all that. Maybe you can propose some better interface, or some nice filtering approach or just some idea for this. We&#8217;re open to all suggestions and comments! Thank you for reading my blog and following this project <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Subscribe to RSS and stay tuned!</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d342').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Liked the post? Bookmark it</em></strong></a>
<br />
<div class="d342" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;title=Requirements+and+Use+Cases.+SMS+Notification+System." rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;title=Requirements+and+Use+Cases.+SMS+Notification+System." rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;title=Requirements+and+Use+Cases.+SMS+Notification+System." rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;T=Requirements+and+Use+Cases.+SMS+Notification+System." rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;title=Requirements+and+Use+Cases.+SMS+Notification+System." rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;title=Requirements+and+Use+Cases.+SMS+Notification+System." rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Requirements+and+Use+Cases.+SMS+Notification+System.+@+http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F18%2Frequirements-use-cases-sms-notification%2F&amp;t=Requirements+and+Use+Cases.+SMS+Notification+System." rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d342').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d342').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2010/03/18/requirements-use-cases-sms-notification/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What is a framework and why you should use one?</title>
		<link>http://programmersnotes.info/2010/03/14/what-is-a-framework-and-why-you-should-use-one/</link>
		<comments>http://programmersnotes.info/2010/03/14/what-is-a-framework-and-why-you-should-use-one/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 12:53:49 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Yii]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=330</guid>
		<description><![CDATA[Building the best application&#8230; Most of the beginners and, sometimes, even more advanced programmers, want to develop everything from scratch because they want have control over the every aspect of their application. I entirely understand this tendency. When you learned how to program and you can code nearly anything you want (this relates more closely [...]


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<h2><a name="building-app">Building the best application&#8230;</a></h2>
<p>Most of the beginners and, sometimes, even more advanced programmers, want to develop everything from scratch because they want have control over the every aspect of their application. I entirely understand this tendency. When you learned how to program and you can code nearly anything you want (this relates more closely to scripting languages like php, perl, js etc), you start to do something global. Something that will change the world. This may be a new CMS, a new framework, new blog of e-commerce engine. And when you start this, you want to code it in the most efficient way, optimizing every little thing: every function, every SQL query. At the same time, you want to reuse the code you&#8217;re writing. So you tend to create more general solutions than you really need. And this slows down the performance. And again you go into optimization, limiting the possible uses to some extent.<br />
Finally, if you finish your application, it <em>may</em> be faster or better than similar ones (however, usually it is the same or worse), but remember how much time you spent developing it. Yes, you&#8217;re proud of yourself – you&#8217;ve created your own DB abstraction layer, that handles SQL injections in a very smart way. You&#8217;ve also developed your own ORM engine, that goes nicely with your DB abstraction layer. It is quite likely, that you&#8217;ve already created your very own template engine and a nice JS library that makes animation very easy. Great job, my fellow programmer!<br />
<span id="more-330"></span></p>
<h2><a name="brilliant">…but is it really brilliant?</a></h2>
<p>Now let&#8217;s stop for a moment and look back. <strong>Why</strong> you&#8217;ve started developing all that? Say you wanted to create a brand new blogging engine, that is better than WordPress. You&#8217;ve spent several months (I can&#8217;t believe you&#8217;ve created nice DB abstraction, ORM and template engine in a week) to develop all that stack. And all you got is just another blog engine! Yes, it is cool and is capable of doing hundreds of things. You&#8217;ve even thought of the external API, so you can send SMS to be automatically published on your blog. But actually that&#8217;s just a blog!<br />
Do you really think that the final result – blog  engine was worth 2 months of the hard work and sleepless nights? If you answer “yes” and you haven&#8217;t got a feeling that something is wrong, then then you&#8217;re either mega-guru and develop only facebook- and google-scale projects or just a beginner who is still very proud of himself that he can code anything and you want to overemphasize this.<br />
I think, every experienced developer was at that stage. I was there too. Moreover, I was there quite long and I regret of this.</p>
<h2><a name="frameworkdef">So what is the framework?</a></h2>
<p>By this time you may be quite bored and start thinking – what all that story has to deal with the framework. Actually the problem described above when you develop everything from scratch occurs when you don&#8217;t use any frameworks.<br />
So what is the framework? Basically it is a set of libraries/classes, tied together that speed up the development of a certain type of applications. For example, MFC (Microsoft Foundation Classes), Qt, wxWidgets are frameworks that simplify the user interface development. It obvious that you should not draw 6 states of the button and change it on different events when it comes to placing the button on some form. You just use the ready-made class. But when it comes to the web-development, some developers think that they don&#8217;t need the ready-made (and well-tested!) DB abstraction layer, proven authorization classes. They want to develop it themselves. As a result, we often see low-quality and buggy applications. On the other hand, using frameworks does not necessarily gives you the excellent code, but it definitely helps you to get where you need much faster.</p>
<h2><a name="useframeworks">Use frameworks!</a></h2>
<p>When you utilize good framework, you automatically get proven solutions in different spheres – DB connectivity, unit testing (most of frameworks provide tools for this or make it more simple), authentication, users and templates management. In addition, you also get a number of widgets, ready-made components for the problems you even don&#8217;t know about at the moment you start coding, but which usually come up during the development – caching, JS and CSS minimizing and publishing etc. Finally, you also get a bunch of extensions for a variety of tasks: RSS creation, CAPTCHA solutions, autocomplete widgets, treeview, drag&#038;drop components to name a few.<br />
The problem with framework usage for scripting languages is that they are quite simple and lots of developers don&#8217;t want to learn complex framework in order to achieve some simple task. And they are right&#8230; as long as their task is really simple. I think, “simple” is something that soesn&#8217;t require heavy DB usage, caching, user management, web-services integration and has very basic MVC implementation. That&#8217;s a simple homepage. All other projects should be developed with frameworks because that saves time and allows you to achieve better result.</p>
<h2><a name="choose">Choosing the framework</a></h2>
<p>But there is a little problem here <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  What framework is “good”? I wrote about this in one of my previous posts where I explained <a href="http://programmersnotes.info/2009/02/24/yii_framework_of_my_choice/">why I chose Yii as a PHP framework</a> for my project. Selecting wrong framework may result in project failure, so please refer to that post to see how I did that, maybe you&#8217;ll find that approach reasonable and join us in Yii community <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
And the last question I want to discuss here. The quality of code. I believe that using a framework with good design helps you to create better-structured applications that can be easily managed and supported. And the quality of code&#8230; I think this is up to programmer. It is definitely easier to create good application with properly designed framework just because you follow it&#8217;s patterns. However if you ignore the framework&#8217;s ideas and continue to create classes with static methods instead of simple functions, the framework is not very useful for you <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2><a name="notuse">When you should not use the frameworks</a></h2>
<p>Frameworks are good, but you should clearly understand what are they doing and use them only when appropriate. As a rule, framework provides a set of generic tools that simplify creation of any project. Key work is any. These techniques give good result for a standard use. However when it comes to some specific things, especially if they are very demanding in terms of performance, it&#8217;s time to evaluate your tools from the point of view of this specific project.<br />
Consider you have a framework designed for a web 2.0 applications. It works well for the most of the projects you create. However you&#8217;ve got to build an SMS portal. Unlike your usual applications, this doesn&#8217;t deal with HTTP a lot, it uses it&#8217;s own protocol. So 80-90% of your favourite framework&#8217;s tools will be useless here.<br />
If you&#8217;re building very large application that will be working under the high load, you should evaluate your framework and either reconfigure it or refuse from using it at all. Most of the great websites like facebook, for example, don&#8217;t use standard frameworks, but in order to become “facebook”, you should gain popularity and using the framework for your first version ill bring you to the marketing and launch much faster and you&#8217;ll start earning money.<br />
Similarly, if you&#8217;re building the business-card-like page, using the framework is a bit overkill <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2><a name="summary">Summary</a></h2>
<ol>
<li>Use frameworks because they simplify your development, help getting better code and simplify documenting your solution (framework documentation is already written!)</li>
<li>Choose framework very carefully!</li>
<li>When solving non-trivial tasks and develop extremely large applications, re-evaluate your framework&#8217;s features and decide if it really worth using it there.</li>
</ol>
<h4>Disclaimer</h4>
<ol>
<li>All above is my own opinion, formed during the 5+ years of everyday web-development. Maybe this is not the absolute truth, but I believe it is somewhere near it <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>Example with SMS portal was for illustration only, I&#8217;ve never developed ones and don&#8217;t know which protocols are used there. I&#8217;d be thankful if someone of my readers clarifies this.</li>
</ol>
<p>And what do you think about it? I&#8217;d be thankful if you share your thoughts in comments. Thanks!</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d330').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Liked the post? Bookmark it</em></strong></a>
<br />
<div class="d330" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;title=What+is+a+framework+and+why+you+should+use+one%3F" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;title=What+is+a+framework+and+why+you+should+use+one%3F" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;title=What+is+a+framework+and+why+you+should+use+one%3F" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;T=What+is+a+framework+and+why+you+should+use+one%3F" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;title=What+is+a+framework+and+why+you+should+use+one%3F" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;title=What+is+a+framework+and+why+you+should+use+one%3F" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+What+is+a+framework+and+why+you+should+use+one%3F+@+http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2010%2F03%2F14%2Fwhat-is-a-framework-and-why-you-should-use-one%2F&amp;t=What+is+a+framework+and+why+you+should+use+one%3F" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d330').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d330').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2010/03/14/what-is-a-framework-and-why-you-should-use-one/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Low Coupling and High Cohesion &#8211; GRASP (Design patterns series)</title>
		<link>http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/</link>
		<comments>http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 06:00:40 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
				<category><![CDATA[Design patterns]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[grasp]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[polymorphism]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=179</guid>
		<description><![CDATA[Low coupling When designing some architecture, you face with the problem &#8220;which object should perform X task?&#8221; We discussed this question in my previous post in this series. There we noted, that object should perform the tasks he has enough info for. He should be an &#8220;expert&#8221;. But because one of the main OOP characteristics [...]


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<h2><a name="low-coupling">Low coupling</a></h2>
<p>When designing some architecture, you face with the problem &#8220;which object should perform X task?&#8221; We discussed this question in my previous post in this series. There we noted, that object should perform the tasks he has enough info for. He should be an &#8220;expert&#8221;. But because one of the main OOP characteristics is interaction between objects, it&#8217;s often hard to answer this question.<br />
<span id="more-179"></span></p>
<h3><a name="lc-example">Introducing example</a></h2>
<p>Consider you should print the table parameters (see <a href="http://programmersnotes.info/2009/02/28/what-is-oop-object-oriented-programming/">What is OOP</a> post for this example). There are tables of 2 types: square and circle ones. Square has parameter &#8220;side&#8221;, circe has parameter &#8211; &#8220;radius&#8221;. And we need the table:<br />
<div id="attachment_312" class="wp-caption aligncenter" style="width: 289px"><a href="http://programmersnotes.info/wp-content/uploads/2009/10/list-of-tables1.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/10/list-of-tables1.png" alt="Tables list" title="List of tables" width="279" height="143" class="size-full wp-image-312" /></a><p class="wp-caption-text">Tables list</p></div><br />
Sure, we can create a class Lister and method Lisert::out(). But what logic to place there? We can go with something like this:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Lister</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$tables</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> out<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+</span></div>
</li>
<li class="li2">
<div class="de2"><span class="st0">| &nbsp;Type &nbsp; &nbsp;| parameter | &nbsp;value &nbsp;|</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="re0">$i</span> = <span class="nu0">0</span>, <span class="re0">$s</span> = <a href="http://www.php.net/sizeof"><span class="kw3">sizeof</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">tables</span><span class="br0">&#41;</span>; <span class="re0">$i</span> &lt; <span class="re0">$s</span>; <span class="re0">$i</span>++<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span> instanceof CircleTable<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;| &nbsp;Circle &nbsp;| &nbsp;radius &nbsp; | &nbsp;&#8217;</span>.<a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%5.2f&#8217;</span>, <span class="re0">$this</span>-&gt;<span class="me1">tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span>-&gt;<span class="me1">radius</span><span class="br0">&#41;</span>.<span class="st0">&quot; &nbsp;|<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;| &nbsp;Square &nbsp;| &nbsp;side &nbsp; &nbsp; | &nbsp;&#8217;</span>.<a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%5.2f&#8217;</span>, <span class="re0">$this</span>-&gt;<span class="me1">tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span>-&gt;<span class="me1">side</span><span class="br0">&#41;</span>.<span class="st0">&quot; &nbsp;|<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">?&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<h3><a name="lc-problem">Identifying the problem</a></h3>
<p>However, this method is strictly linked to using only CircleTable and SquareTable, it will not work without CircleTable and adding other table types is impossible. This can be imagined like this:<br />
<div id="attachment_313" class="wp-caption aligncenter" style="width: 288px"><a href="http://programmersnotes.info/wp-content/uploads/2009/10/classescoupling1.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/10/classescoupling1.png" alt="Classes coupling" title="Classes Coupling" width="278" height="257" class="size-full wp-image-313" /></a><p class="wp-caption-text">Classes coupling</p></div><br />
Notice that since Lister class uses CircleTable and SquareTable, they can&#8217;t be used along. This picture illustrates it &#8211; they are coupled together.</p>
<h3><a name="lc-solution">Coming up with a solution</a></h2>
<p>The correct variant of solving the task of table output is the following.</p>
<ol>
<li>Refactor Table classes putting the output logic, that is specific to the table into the class</li>
<li>Use these new methods of table classes in the Lister::out method</li>
</ol>
<p>Enough talk, let&#8217;s walk!</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> CircleTable <span class="kw2">extends</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$radius</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$r</span>, <span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::__construct<span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span>;<span class="co1">//calling the constructor of the parent class, passing height there.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">radius</span> = <span class="re0">$r</span>;<span class="co1">//setting the radius</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">radius*</span><span class="re0">$this</span>-&gt;<span class="me1">radius*M_PI</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> out<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;| &nbsp;Circle &nbsp;| &nbsp;radius &nbsp; | &nbsp;&#8217;</span>.<a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%5.2f&#8217;</span>, <span class="re0">$this</span>-&gt;<span class="me1">radius</span><span class="br0">&#41;</span>.<span class="st0">&quot; &nbsp;|<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> SquareTable <span class="kw2">extends</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$side</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$s</span>, <span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::__construct<span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span>;<span class="co1">//calling the constructor of the parent class, passing height there.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">side</span> = <span class="re0">$s</span>;<span class="co1">//setting the side</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="re0">$this</span>-&gt;<span class="me1">side</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> out<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;| &nbsp;Square &nbsp;| &nbsp;side &nbsp; &nbsp; | &nbsp;&#8217;</span>.<a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%5.2f&#8217;</span>, <span class="re0">$this</span>-&gt;<span class="me1">side</span><span class="br0">&#41;</span>.<span class="st0">&quot; &nbsp;|<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>And we&#8217;ll use these methods in the Lister class:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Lister</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$tables</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> out<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">| &nbsp;Type &nbsp; &nbsp;| parameter | &nbsp;value &nbsp;|</span></div>
</li>
<li class="li2">
<div class="de2"><span class="st0">+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="re0">$i</span> = <span class="nu0">0</span>, <span class="re0">$s</span> = <a href="http://www.php.net/sizeof"><span class="kw3">sizeof</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;<span class="me1">tables</span><span class="br0">&#41;</span>; <span class="re0">$i</span> &lt; <span class="re0">$s</span>; <span class="re0">$i</span>++<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span>-&gt;<span class="me1">out</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&quot;+&#8212;&#8212;&#8212;-+&#8212;&#8212;&#8212;&#8211;+&#8212;&#8212;&#8212;+<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>Running the code with the same data will give us exactly the same thing. But only one difference &#8211; this code is easy to maintain, change and expand. We can imagine this structure as following:<br />
<div id="attachment_318" class="wp-caption aligncenter" style="width: 422px"><a href="http://programmersnotes.info/wp-content/uploads/2009/10/classeslowcoupling1.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/10/classeslowcoupling1.png" alt="Low coupling illustration" title="Low coupling illustration" width="412" height="337" class="size-full wp-image-318" /></a><p class="wp-caption-text">Low coupling illustration</p></div><br />
For example, if we need one more table &#8211; triangle &#8211; we just add this class and then &#8211; created object into the List::tables property and that&#8217;s all. It will be printed out.<br />
Here is the code:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> TriangleTable <span class="kw2">extends</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$side</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$s</span>, <span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::__construct<span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span>;<span class="co1">//calling the constructor of the parent class, passing height there.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">side</span> = <span class="re0">$s</span>;<span class="co1">//setting the side</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="nu0">0.433</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> out<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;| Triangle | &nbsp; side &nbsp; &nbsp;| &nbsp;&#8217;</span>.<a href="http://www.php.net/sprintf"><span class="kw3">sprintf</span></a><span class="br0">&#40;</span><span class="st0">&#8216;%5.2f&#8217;</span>, <span class="re0">$this</span>-&gt;<span class="me1">side</span><span class="br0">&#41;</span>.<span class="st0">&quot; &nbsp;|<span class="es0">\n</span>&quot;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>And now add this table to the test script:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">&lt;?php</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st0">&#8216;Table.php&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">require_once</span><span class="br0">&#40;</span><span class="st0">&#8216;Lister.php&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="re0">$list</span> = <span class="kw2">new</span> Lister<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$list</span>-&gt;<span class="me1">tables</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> CircleTable<span class="br0">&#40;</span><span class="nu0">5</span>, <span class="nu0">90</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> SquareTable<span class="br0">&#40;</span><span class="nu0">2</span>, <span class="nu0">85</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> SquareTable<span class="br0">&#40;</span><span class="nu0">8</span>, <span class="nu0">100</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> TriangleTable<span class="br0">&#40;</span><span class="nu0">7</span>,<span class="nu0">150</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">new</span> TriangleTable<span class="br0">&#40;</span><span class="nu0">10</span>,<span class="nu0">120</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/header"><span class="kw3">header</span></a><span class="br0">&#40;</span><span class="st0">&#8216;Content-type: text/plain&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$list</span>-&gt;<span class="me1">out</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">?&gt;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>And we&#8217;ll get the following table with <strong>no modifications in the Lister class</strong>:<br />
<div id="attachment_314" class="wp-caption aligncenter" style="width: 285px"><a href="http://programmersnotes.info/wp-content/uploads/2009/10/list-of-tables-21.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/10/list-of-tables-21.png" alt="Modified list of tables" title="Modified list of tables" width="275" height="204" class="size-full wp-image-314" /></a><p class="wp-caption-text">Modified list of tables</p></div><br />
Note, that we can populate tables list from the DB table and no code will be changed. Adding new table type is very simple &#8211; you just develop the class similar to CircleTable or SquareTable and then use it. No more changes needed.<br />
This technique when you do your best to write the isolated pieces of code, which can be reused separately is called &#8220;Low Coupling&#8221;, your goal is to create classes in such a way that they do not depend on each other or check these dependencies preventing runtime errors.<br />
Sure, that&#8217;s the goal, it can&#8217;t be achieved in full, but you should aim to do this.</p>
<h2><a name="high-cohesion">High cohesion</a></h2>
<p>When you do your best to implement the Lout Coupling principle, you write your code in a small portions, and each of them does some definite task. And since each class is small and does only one or several cohesive jobs, you get High Cohesion in your code.<br />
These two principles go together. When you fulfil one of them, you automatically follow another one.</p>
<h2><a name="summary">Summary</a></h2>
<p>Putting it all together, we may say that Low Coupling principle requires writing isolated pieces of code, that form complex logic only by interacting with each other, not by themselves. And While following this principle, our little parts automatically become focused on one task, so they become cohesive. This way we fulfil both principles at once.<br />
And good OO-design follow all GRASP principles at the same time just because they characterise nice application design.</p>
<p>What can you add here? Maybe you&#8217;ve got some questions? Feel free to ask here in comments or by email.<br />
Grab the code of examples <a href="http://programmersnotes.info/wp-content/uploads/2009/10/lc-and-hc.zip">here</a>.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d179').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Liked the post? Bookmark it</em></strong></a>
<br />
<div class="d179" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;title=Low+Coupling+and+High+Cohesion+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;title=Low+Coupling+and+High+Cohesion+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;title=Low+Coupling+and+High+Cohesion+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;T=Low+Coupling+and+High+Cohesion+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;title=Low+Coupling+and+High+Cohesion+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;title=Low+Coupling+and+High+Cohesion+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Low+Coupling+and+High+Cohesion+%26%238211%3B+GRASP+%28Design+patterns+series%29+@+http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F10%2F06%2Flow-coupling-and-high-cohesion-grasp-design-patterns%2F&amp;t=Low+Coupling+and+High+Cohesion+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d179').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d179').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/10/06/low-coupling-and-high-cohesion-grasp-design-patterns/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>OOP in jQuery</title>
		<link>http://programmersnotes.info/2009/04/30/oop-in-jquery/</link>
		<comments>http://programmersnotes.info/2009/04/30/oop-in-jquery/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 20:19:51 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[tip]]></category>
		<category><![CDATA[trick]]></category>
		<category><![CDATA[workaround]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://konstantin.takeforce.net/?p=12</guid>
		<description><![CDATA[Article shows several tricks, that allow to use full-featured OOP techniques in developing with jQuery. Everything is illustrated with live examples.


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<h2><a name="#intro">Intro</a></h2>
<p>OOP is a great concept, it simplifies the programmer&#8217;s life, especially in the field of interface development. Nearly all modern web-interfaces use JS in some way &#8211; for field highlighting, validation, help etc. It&#8217;s OK when there are such simple things.<br />
And now imagine, that we have a set of dialogue windows (HTML+CSS) for adding a comment, rating, sending to the friend. Each of these boxes has a number of functions &#8211; check if user is logged in(if not &#8211; show login form), send AJAX request, process response, show error/success messages, position the box on the page etc.</p>
<p><span id="more-12"></span></p>
<h2><a name="#problem">The problem</a></h2>
<p>First idea I had about them is to use simple inheritance, create Box class, derive CommentBox, RatingBox and other classes, but it turned out, that jQuery (I used it there, because system is built using Yii framework, which uses jQuery for client-side coding) doesn&#8217;t support extending classes like, for example, ExtJS or YUI. Yes, it has $.extend() function, but it just copies the object, not actually extends class. Why it is not the same? Let&#8217;s look here:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co1">//namespace</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">var</span> Popups = <span class="br0">&#123;</span><span class="br0">&#125;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//class constructor</span></div>
</li>
<li class="li1">
<div class="de1">Popups.<span class="me1">Box</span> = <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//initialization here</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">Popups.<span class="me1">Box</span>.<span class="me1">prototype</span> = <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; container:$<span class="br0">&#40;</span><span class="st0">&#8216;&lt;div class=&quot;container&quot;&gt;&lt;/div&gt;&#8217;</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; content:<span class="kw2">null</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; create:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//dom elements generation here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; showHide:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//some logic here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//other methods and properties go here</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Normally (in ExtJS, for example) when writing:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Popups.<span class="me1">CommentBox</span> = $.<span class="me1">extend</span><span class="br0">&#40;</span><span class="kw2">true</span>,<span class="br0">&#123;</span><span class="br0">&#125;</span>, Popups.<span class="me1">Box</span>, <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; create:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="st0">&#8216;overriden!&#8217;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span><span class="br0">&#41;</span></div>
</li>
</ol>
</div>
<p>I expect, that if I do:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">var</span> c = <span class="kw2">new</span> CommentBox<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">c.<span class="me1">create</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>I should get an alert window. However, this is not true for jQuery. It just copies everything and we get some messed things. So I gave this up and followed another pattern.</p>
<h2><a name="#solution">The solution</a></h2>
<p>Box object became container for all boxes. I took care of messaging, determining if user is logged, and all other routines. It also serves as a factory, because all clicking icons go to the showHide method and it has to determine, which box to show and create this inner object.<br />
And this inner object takes care of the box-specific logic. If it needs some common info, for example, the ID of the product it is called for, it calls the Box method (see LowCoupling GRASP principle here <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> ). So in the Box I have:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; getBoxType:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//determine the box we&#8217;re dealing with by the link class</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> tg = $<span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">event</span>.<span class="me1">target</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;f&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;favourite&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;t&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;friend&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;ra&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;rate&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;c&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;comment&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;r&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;rss&#8217;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;add&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;addTag&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span> <span class="kw1">if</span> <span class="br0">&#40;</span>tg.<span class="me1">hasClass</span><span class="br0">&#40;</span><span class="st0">&#8216;b&#8217;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8216;bookmark&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="st0">&#8221;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; getInnerObj:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>!us.<span class="me1">logged</span><span class="br0">&#41;</span><span class="co1">//if user is not logged, we show him the login box instead of anything else</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="kw2">new</span> Popups.<span class="me1">LoginObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> cls;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">type</span> = <span class="kw1">this</span>.<span class="me1">getBoxType</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">switch</span> <span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;favourite&#8217;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Popups.<span class="me1">FavouriteObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;friend&#8217;</span>:</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Popups.<span class="me1">FriendObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;rate&#8217;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Popups.<span class="me1">RateObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;comment&#8217;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Popups.<span class="me1">CommentObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;rss&#8217;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Popups.<span class="me1">RssObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;bookmark&#8217;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Popups.<span class="me1">BookmarkObject</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">default</span>:</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cls = <span class="kw1">this</span>.<span class="me1">objects</span><span class="br0">&#91;</span><span class="kw1">this</span>.<span class="me1">type</span><span class="br0">&#93;</span> = <span class="kw2">null</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> cls;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; show:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//some stuff</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> obj = <span class="kw1">this</span>.<span class="me1">getInnerObj</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">typeof</span> obj == <span class="st0">&#8216;object&#8217;</span> &amp;&amp; obj != <span class="kw2">null</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">content</span>.<span class="me1">append</span><span class="br0">&#40;</span>obj.<span class="me1">getContents</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
</ol>
</div>
<p>So instead of inheritance I implemented aggregation (Box aggregates CommmentObject, Favourite object etc).</p>
<p>Everything goes well until you want to to attach some method of current object as a handler to some event. For example:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Popups.<span class="me1">Box</span>.<span class="me1">prototype</span> = <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//properties here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; productID:<span class="nu0">0</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; create:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">form</span>.<span class="me1">submit</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">onSubmit</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productID = <span class="kw1">this</span>.<span class="me1">parentBox</span>.<span class="me1">getItemID</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; onSubmit:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">productID</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//other methods follow</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Right? Wrong! Because when submit event occurs, jQuery will call the onSubmit method, but this will refer to the form, not to the Box object! However, it is logical to put processing of the form into the same object. In ExtJS and YUI you can pass the scope parameter for every callback function, so you can control the &#8220;this&#8221; variable in the callback call. jQuery doesn&#8217;t allow this. I used the following workaround:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">Popups.<span class="me1">Box</span>.<span class="me1">prototype</span> = <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//properties here</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; productID:<span class="nu0">0</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; create:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> ctx = <span class="kw1">this</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">form</span>.<span class="me1">submit</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.<span class="me1">onSubmit</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productID = <span class="kw1">this</span>.<span class="me1">parentBox</span>.<span class="me1">getItemID</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; onSubmit:<span class="kw2">function</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">productID</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//other methods follow</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>If you need the object, that triggered an event, just pass &#8220;this&#8221; there:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">create:<span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">var</span> ctx = <span class="kw1">this</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">form</span>.<span class="me1">submit</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.<span class="me1">onSubmit</span><span class="br0">&#40;</span><span class="kw1">this</span>, event<span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; productID = <span class="kw1">this</span>.<span class="me1">parentBox</span>.<span class="me1">getItemID</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span>,</div>
</li>
<li class="li1">
<div class="de1">onSubmit:<span class="kw2">function</span><span class="br0">&#40;</span>form, event<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw3">alert</span><span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">productID</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Having these 2 tricks you can use OOP approach to designing reusable interface components in a handy modern way.</p>
<h2><a name="#inheritance">Back to inheritance</a></h2>
<p>Since I discovered the problem with inheritance, I want to develop a plugin, that fixes this, that will provide nice class inheritance model to take jQuery a step ahead. I was going to digg the ExtJS&#8217;s Ext.extend() code and find out how this is done correctly, but while preparing an article, I found this material, which is actually a guide for creation of such plugin: <a href="http://phrogz.net/JS/Classes/OOPinJS2.html">http://phrogz.net/JS/Classes/OOPinJS2.html</a>. Unfortunately, I don&#8217;t have time for this now, but I plan to do this in the nearest future. If someone does this earlier, please let me know <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3><a name="#reading">Links</a></h3>
<ul>
<li><a href="http://extjs.com/">ExtJS</a> &#8211; a component, interface-oriented JS framework</li>
<li><a href="http://developer.yahoo.com/yui/">YUI</a> &#8211; Yahoo library for the user interface. ExtJS was derived from it long ago. They are similar in extending and event processing (the &#8220;scope&#8221; I mentioned initially appeared in YUI)</li>
<li><a href="http://jquery.com/">jQuery</a> &#8211; a lightweight VERY popular JS framework with an easily extensible structure, but lacks OOP techniques!</li>
</ul>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d12').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Liked the post? Bookmark it</em></strong></a>
<br />
<div class="d12" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F04%2F30%2Foop-in-jquery%2F&amp;title=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F04%2F30%2Foop-in-jquery%2F&amp;title=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F04%2F30%2Foop-in-jquery%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F04%2F30%2Foop-in-jquery%2F&amp;title=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F04%2F30%2Foop-in-jquery%2F&amp;T=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F04%2F30%2Foop-in-jquery%2F&amp;title=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F04%2F30%2Foop-in-jquery%2F&amp;title=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F04%2F30%2Foop-in-jquery%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+OOP+in+jQuery+@+http%3A%2F%2Fprogrammersnotes.info%2F2009%2F04%2F30%2Foop-in-jquery%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F04%2F30%2Foop-in-jquery%2F&amp;t=OOP+in+jQuery" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d12').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d12').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/04/30/oop-in-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creator and Information Expert principles &#8211; GRASP (Design patterns series)</title>
		<link>http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/</link>
		<comments>http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 13:37:32 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
				<category><![CDATA[Design patterns]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[grasp]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[polymorphism]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://konstantin.takeforce.net/?p=13</guid>
		<description><![CDATA[Post gives an overview of design patterns, GRASP principles and explains first two of them. Everything is illustrated with code and practical examples.


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<h2><a name="intro">Preface</a></h2>
<p>This is an introductory post to the series, where I&#8217;ll cover all design patterns, their usage, diagrams. Besides patterns, I am going to guide you through the principles of the high-quality application design. We&#8217;ll start from these principles (GRASP), and then advance to patterns, see how they use these principles and develop several systems (architecture only). The whole series will take 10-15 posts. However, I don&#8217;t have exact plan yet <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  So, let&#8217;s go!</p>
<h2>So what we&#8217;re talking about?</h2>
<p>Actually, design pattern is no more, than a proven solution to some common problem. When saying &#8220;problem&#8221; I mean things you face with when developing the application architecture. These solutions are intended to make you application better &#8211; more extensible, flexible, simple. </p>
<p>Common examples of design problems (Let&#8217;s agree that in this series &#8220;design&#8221; means application, not graphics or DB design) are:<br />
<span id="more-13"></span></p>
<ul>
<li>Organizing access to the shared resource. This can be application object, DB abstraction object, Session object or other similar things. Main problem is that they should be created only once and accessed in every method easily. Singleton pattern solves this problem.</li>
<li>Creating the easily extensible application. For example, code shopping cart in the way, that allows to add new payment or taxation modules easily. Adapter pattern saves us here.</li>
<li>Providing the simple interface for accessing features of the application or application module, hiding all the underlying classes/functions from the user. Façade pattern does this.</li>
<li>Separating data from data processing and from presentation. You&#8217;ve definitely heard about MVC (Model-View-Controller) &#8211; that&#8217;s also a pattern!</li>
</ul>
<p>So patterns are everywhere in OOP and knowing them is essential for high-quality and extensible application design.</p>
<h2><a name="basics">The basics</a></h2>
<p>However, patterns themselves are implementation of the 9 basic principles (GPASP &#8211; General Responsibility Assignment Software Principles).<br />
<div id="attachment_162" class="wp-caption aligncenter" style="width: 310px"><a href="http://programmersnotes.info/wp-content/uploads/2009/03/patterns1.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/03/patterns-300x252.png" alt="GRASP and patterns" title="patterns" width="300" height="252" class="size-medium wp-image-162" /></a><p class="wp-caption-text">GRASP and patterns</p></div><br />
All OOP design is about creating real-world abstractions (classes), design methods (responsibilities) and their interaction. The key thing to good architecture is assigning responsibilities to the classes correctly. GRASP deals exclusively with this problem. We&#8217;ll go through all of them:</p>
<ul>
<li><a href="#creator">Creator</a></li>
<li><a href="#expert">Information Expert</a></li>
<li>Low Coupling</li>
<li>High Cohesion</li>
<li>Controller</li>
<li>Polymorphysm</li>
<li>Pure Fabrication</li>
<li>Indirection</li>
<li>Protected Variations</li>
</ul>
<p>Today we&#8217;ll start from the first principle.</p>
<h2><a name="creator">Creator</a></h2>
<p>The most common problem in OO design is &#8220;Who should create object X?&#8221; This principle states, that object A should create object B if:</p>
<ul>
<li><a href="#a_aggregates_b">A contains or aggregates B</a> (e.g. Object User should initialize object Address)</li>
<li><a href="#a_records_b">A records B</a> (saves to DB or file)</li>
<li>A closely uses B</li>
<li>A has the initialization data that should be passed to B when object is created</li>
</ul>
<p>If any of these conditions are true, then A should create B. Now let&#8217;s demonstrate how these principles work in real examples.</p>
<h3><a name="a_aggregates_b">A contains or aggregates B</a></h3>
<p>Consider we have table. Common table consists of desk and legs.<br />
<div id="attachment_166" class="wp-caption aligncenter" style="width: 310px"><a href="http://programmersnotes.info/wp-content/uploads/2009/03/table1.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/03/table-300x277.png" alt="Common table" title="table" width="300" height="277" class="size-medium wp-image-166" /></a><p class="wp-caption-text">Common table</p></div><br />
If we use object decomposition to program the table, we&#8217;ll get 3 classes:</p>
<ul>
<li>
		Table (blue box on image):</p>
<ul>
<li>Desk (light grey)</li>
<li>Leg (wooden)</li>
</ul>
</li>
</ul>
<blockquote><p>Object decomposition basically means breaking some entity into parts and representing each part with a object. These parts can be decomposed further. For example, we can break Table into Desk and Leg. And then break Leg into Screw, Plank. In complex system there is a number of such levels.</p></blockquote>
<p>If we start programming this, we can go in 2 ways. For example, we don&#8217;t want to use the Creator principle and will create Table class as follows:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$desk</span>;<span class="co1">//desk object</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$legs</span>;<span class="co1">//array of legs</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span>Desk <span class="re0">$d</span>, <span class="re0">$l</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">desk</span> = <span class="re0">$dd</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">legs</span> = <span class="re0">$l</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Then Desk and Leg classes:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Desk</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$width</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$length</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$height</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$w</span>, <span class="re0">$l</span>, <span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">width</span> = <span class="re0">$w</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">length</span> = <span class="re0">$l</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">height</span> = <span class="re0">$h</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Leg</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$width</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$length</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$height</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$w</span>, <span class="re0">$l</span>, <span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">width</span> = <span class="re0">$w</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">length</span> = <span class="re0">$l</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">height</span> = <span class="re0">$h</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Yes, Desk and Leg classes are really the same cause they both are parallelepipeds. So we can create Parallelepiped class and extend both of them from it. We&#8217;ll do it later, now we&#8217;re talking about object creation. The above classes can be used in the following way:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$desk</span> = <span class="kw2">new</span> Desk<span class="br0">&#40;</span><span class="nu0">900</span>,<span class="nu0">900</span>,<span class="nu0">20</span><span class="br0">&#41;</span>;<span class="co1">//90 cm width, 90 length (square) and 2cm height. A normal table for kitchen <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </span></div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$legs</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">new</span> Leg<span class="br0">&#40;</span><span class="nu0">40</span>,<span class="nu0">40</span>, <span class="nu0">880</span><span class="br0">&#41;</span>,<span class="co1">//4&#215;4 cm square and 88cm height. 88cm leg + 2 cm desk = 90cm full height</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">new</span> Leg<span class="br0">&#40;</span><span class="nu0">40</span>,<span class="nu0">40</span>, <span class="nu0">880</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">new</span> Leg<span class="br0">&#40;</span><span class="nu0">40</span>,<span class="nu0">40</span>, <span class="nu0">880</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">new</span> Leg<span class="br0">&#40;</span><span class="nu0">40</span>,<span class="nu0">40</span>, <span class="nu0">880</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$table</span> = <span class="kw2">new</span> Table<span class="br0">&#40;</span><span class="re0">$desk</span>, <span class="re0">$legs</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Yes, the object is created, but large part of the creation logic is outside of the object. The class user (the programmer who is using this class) should know the internal structure of objects to create the whole object. This approach is inefficient when we deal with large application; on the other side, such solution can&#8217;t be reused without changing the code. And it violates the first condition of using the Creator principle. Either Desk and Leg are parts of the Table object, so it should take care of their instantiation. So let&#8217;s learn how to solve such task correctly:<br />
We&#8217;ll create constructor for Table class with parameters:</p>
<ul>
<li>Width &#8211; width of the table</li>
<li>Length &#8211; length of the table</li>
<li>Height &#8211; height of the table</li>
<li>DeskHeight &#8211; height of the desk</li>
<li>LegSection &#8211; (assume we want square legs)</li>
</ul>
<p>So the end-user of the class should specify only these 5 parameters, we don&#8217;t ask him to create parts of our object. OK, let&#8217;s code it:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$desk</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; protected <span class="re0">$legs</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$width</span>, <span class="re0">$length</span>, <span class="re0">$height</span>, <span class="re0">$deskHeight</span>, <span class="re0">$legSection</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">desk</span> = <span class="kw2">new</span> Desk<span class="br0">&#40;</span><span class="re0">$width</span>, <span class="re0">$length</span>, <span class="re0">$deskHeight</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">for</span> <span class="br0">&#40;</span><span class="re0">$i</span> = <span class="nu0">0</span>; <span class="re0">$i</span> &lt; <span class="nu0">4</span>; <span class="re0">$i</span>++<span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">legs</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span> = <span class="kw2">new</span> Leg<span class="br0">&#40;</span><span class="re0">$legSection</span>, <span class="re0">$legSection</span>, <span class="re0">$height</span> &#8211; <span class="re0">$deskHeight</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Desk and Leg classes remain the same. So here we followed the first condition (actually, the most important) and got nice architecture.</p>
<h3><a name="a_records_b">A records B</a></h3>
<p>After detailed discussion of the first condition, this should be quite evident. Usually if some object saves another one, it is his part. For example, we have some system with users. Each user may have several addresses. So Address will be the part of User (it is quite logical to design so), and User object will save all addresses in one field (Using serialization). And when loading, it will load the address info from DB and create Address objects. This is example of both 2, 3 and 4 item. The User object saves Address. User object has initialization info for Address and user object closely uses Address. By the way, first item is also here, Address is part of User object.<br />
As you see, all these conditions often go together.<br />
I think, that the final 2 items should be evident.</p>
<h4>Further development</h4>
<p>Tasks for improvement your skills:</p>
<ul>
<li>Extend both Leg and Desk from the Parallelipiped class</li>
<li>Implement getHeight() method for Table, Desk and Leg. Hint: In Leg and Desk classes it should simply return height. In Table class it should return sum of leg height and Desk height</li>
<li>Implement setHeight method for all classes. Hint: when setting height for table, you should actually increment Leg&#8217;s height</li>
<li>Implement Saving and loading from file or DB. Hint: actually you have to save only 5 parameters &#8211; those passed to table constructor. So when saving and loading Table class with interact with Desk and Leg classes. And when loading &#8211; will create these classes.</li>
</ul>
<h2><a name="expert">Information Expert</a></h2>
<p>The last condition of applying the Creator principle shows, that object A should have all info about B to create it. In other words, A should be <em>Expert</em> in info about B. Information Expert principle helps us assigning all responsibilities in the project. Real-world application usually holds a number of classes, that perform hundreds and thousands of actions, so it is essential to assign responsibilities correctly. The principle itself is fairly simple &#8211; you should give the responsibilities to the object it has information for. For example, assigning Table class responsibility to create Desk is correct because it has info about it. Assigning User responsibility to save data to DB is also correct, because User holds data about user and can perform this task without any other objects (assuming it deals with DB without DB abstraction layer). And assigning User to save information about Group is incorrect, because User will have to &#8220;ask&#8221; Group about it&#8217;s properties &#8211; ID, name, access rights etc. in order to save it correctly. In this case it&#8217;s much better to assign Group responsibility to save itself. And Use can call save() method on Group object.</p>
<p>In the next post I&#8217;ll cover Low Coupling and High Cohesion principles and illustrate it with practical examples. Subscribe to RSS not to miss it!<br />
Feel free to ask any questions in comments or by email: <a href="mailto:konstantin.mirin[at]programmersnotes.info">konstantin.mirin[at]programmersnotes.info</a></p>
<h4>Further reading</h4>
<ul>
<li><a href="http://en.wikipedia.org/wiki/GRASP_(Object_Oriented_Design)">GRASP</a></li>
<li><a href="http://en.wikipedia.org/wiki/Craig_Larman">Larman, Craig</a> (2005). Applying UML and Patterns &#8211; An Introduction to Object-Oriented Analysis and Design and Iterative Development (3rd ed.). Prentice Hall PTR. ISBN 0-13-148906-2.</li>
</ul>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d13').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Liked the post? Bookmark it</em></strong></a>
<br />
<div class="d13" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F28%2Fcreator-and-information-expert-grasp-design-pattern-series%2F&amp;title=Creator+and+Information+Expert+principles+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F28%2Fcreator-and-information-expert-grasp-design-pattern-series%2F&amp;title=Creator+and+Information+Expert+principles+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F28%2Fcreator-and-information-expert-grasp-design-pattern-series%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F28%2Fcreator-and-information-expert-grasp-design-pattern-series%2F&amp;title=Creator+and+Information+Expert+principles+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F28%2Fcreator-and-information-expert-grasp-design-pattern-series%2F&amp;T=Creator+and+Information+Expert+principles+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F28%2Fcreator-and-information-expert-grasp-design-pattern-series%2F&amp;title=Creator+and+Information+Expert+principles+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F28%2Fcreator-and-information-expert-grasp-design-pattern-series%2F&amp;title=Creator+and+Information+Expert+principles+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F28%2Fcreator-and-information-expert-grasp-design-pattern-series%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Creator+and+Information+Expert+principles+%26%238211%3B+GRASP+%28Design+patterns+series%29+@+http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F28%2Fcreator-and-information-expert-grasp-design-pattern-series%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F28%2Fcreator-and-information-expert-grasp-design-pattern-series%2F&amp;t=Creator+and+Information+Expert+principles+%26%238211%3B+GRASP+%28Design+patterns+series%29" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d13').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d13').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Difference between Adapter and Template Method pattern</title>
		<link>http://programmersnotes.info/2009/03/03/difference-between-adapter-and-template-method-pattern/</link>
		<comments>http://programmersnotes.info/2009/03/03/difference-between-adapter-and-template-method-pattern/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 07:19:31 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
				<category><![CDATA[Design patterns]]></category>
		<category><![CDATA[adapter]]></category>
		<category><![CDATA[design pattern]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[template method]]></category>
		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=99</guid>
		<description><![CDATA[The difference between Adapter and Template Method pattern


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<p>When I was studying patterns, I went through main patterns in the alphabetical order. Adapter pattern was in the beginning and Template Method &#8211; in the end. And when I came to the latter and read about it, I was confused. Thee solve the same task, implement the same GRASP principle (Protected Variations) &#8211; they are the same, but why are they called differently?<br />
<span id="more-99"></span><br />
But when I studied them both in more details, it turned out, that there is a significant difference between them. Yes, they solve the same things, but do this differently. To see this, take a look at UML&#8217;s:<br />
<div id="attachment_104" class="wp-caption aligncenter" style="width: 310px"><a href="http://programmersnotes.info/wp-content/uploads/2009/03/adapter1.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/03/adapter-300x191.png" alt="Adapter design pattern" title="Adapter design pattern" width="300" height="191" class="size-medium wp-image-104" /></a><p class="wp-caption-text">Adapter design pattern</p></div><br />
Client calls the <strong>doRoutine()</strong> method of the correspondent adapter. And this adapter calls the specific method of each <strong>Adaptee</strong>. <strong>Adaptee</strong> may be some concrete class or some specific API or anything like this.</p>
<p><div id="attachment_106" class="wp-caption aligncenter" style="width: 310px"><a href="http://programmersnotes.info/wp-content/uploads/2009/03/templatemethod1.png"><img src="http://programmersnotes.info/wp-content/uploads/2009/03/templatemethod-300x206.png" alt="Template Method" title="Template Method" width="300" height="206" class="size-medium wp-image-106" /></a><p class="wp-caption-text">Template Method</p></div><br />
<strong>TemplateMethod()</strong> simply calls operations, that are different from class to class. Derived classes implement these operations (<strong>operation1()</strong>, <strong>operation2()</strong>&#8230;) and, as a result, we get working class with the functionality we need.</p>
<p>You see, using adapter is more like implementing the interface, while using Template Method is about implementing the parts of algorithm differently. So Adapter is used when we need all modules/components to response to the same API. At the same time, we use Template Method when we have some general process, which differs in minor details, which we implement in the derived classes.</p>
<p>For example, we should use Adapter pattern for the DB abstraction layer and TemplateMethod pattern for the calculation of table&#8217;s volume (see <a href="http://programmersnotes.info/2009/02/28/what-is-oop-object-oriented-programming/#improvements">&#8220;What is OOP post&#8221;</a>, I showed this example there)</p>
<h4>Further reading</h4>
<ul>
<li><a target="_blank" href="http://en.wikipedia.org/wiki/Adapter_pattern">Adapter pattern</a></li>
<li><a target="_blank" href="http://en.wikipedia.org/wiki/Template_method_pattern">Template Method pattern</a></li>
</ul>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d99').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Liked the post? Bookmark it</em></strong></a>
<br />
<div class="d99" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F03%2Fdifference-between-adapter-and-template-method-pattern%2F&amp;title=Difference+between+Adapter+and+Template+Method+pattern" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F03%2Fdifference-between-adapter-and-template-method-pattern%2F&amp;title=Difference+between+Adapter+and+Template+Method+pattern" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F03%2Fdifference-between-adapter-and-template-method-pattern%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F03%2Fdifference-between-adapter-and-template-method-pattern%2F&amp;title=Difference+between+Adapter+and+Template+Method+pattern" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F03%2Fdifference-between-adapter-and-template-method-pattern%2F&amp;T=Difference+between+Adapter+and+Template+Method+pattern" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F03%2Fdifference-between-adapter-and-template-method-pattern%2F&amp;title=Difference+between+Adapter+and+Template+Method+pattern" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F03%2Fdifference-between-adapter-and-template-method-pattern%2F&amp;title=Difference+between+Adapter+and+Template+Method+pattern" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F03%2Fdifference-between-adapter-and-template-method-pattern%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Difference+between+Adapter+and+Template+Method+pattern+@+http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F03%2Fdifference-between-adapter-and-template-method-pattern%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F03%2F03%2Fdifference-between-adapter-and-template-method-pattern%2F&amp;t=Difference+between+Adapter+and+Template+Method+pattern" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d99').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d99').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/03/03/difference-between-adapter-and-template-method-pattern/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What is OOP (Object-oriented programming)?</title>
		<link>http://programmersnotes.info/2009/02/28/what-is-oop-object-oriented-programming/</link>
		<comments>http://programmersnotes.info/2009/02/28/what-is-oop-object-oriented-programming/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 14:27:58 +0000</pubDate>
		<dc:creator>Konstantin Mirin</dc:creator>
				<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[basics]]></category>
		<category><![CDATA[concept]]></category>
		<category><![CDATA[encapsulation]]></category>
		<category><![CDATA[inheritance]]></category>
		<category><![CDATA[polymorphism]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[UML]]></category>

		<guid isPermaLink="false">http://programmersnotes.info/?p=25</guid>
		<description><![CDATA[What is object-oriented programming and what's the difference from procedural programming? Why all modern applications use OOP? We'll go through all these terms, we'll demonstrate the difference of these approaches and advantages of the OOP over procedural concept. we'll describe the tree main principles of OOP - encapsulation, polymorphism, inheritance. We'll see what's the difference between class and object, how object is created. We'll learn how to use constructors. Examples in the post are for PHP, but concept is the same in all language.


No related posts.

Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.]]></description>
			<content:encoded><![CDATA[<h2><a name="intro">Introduction</a></h2>
<p><strong>OOP</strong> stands for Object-Oriented Programming. What does it mean? And what&#8217;s the difference between OOP and procedural programming? We&#8217;ll go through all these terms, We&#8217;ll demonstrate the difference of these approaches and advantages of the OOP over procedural concept. Examples are for PHP, but concept is the same in all object-oriented languages.</p>
<p>So, let&#8217;s start with defining, what OOP is and how it differs from procedural programming. Just take a look around. For example, you see a table. Consider you have to program it. When using procedural approach, you focus on it&#8217;s behaviour (for example, you write function, that calculates height or that assembles it from the given dimensions). However, table has properties besides behaviour. When following procedural paradigm, we create variables for each property &#8211; height, width, length, square, weight. All that data &#8211; properties (variables) and behaviour (functions) describe the object from the real world &#8211; table. What&#8217;s wrong with this approach? Nothing wrong, everything is fine, when we&#8217;re dealing with one table.<br />
<span id="more-25"></span></p>
<h2><a name="procedural-programming">Procedural programming &#8211; the problem</a></h2>
<p>And now imagine if we have several table types &#8211; square, triangle and rounded. Consider the following task. We have to calculate square of every table. Let&#8217;s try to do this with procedural approach, without any objects. Each table has only one property, that we&#8217;re interested in &#8211; side for triangle and square and radius for rounded one. Now we&#8217;ll create 3 functions, that calculate the square. (see formulas for <a href="http://mathworld.wolfram.com/EquilateralTriangle.html">equilateral triangle</a> and <a href="http://en.wikipedia.org/wiki/Circle#Area_enclosed">circle</a>)</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">function</span> calcSquare<span class="br0">&#40;</span><span class="re0">$side</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$side</span>*<span class="re0">$side</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> calcTriangle<span class="br0">&#40;</span><span class="re0">$side</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$side</span>*<span class="re0">$side</span>*<span class="nu0">0.433</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">function</span> calcCircle<span class="br0">&#40;</span><span class="re0">$rad</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> M_PI*<span class="re0">$rad</span>*<span class="re0">$rad</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Everything is fine until we have to print list of tables with squares like:</p>
<table border="1" cellspacing="0" cellpadding="3">
<tr>
<td>Table name</td>
<td>Type</td>
<td>Square</td>
</tr>
<tr>
<td>table 1</td>
<td>circle</td>
<td>0.95 m<sup>2</sup></td>
</tr>
<tr>
<td>table 2</td>
<td>triangle</td>
<td>0.78 m<sup>2</sup></td>
</tr>
<tr>
<td>table 3</td>
<td>square</td>
<td>1 m<sup>2</sup></td>
</tr>
<tr>
<td colspan="3" align="center">&#8230;</td>
</tr>
</table>
<p>To process it, we need one more parameter in our array &#8211; the type of the table. And adding it makes array a 2-dimensional one:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$tables</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;circle&#8217;</span>, <span class="nu0">0.7</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;circle&#8217;</span>, <span class="nu0">0.8</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;square&#8217;</span>, <span class="nu0">0.9</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">&#8216;triangle&#8217;</span>, <span class="nu0">1</span><span class="br0">&#41;</span>,</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>And now let&#8217;s program processing of these arrays (using the <a href="http://programmersnotes.info/2009/02/20/nice-trick-with-for-loop/">trick with for loop</a>):</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;&lt;table border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;3&quot;&gt;&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span><span class="re0">$i</span> = <span class="nu0">0</span>, <span class="re0">$s</span> = <a href="http://www.php.net/sizeof"><span class="kw3">sizeof</span></a><span class="br0">&#40;</span><span class="re0">$tables</span><span class="br0">&#41;</span>; <span class="re0">$i</span> &lt; <span class="re0">$s</span>; <span class="re0">$i</span>++<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">switch</span> <span class="br0">&#40;</span><span class="re0">$tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span><span class="br0">&#41;</span><span class="co1">//we have to define which type we&#8217;re dealing with</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">//and call correct function for it</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;circle&#8217;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$square</span> = caclCircle<span class="br0">&#40;</span><span class="re0">$tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;square&#8217;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$square</span> = caclSquare<span class="br0">&#40;</span><span class="re0">$tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">case</span> <span class="st0">&#8216;triangle&#8217;</span>:</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$square</span> = caclTriangle<span class="br0">&#40;</span><span class="re0">$tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">break</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&nbsp; &nbsp; &nbsp; &nbsp; &lt;tr&gt;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="st0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;table &#8216;</span>.<span class="br0">&#40;</span><span class="re0">$i</span><span class="nu0">+1</span><span class="br0">&#41;</span>.<span class="st0">&#8216;&lt;/td&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&#8217;</span>.<span class="re0">$tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>.<span class="st0">&#8216;&lt;/td&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&#8217;</span>.<span class="re0">$square</span>.<span class="st0">&#8216; m&lt;sup&gt;2&lt;/sup&gt;&lt;/td&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/tr&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&#8216;</span>;</div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;&lt;/table&gt;&#8217;</span>;</div>
</li>
</ol>
</div>
<p>Yes! We did it &#8211; created all the stuff we had to and it works fine. But take a look at this. In the array we have to specify the table type, then check it and select the corresponding function. And how about dealing with 10 table types? Adding each type requires adding new calculation function and changing the printing one.</p>
<h2><a name="oop-solution">OOP &#8211; solution</a></h2>
<p>And now we&#8217;ll take a look at the object-oriented solution. Maybe you&#8217;ve heard of the 3 principles of the OO programming &#8211; <strong>Inheritance</strong>, <strong>Encapsulation</strong>, <strong>Polymorphism</strong>. And all of them are used even in this simple example. But before plunging into the OO world, let&#8217;s see, what is object.<br />
<em>When developing, by saying <strong>&#8220;object&#8221;</strong> we mean some abstraction of the real world object <em>(not always real, it may be an abstraction)</em>, that has some attributes (called <strong>properties</strong> or fields) and behaviour or actions (called <strong>methods</strong>).</em><br />
So since object holds both it&#8217;s properties and behaviour, we can define 3 <strong>classes</strong> and use them all. Yes, we mentioned &#8220;classes&#8221;. Classes are &#8220;blueprints&#8221; for objects. Class describes, what properties the object will have, what methods will work with his properties and how. And when we need the object of this class to work with, we instantiate it.</p>
<p>Basically, you can imagine class as an empty pot. It has properties:</p>
<ul>
<li>contents</li>
<li>volume</li>
</ul>
<p>And objects of that class are:</p>
<ul>
<li>Big pot with honey (Contents = &#8220;honey&#8221;, volume=&#8221;big&#8221;)</li>
<li>Little pot with water (Contents = &#8220;water&#8221;, volume=&#8221;little&#8221;)</li>
<li>Medium-sized empty pot (Contents = &#8220;&#8221;, volume=&#8221;medium&#8221;)</li>
</ul>
<p>You also can imagine the process of instantiation of the pot as the sequence of operations:</p>
<ol>
<li>Take blueprint (class definition)</li>
<li>Create pot from clay (allocate memory according to the class definition)</li>
<li>Put something into the pot (Initialize properties with some values)</li>
</ol>
<p>Steps #1 and #2 are done automatically by the programming language compiler (or interpreter), and the third step depends on the:</p>
<ul>
<li><strong>Class definition</strong> &#8211; you can specify default values for the properties</li>
<li><strong>Class constructor</strong> &#8211; you can define special function, that takes care of attributes initialization. We&#8217;ll talk aabout them later.</li>
</ul>
<p>So, now we have theory, let&#8217;s start coding.</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> CircleTable</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$radius</span>;<span class="co1">//defining property</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="co1">//and method</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">radius*</span><span class="re0">$this</span>-&gt;<span class="me1">radius*M_PI</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> SquareTable</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$side</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="re0">$this</span>-&gt;<span class="me1">side</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> TriangleTable</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$side</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="nu0">0.433</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>In PHP, when we define the property without specifying it&#8217;s visibility, it is considered <strong>public</strong>. That means, that we can create an object and access it&#8217;s public property:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$table</span> = <span class="kw2">new</span> SquareTable<span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$table</span>-&gt;<span class="me1">side</span> = <span class="nu0">0.7</span>;</div>
</li>
</ol>
</div>
<p>Other variants are <strong>private</strong> and <strong>protected</strong>. When we define property as private, it can be accessed only from methods of the same class. This is used for some internal variables. Protected is the same as private, but it can be accessed from the child classes also. Don&#8217;t worry if you don&#8217;t understand the concept now, we&#8217;ll go through this later.<br />
<em>Note: In other languages default visibility may be different! (e.g. C++ properties are defined as private by default)</em></p>
<h2><a name="refactoring">Refactoring OO solution</a></h2>
<blockquote cite="http://en.wikipedia.org/wiki/Refactoring"><p><strong>Code refactoring</strong> is the process of changing a computer program&#8217;s internal structure without modifying its external functional behaviour or existing functionality. This is usually done to improve external or internal non-functional properties of the software, such as code readability, simplify code structure, change code to adhere to a given programming paradigm, improve maintainability, improve extensibility, or increase execution performance. <em>(Wikipedia)</em></p></blockquote>
<p>Now let&#8217;s take a close look at these classes. All of them have one parameter and a common method <strong>getSquare()</strong>. We can create the parent object &#8211; <strong>Table</strong> and extend these three classes from it. See the UML diagram:<br />
<img src="http://programmersnotes.info/wp-content/uploads/2009/02/classmodel1.png" alt="Tables class model" title="classmodel" width="442" height="221" class="size-full wp-image-59" /><br />
This should be evident, but we&#8217;ll explain what&#8217;s there. First of all, we have class <strong>Table</strong>. It is a <strong>parent class</strong> for all tables. Then we have three derived classes (arrow points from child to the parent class), which override parent&#8217;s <strong>getSquare()</strong> method.<br />
and the code:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> CircleTable <span class="kw2">extends</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$radius</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">radius*</span><span class="re0">$this</span>-&gt;<span class="me1">radius*M_PI</span>;<span class="co1">//note $this variable. It points to the current object we&#8217;re in.</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> SquareTable <span class="kw2">extends</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$side</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="re0">$this</span>-&gt;<span class="me1">side</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> TriangleTable <span class="kw2">extends</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$side</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="nu0">0.433</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<h2><a name="make-work">Make it work!</a></h2>
<p>So, everything is OK now and we can print the same table using defined classes. Unlike previous approach, we&#8217;ll have a simple array of objects here:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="co1">//creating the objects</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//in real application it is done much more smart</span></div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//we&#8217;ll see how to do it better a bit later</span></div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$tables</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> = <span class="kw2">new</span> CircleTable;</div>
</li>
<li class="li2">
<div class="de2"><span class="re0">$tables</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>-&gt;<span class="me1">radius</span> = <span class="nu0">0.7</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$tables</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span> = <span class="kw2">new</span> SquareTable;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$tables</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>-&gt;<span class="me1">side</span> = <span class="nu0">0.9</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2"><span class="re0">$tables</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span> = <span class="kw2">new</span> TriangleTable;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$tables</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>-&gt;<span class="me1">side</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$tables</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span> = <span class="kw2">new</span> CircleTable;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$tables</span><span class="br0">&#91;</span><span class="nu0">3</span><span class="br0">&#93;</span>-&gt;<span class="me1">radius</span> = <span class="nu0">1</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="co1">//showing table</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;&lt;table border=&quot;1&quot; cellspacing=&quot;0&quot; cellpadding=&quot;2&quot;&gt;&#8217;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw1">for</span> <span class="br0">&#40;</span><span class="re0">$i</span> = <span class="nu0">0</span>, <span class="re0">$s</span> = <a href="http://www.php.net/sizeof"><span class="kw3">sizeof</span></a><span class="br0">&#40;</span><span class="re0">$tables</span><span class="br0">&#41;</span>; <span class="re0">$i</span> &lt; <span class="re0">$s</span>; <span class="re0">$i</span>++<span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&nbsp; &nbsp; &nbsp; &nbsp; &lt;tr&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;table &#8216;</span>.<span class="br0">&#40;</span><span class="re0">$i</span><span class="nu0">+1</span><span class="br0">&#41;</span>.<span class="st0">&#8216;&lt;/td&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&#8217;</span>.<span class="re0">$tables</span><span class="br0">&#91;</span><span class="re0">$i</span><span class="br0">&#93;</span>-&gt;<span class="me1">getSquare</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="st0">&#8216; m&lt;sup&gt;2&lt;/sup&gt;&lt;/td&gt;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="st0">&nbsp; &nbsp; &nbsp; &nbsp; &lt;/tr&gt;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="st0">&#8216;</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;&lt;/table&gt;&#8217;</span>;</div>
</li>
</ol>
</div>
<p>Note, that we don&#8217;t need any function selection, we just call getSquare() for any object. And since all of them have it (that&#8217;s why we defined common parent for them), we get correct result depending on the object we&#8217;re working with.<br />
One more benefit is extending this code. Remember, when using procedural approach, to add new table type we had to add new function and modify the printing function. Using object-oriented approach, all we have to do is to create one more class. The printing function (which may contain complex logic or be hidden from the end-user) remains unchanged. So with OOP we write less and, at the same time, get much better solutions.</p>
<h3><a name="oop-principles">OOP principles explained</a></h3>
<p>This principle, when different actions are performed when calling the same method for different objects, is called <strong>Polymorphism</strong>. <strong>Inheritance</strong> is the simplest thing for understanding &#8211; we used this to inherit (programmers usually say &#8220;extend&#8221;) three table classes from the one generic table class.</p>
<p>Finally, <strong>encapsulation</strong> is about hiding implementation details from the end-used. Let&#8217;s take our example; imagine that there are 2 persons. One of them is excellent mathematician and he created that 4 classed (base class &#8211; Table and 3 derived (child) classes). Other should show that list. With procedural approach he should take a look at the procedures and write code, that determines the object type and calls correct function. With OOA (object-oriented approach) he doesn&#8217;t have to know that. He just calls <strong>getSquare()</strong> and doesn&#8217;t care about anything.</p>
<h2><a name="improvements">Further improvements</a></h2>
<p>To make these principles more understandable, let&#8217;s improve our code and add some data to the objects. Let&#8217;s store height of the table and then print volume, that it occupies. <u>Volume=Square*Height</u>. Square is calculated differently, formula for volume is the same. So we add new property to the parent class &#8211; <strong>$height</strong>. And new method &#8211; <strong>getVolume()</strong>. Note, we don&#8217;t change any child class at all!<br />
<img src="http://programmersnotes.info/wp-content/uploads/2009/02/classmodel21.png" alt="Revised class model" title="classmodel2" width="442" height="258" class="size-full wp-image-65" /></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$height</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getVolume<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">height</span> * <span class="re0">$this</span>-&gt;<span class="me1">getSquare</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>And here is how it works:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="re0">$table</span> = <span class="kw2">new</span> CircleTable;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$table</span>-&gt;<span class="me1">radius</span> = <span class="nu0">0.7</span>;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$table</span>-&gt;<span class="me1">height</span> = <span class="nu0">0.9</span>;</div>
</li>
<li class="li1">
<div class="de1"><a href="http://www.php.net/echo"><span class="kw3">echo</span></a> <span class="st0">&#8216;Table square = &#8216;</span>.<span class="re0">$table</span>-&gt;<span class="me1">getSquare</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="st0">&#8216;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="st0">Table volume = &#8216;</span>.<span class="re0">$table</span>-&gt;<span class="me1">getVolume</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
</ol>
</div>
<p>Note, that we didn&#8217;t define $height or method <strong>getVolume()</strong> in <strong>CircleTable</strong> class, this is inherited from parent class. Don&#8217;t you think it&#8217;s very convenient? <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>By the way, we used <strong>design pattern</strong> here, which is called <a href="http://en.wikipedia.org/wiki/Template_method_pattern">TemplateMethod</a> &#8211; we defined method <strong>getVolume()</strong>, which uses method <strong>getSquare()</strong>. And <strong>getSquare()</strong> itself is defined in the derived classes. Read more about this pattern in my future posts <img src='http://programmersnotes.info/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2><a name="constructors">Using constructors</a></h2>
<p>Now let&#8217;s do some more improvements &#8211; we&#8217;ll create <strong>constructors</strong> for the classes. Constructor is special function, that is called when object is created. We&#8217;ve already mentioned them. In PHP 5 constructor is defined as a special function, named __construct. It is a regular method, but it can&#8217;t return any value. However, it can be public, private or protected. Usually, constructors are private unless some design pattern is used (e.g. <a href="http://en.wikipedia.org/wiki/Singleton_pattern">Singleton</a>). Here is the code, that uses constructors for smarter object initialization:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$height</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">height</span> = <span class="re0">$h</span>;<span class="co1">//setting the height</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getVolume<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">height</span> * <span class="re0">$this</span>-&gt;<span class="me1">getSquare</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="kw2">class</span> CircleTable <span class="kw2">extends</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$radius</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$r</span>, <span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::__construct<span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span>;<span class="co1">//calling the constructor of the parent class, passing height there.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">radius</span> = <span class="re0">$r</span>;<span class="co1">//setting the radius</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">radius*</span><span class="re0">$this</span>-&gt;<span class="me1">radius*M_PI</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> SquareTable <span class="kw2">extends</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$side</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$s</span>, <span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::__construct<span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span>;<span class="co1">//calling the constructor of the parent class, passing height there.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">side</span> = <span class="re0">$s</span>;<span class="co1">//setting the side</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="re0">$this</span>-&gt;<span class="me1">side</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="kw2">class</span> TriangleTable <span class="kw2">extends</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$side</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$s</span>, <span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent::__construct<span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span>;<span class="co1">//calling the constructor of the parent class, passing height there.</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">side</span> = <span class="re0">$s</span>;<span class="co1">//setting the side</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="re0">$this</span>-&gt;<span class="me1">side*</span><span class="nu0">0.433</span>;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Here is how to create such classes:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li1">
<div class="de1"><span class="re0">$table</span> = <span class="kw2">new</span> CircleTable<span class="br0">&#40;</span><span class="nu0">0.7</span>, <span class="nu0">0.9</span><span class="br0">&#41;</span>;<span class="co1">//passing radius and height to the constructor</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
</ol>
</div>
<p>The following usage is the same, it just helps to initialize object attributes in one line of code, encapsulating some preprocessing. For example, we know that height can&#8217;t be negative, so we can correct definition of the Table class:</p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="re0">$height</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$h</span> &lt;= <span class="nu0">0</span><span class="br0">&#41;</span> <span class="re0">$h</span> = <span class="nu0">0</span>; <span class="co1">//restrict negative values</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">height</span> = <span class="re0">$h</span>;<span class="co1">//setting the height</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getVolume<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">height</span> * <span class="re0">$this</span>-&gt;<span class="me1">getSquare</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>So you can see, using constructors is VERY handy.<br />
Note, if you have the properties public (like in this example), you may still use <strong>$table->radius</strong> to access it.</p>
<h2><a name="access-levels">Attributes and methods access</a></h2>
<p>Finally, let&#8217;s take a look at the properties and methods visibility. As we already mentioned, there are three access levels:</p>
<ol>
<li><strong>Public</strong> &#8211; accessible both from class itself ($this->property) and from outside ($user->login)</li>
<li><strong>Protected</strong> &#8211; accessible from the class itself and from it&#8217;s derived classes ($this->property, property may be defined in current class or in the parent one)</li>
<li><strong>Private</strong> &#8211; accessible only from the class itself ($this->property, property should be defined in the current class)</li>
</ol>
<p>To demonstrate this in action, we&#8217;ll modify the Table class and make it&#8217;s $height property private. Since it is used only in getVolume() method, which is also defined in the Table class, it should work fine. Also, we add a new method setHeight(). This will be used to modify height since we&#8217;ll not have access to it through $table->height. So here is UML and the code:<br />
<img src="http://programmersnotes.info/wp-content/uploads/2009/02/table1.png" alt="Final Table class" title="table" width="186" height="174" class="size-medium wp-image-63" /></p>
<div class="dean_ch" style="white-space: wrap;">
<ol>
<li class="li1">
<div class="de1"><span class="kw2">class</span> Table</div>
</li>
<li class="li1">
<div class="de1"><span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">private</span> <span class="re0">$height</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> __construct<span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">setHeight</span><span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp;</div>
</li>
<li class="li2">
<div class="de2">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> setHeight<span class="br0">&#40;</span><span class="re0">$h</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$h</span> &lt;= <span class="nu0">0</span><span class="br0">&#41;</span> <span class="re0">$h</span> = <span class="nu0">0</span>; <span class="co1">//restrict negative values</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;<span class="me1">height</span> = <span class="re0">$h</span>;<span class="co1">//setting the height</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getSquare<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="nu0">0</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2">&nbsp;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw2">public</span> <span class="kw2">function</span> getVolume<span class="br0">&#40;</span><span class="br0">&#41;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span></div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;<span class="me1">height</span> * <span class="re0">$this</span>-&gt;<span class="me1">getSquare</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;</div>
</li>
<li class="li1">
<div class="de1">&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span></div>
</li>
<li class="li2">
<div class="de2"><span class="br0">&#125;</span></div>
</li>
</ol>
</div>
<p>Note, that we changed constructor, not it doesn&#8217;t set the height itself, it delegates this to the special setter function, which perform all validation. This is a good practice &#8211; to have attributes of an object hidden and have getter/setter methods to access it. Why? Because this way you control, what is returned and what is set and can ensure that object&#8217;s properties are always correct.<br />
When using this definition, we&#8217;ll not be able to access $height in CircleTable or TriangleTable. To enable this, we can either add <strong>getter</strong> method or make $height protected. First variant will allow everybody to get the height. Second variant allows to get the $height only within the derived classes (see protected access level above)</p>
<h4>Glossary</h4>
<ul>
<li><strong>Class</strong> is a formal definition of some real object or it&#8217;s abstraction. It contains definition of it&#8217;s both properties and methods.</li>
<li><strong>Object</strong> is an instantiated class</li>
<li><strong>Property</strong> is a special variable, that represents some attribute of an object. Sometimes it is called &#8220;member variable&#8221;</li>
<li><strong>Method</strong> is a special function, that describes some behaviour of an object.</li>
<li><strong>Constructor</strong> is a special method, that is called when object is created. This is mainly used for the object initialization.</li>
<li><strong>Getter</strong> is a special method, that returns property value. Usually used for private or protected properties or for some kind of processing before returning.</li>
<li><strong>Setter</strong> is a special method, that sets property value. Used for private or protected variables or for some additional validation.</li>
<li><strong>Superclass</strong> is same as parent class or base class</li>
<li><strong>Subclass</strong> is same as derived class, child class, extended class</li>
<li><strong>Inheritance</strong> is an OOP principle, which is used to define some common behaviour in the base class and then extend it and use in the derived classes.</li>
<li><strong>Encapsulation</strong> is an OOP principle, which used for hiding implementation details from the end-user. (remember example with two programmers developing these table classes)</li>
<li><strong>Polymorphysm</strong> in an OOP principle, which allows different classes to behave differently when calling the same method.</li>
<li><strong>Design pattern</strong> is some standard proven solution to the common problem. For example, Singleton pattern solves the problem when we need some common resource in different places of an application. For example DB abstraction object.</li>
<li><strong>Refactoring</strong> is the process of changing the source code to make it work faster, look better, use better solutions, be better in support and future development</li>
</ul>
<h4>Further reading</h4>
<ul>
<li><a href="http://programmersnotes.info/2009/03/28/creator-and-information-expert-grasp-design-pattern-series/">Creator and Information Expert GRASP principles</a></li>
<li><a href="http://en.wikipedia.org/wiki/Object-oriented_programming">OOP</a></li>
<li><a href="http://en.wikipedia.org/wiki/Procedural_programming">Procedural programming</a></li>
<li><a href="http://en.wikipedia.org/wiki/Constructor_(computer_science)">Constructor</a></li>
<li><a href="http://en.wikipedia.org/wiki/Singleton_pattern">Design patterns: Singleton</a></li>
<li><a href="http://en.wikipedia.org/wiki/Template_method_pattern">Design patterns: Template method</a></li>
<li><a href="http://en.wikipedia.org/wiki/Equilateral_Triangle">Equilateral triangle</a></li>
<li><a href="http://en.wikipedia.org/wiki/Circle#Area_enclosed">Circle</a></li>
</ul>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d25').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Liked the post? Bookmark it</em></strong></a>
<br />
<div class="d25" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F02%2F28%2Fwhat-is-oop-object-oriented-programming%2F&amp;title=What+is+OOP+%28Object-oriented+programming%29%3F" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F02%2F28%2Fwhat-is-oop-object-oriented-programming%2F&amp;title=What+is+OOP+%28Object-oriented+programming%29%3F" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F02%2F28%2Fwhat-is-oop-object-oriented-programming%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F02%2F28%2Fwhat-is-oop-object-oriented-programming%2F&amp;title=What+is+OOP+%28Object-oriented+programming%29%3F" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.netscape.com/submit/?U=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F02%2F28%2Fwhat-is-oop-object-oriented-programming%2F&amp;T=What+is+OOP+%28Object-oriented+programming%29%3F" rel="nofollow" title="Add to&nbsp;Netscape"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/netscape.png" title="Add to&nbsp;Netscape" alt="Add to&nbsp;Netscape" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F02%2F28%2Fwhat-is-oop-object-oriented-programming%2F&amp;title=What+is+OOP+%28Object-oriented+programming%29%3F" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F02%2F28%2Fwhat-is-oop-object-oriented-programming%2F&amp;title=What+is+OOP+%28Object-oriented+programming%29%3F" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F02%2F28%2Fwhat-is-oop-object-oriented-programming%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+What+is+OOP+%28Object-oriented+programming%29%3F+@+http%3A%2F%2Fprogrammersnotes.info%2F2009%2F02%2F28%2Fwhat-is-oop-object-oriented-programming%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://myweb2.search.yahoo.com/myresults/bookmarklet?u=http%3A%2F%2Fprogrammersnotes.info%2F2009%2F02%2F28%2Fwhat-is-oop-object-oriented-programming%2F&amp;t=What+is+OOP+%28Object-oriented+programming%29%3F" rel="nofollow" title="Add to&nbsp;Yahoo My Web"><img class="social_img" src="http://programmersnotes.info/wp-content/plugins/social-bookmarks/images/yahoo.png" title="Add to&nbsp;Yahoo My Web" alt="Add to&nbsp;Yahoo My Web" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d25').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d25').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>

<p>No related posts.</p>
<p>Related posts brought to you by <a href='http://yarpp.org'>Yet Another Related Posts Plugin</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://programmersnotes.info/2009/02/28/what-is-oop-object-oriented-programming/feed/</wfw:commentRss>
		<slash:comments>37</slash:comments>
		</item>
	</channel>
</rss>

