|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Feature ColdFusion Feature — Objects Everywhere
A solution to object-oriented spaghetti code
By: Jeff Chastain
May. 15, 2007 06:15 PM
Many times object-oriented programming (OOP) is billed as the end-all solution to cure the spaghetti code that can come from procedural style applications. After all, you just have to stuff your logic code into a component (big OOP buzzword - encapsulation), and now your code is instantly better, right? How hard is it to stick a createObject function call or a <cfobject> tag in when you need to access that bit of code? Can anybody look into the future and see a problem here?
<cfset oCar = createObject('component', 'com.mydomain.Car').init() /> Nice and easy, right? However, our application is going to get a bit more complex. Our car is not going to do much without an engine and a transmission. An engine and a transmission are not a simple property though; these are also objects with their own properties. In order to build our model, we need a couple more lines of code.
<cfset oEngine = createObject('component', 'com.mydomain.Engine').init() /> Still, not too bad. But what happens when the business model continues to grow? None of us have ever experienced anything called scope creep, right?
The Problem Add in the difficulty of a component path changing or a new dependency being added to the car object and all of a sudden we have spaghetti code all over again. So much for OOP being the solution to all the world's (or at least software developments) problems!
Introducing Design Patterns
Today, we are going to look at the Factory Pattern, which will hopefully help clean up some of this mess we're in. In the real world, what does a factory do? It makes things, right? Think of the GM Bowling Green Assembly Plant. At one end you say "I want a new red Corvette," and at the other end of the factory, out comes a new Corvette (a new instance). I don't know anything about creating engines or transmissions. All I did was ask the factory for a new Corvette. Somewhere within the factory is the knowledge of what exactly makes up a Corvette and where to find all of the parts. The factory takes care of building the car without my having any knowledge of these specific details. Wait, factories don't have to be this specialized. In fact, I can go to the GM Bowling Green Assembly Plant and ask for a new blue Cadillac XLR. Once again, my order goes in one end and out pops a car from the other end without my having any knowledge of exactly how the car was built. This one factory builds multiple kinds of cars, possibly using some of the same parts, but all of that implementation is hidden. How does this apply to object-oriented development?
The Object Factory Reader Feedback: Page 1 of 1
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
|||||||||||||||||||||||||||||||||||||||||||||||||