12-04-2010, 07:44 PM
|
#1
|
|
newbe forever!
Join Date: Oct 2004
Location: Madrid, Spain
Posts: 88
|
Constructor functions are cluster bombs
I would like to know your opinion on what Bill Sanders, one of the co-authors of "ActionScript 3 Design Patterns" says about not using constructors at all, here is asummary of his original post Constructors functions are Cluster bombs:
* "If you do not include one, you’re not tempted to add properties and methods in the constructor that might generate dependencies. Your client isn’t a problem because its just making requests and none of the other classes instantiate your client."
* "With “parameterized” classes, instead of passing a parameter to the class, it’s less binding to pass it just to the method that uses it."
* "If everything in a class is needed immediately by the constructed instance, first instantiate an instance and then call a method through the instance that fires off the whole crew."
Personally, I am starting to think he is right. Hey! He (among others) wrote the most interesting book I've red about AS3. In fact I think I will read it again just when I finish it. But as with the book, the concepts related loose-coupling between classes, are a litle difficult to grasp for me. Yet
__________________
glantucan
|
|
|
12-05-2010, 12:45 PM
|
#2
|
|
Flexpert
Join Date: Sep 2006
Location: Seattle, WA: USA
Posts: 1,564
|
Yeah, I would agree that constructors are bad for some classes (especially view classes). They're fine for simple things like a model, or a custom event. However, view classes usually get created in stages, or as part of a life-cycle such as the case with Flex. Lots of logic in the constructor also makes them harder to extend. In general, it's usually best to break up functionality into as many chunks as possible, which is not just limited to the constructor. For exmple, you may have an init function which just bootstraps more specific functions like initStyles(), initModel(), etc.
|
|
|
12-05-2010, 04:11 PM
|
#3
|
|
newbe forever!
Join Date: Oct 2004
Location: Madrid, Spain
Posts: 88
|
Yup, I understand that,... I think
But what I don't get very well, hoaw could you not use constructors at all. In a MVC pattern you have, at least, to tell the Views which controllers and models to use, and I don't see how they are less coupled with them by doing it through another method instead of the constructor.
__________________
glantucan
|
|
|
12-05-2010, 04:30 PM
|
#4
|
|
Senior Member
Join Date: May 2003
Location: berlin | germany
Posts: 944
|
Quote:
|
..at least, to tell the Views which controllers and models to use..
|
this could be done by e.g. programming to interfaces like..
ActionScript Code:
package
{
public interface IAbstractView
{
function setModel (model:IAbstractModel);
function getModel () :IAbstractModel;
function setController (): IAbstractController;
function getController (): IAbstractController;
}
}
if any view is implementing this interface you don't have to deal with the constructor..
please note, this is just an 'dirty example' but should point you in the direction..
|
|
|
12-05-2010, 05:07 PM
|
#5
|
|
newbe forever!
Join Date: Oct 2004
Location: Madrid, Spain
Posts: 88
|
Ok, I see. That way you can change models and controllers even at runtime if they implement IAbstractModel and IAbstractController.
But if you include this methods while initializing them in the constructor, why are the classes more coupled. Still don't see it. Perhaps I didn't get the "coupling" thing at all.
For instance
ActionScript Code:
public class myView
{
public function myView(model:IAbstractModel, controller:IAbstractController)
{
...}
function setModel (model:IAbstractModel):void{
...}
function getModel () :IAbstractModel{
...}
function setController (controller:IAbstractController):void{
...}
function getController (): IAbstractController{
...}
...
}
Oh!, maybe... without initialization that class could work without the need of models and controllers instantiated. Is that the reason?
Tanks guys, I'm learning a lot
__________________
glantucan
|
|
|
01-10-2011, 10:05 PM
|
#6
|
|
rather be programming
Join Date: Feb 2005
Location: City of Angels
Posts: 10,141
|
I've read some stuff before by that guy and I kind of thought he's not the greatest person to be giving advice on design patterns or even programming. Take it with a metric ton of salt....
There are definitely occasions where constructors are needed such as adapters and commands to name a quick one.
__________________
trace("Good bye Flash.") Log.i(TAG, "Hello Droid");
Last edited by Flash Gordon; 01-11-2011 at 04:28 AM.
|
|
|
01-11-2011, 01:26 AM
|
#7
|
|
Obfuscated Coder
Join Date: Apr 2008
Posts: 1,571
|
Quote:
Originally Posted by Flash Gordon
Take it with a metric ton of salt....
|
I would agree with the grain of salt appraisal. Basic operator precedence isn't something you should be surprised by after so many years.
Last edited by maskedMan; 01-11-2011 at 02:36 PM.
|
|
|
01-11-2011, 04:25 AM
|
#8
|
|
rather be programming
Join Date: Feb 2005
Location: City of Angels
Posts: 10,141
|
you're right, harsh words. Allow me to edit them and you may as well.
__________________
trace("Good bye Flash.") Log.i(TAG, "Hello Droid");
|
|
|
01-11-2011, 12:10 PM
|
#9
|
|
dondeEstanMisPantalones?
Join Date: Nov 2005
Location: New York Proper
Posts: 1,355
|
well based on that "operator precedence" post, i'm inclined to agree with calling him an idiot, but that doesn't change the fact that i can't think of one decent reason you should ever need a constructor--architecturally or otherwise.
|
|
|
01-11-2011, 02:36 PM
|
#10
|
|
Obfuscated Coder
Join Date: Apr 2008
Posts: 1,571
|
Quote:
Originally Posted by Flash Gordon
you're right, harsh words. Allow me to edit them and you may as well.
|
No problem.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 10:17 AM.
///
|
|