| By Kola Oyedeji | Article Rating: |
|
| May 13, 2007 01:45 PM EDT | Reads: |
9,719 |
In these examples, the Person component "me" is returning the address component on which we then call the getPostCode function. The problem with this is that we have now coupled this code to two components. Say, for example, the address is now stored internally in a structure, not a component, and the getAddress function returns a structure instead of an Address object, we have to change this code. Similarly, if the address component method changes, perhaps the name of the getPostCode method is changed or the getPostCode method is removed completely, again we have to change this code.
Of course, changes to your public API will impact the code that uses it, but taking the time to think through your public API and how functionality is exposed will pay dividends down the road. Encapsulating the internals of your components prevents other code from being written based on these internals. This frees you to change the implementation of such components without breaking code that uses your component (provided the arguments and return values are the same). This also is the reason it's often recommended to not expose variables in the THIS scope.
Applying the Law of Demeter, instead of directly referencing the address component returned from the Person CFC, we add the following function to the Person component:
<cffunction name="getPostCode" returns="string" output="false" >
<cfreturn variables.personsAddressCFC.getPostCode() />
</cffunction>
We then change our calling code to call this new method, which is really nothing more than a wrapper method delegating the call to the actual address component held in the variables scope of the component.
<cfset someGuy = createObject("component","Person") />
<cfset pc = someGuy.getPostCode() />
Now if we change how the address is stored in the person CFC, if we change it to a structure - and change the person getPostCode function:
<cffunction name="getPostCode" returns="string" output="false" >
<cfreturn variables.personsAddressStruct.postCode />
</cffunction>
As you can see, following the Law of Demeter now shields the calling code from changes to the Address CFC. The address may be internally stored as a CFC, a struct, or a Java class, but the type makes no difference to the code calling the getPostCode function. As long as a string is still returned from the function as expected, this shields calling code from the impact of changes. We have minimized the scope of what needs to be changed so that code that now simply needs to get the postcode is not impacted by changes to the address component.
As with any design decision, there is a trade-off. One disadvantage of strictly applying the Law of Demeter is that you can end up with CFCs that contain many functions that do nothing more than delegate calls to other CFCs.
I hope that through these examples you can see that the Law of Demeter can help reduce the amount of dependencies between components in your applications. This ultimately leads to easier application maintenance.
References
• www.ccs.neu.edu/research/demeter/papers/law-of-demeter/oopsla88-law-of-demeter.pdf
• www.cmcrossroads.com/bradapp/docs/demeter-intro.html
Published May 13, 2007 Reads 9,719
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Kola Oyedeji
Kola Oyedeji is technical director of Vivid Lime, a London-based full services agency. Prior to that, he was a senior software developer for the Collinson Group, developing Enterprise Loyalty and Insurance Systems. Kola holds a BSc in Computer and Information systems. His blog is available at www.coolskool.blog-city.com.
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Adobe LiveCycle Enterprise Suite 2 for Cloud Computing
- Adobe Betas Target RIAs and Cloud Computing
- Adobe Cans Another 9% of its Workforce
- Moyea DVD4Web Converter V2.0 Converts DVD to FLV Fast and Synchronously with Watermarks
- Adobe Fiddles with its Web Apps
- Adobe & Salesforce Cut Cloud Deal
- Hosting.com Launches ColdFusion 9 in the Cloud
- The Real Time Infrastructure Ultimatum
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Is Microsoft as Free as Open Source?
- Adobe Reader Sued
- The Planet Named “Bronze Sponsor” of Cloud Computing Expo
- Microsoft Expression Web Has Got Game
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Adobe Flex Developer Earns $100K in New York City
- Bruce Chizen Joins Voyager Capital as Venture Partner
- My Top Seven Wishes From Adobe MAX 2009
- The Next Programming Models, RIAs and Composite Applications
- Where Are RIA Technologies Headed in 2008?
- Constructing an Application with Flash Forms from the Ground Up
- AJAX World RIA Conference & Expo Kicks Off in New York City
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Personal Branding Checklist
- Adobe Flex 2: Advanced DataGrid
- Has the Technology Bounceback Begun?
- Building a Zip Code Proximity Search with ColdFusion
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers
- The Asynchronous CFML Gateway
- Web Services Using ColdFusion and Apache CXF
























