0

In a MVC architecture the model is used to store data used in the application. I create a class and use some static properties to store data so that it can be used throughout the application. For example if there is a datagrid, I use a static arraycollection in model class. Update it whenever a service is called and by using databinding update the dataprovider of the datagrid. I have read that singleton class can also be used to accomplish this task. Is the way I use for model class is better or there exists some other ways too?

nitin
  • 75
  • 1
  • 12

2 Answers2

1

As the discussion Tianshen referred to points out, there is hardly any difference between a bunch of static variables and a Singleton. Both can be seen as a form of global variables, with all the accompanying downsides. I'm not going to argue about how Singletons are evil here; plenty has been written about the subject already that you can easily find on the web.

However, I would like to present an alternative: Inversion of Control (IoC), also referred to as Dependency Injection (DI). I'll explain this pattern in short, but you can also find plenty of information for yourself. Take the ArrayCollection from your example; if you'd want to avoid the static variables or the Singleton pattern, you would have to create one instance and pass that instance around from object to object throughout your application and perhaps it would even have to be passed through an object that doesn't really need it, which wouldn't be very clean either.
In comes the IoC container (for a Flex app it would take the form of library you add to your project): with such a library, you can create/configure that ArrayCollection in one place and let the IoC "inject" that single instance in whatever class that needs it.

A concrete example: we might have a configuration file like this

<fx:Object>
    <s:ArrayCollection id="myLetters">
        <fx:String>A</String>
        <fx:String>B</String>
    </s:ArrayCollection>
</fx:Object>

and a class like this

public class MyClass {

    [Inject(id="myLetters")]
    public var letters:IList;

}

The IoC container would then inject the myLetters ArrayCollection instance whenever a MyClass would be instantiated. There are a lot of other injection techniques, but this example is just to give you an idea.

At the time of this writing I belive Parsley to be the most widely used IoC container for Flex.

RIAstar
  • 11,912
  • 20
  • 37
  • I agree that using Parsley is a much much more elegant solution, but it has a bit of a learning curve for many who are unfamiliar with DI/IoC principal or don't yet know the necessity. – Tianzhen Lin Jul 11 '12 at 02:24
  • @TianzhenLin I know that Parsley is a lot more than just an IoC container, but if you only use that particular feature, I don't think is that hard. Once you get your head around the concept, it's actually easier to set up an architecture because you can move classes around much more easily. – RIAstar Jul 11 '12 at 08:31
  • I am in the process of moving from Cairngorm to Parsley, the learning curve and the catches in wiring are well worth the effort to have an application further decoupled, especially for a large application with hundreds of components and classes. But for most Flex or UI development beginners, Parsley and Cairngorm may intimidate and scare them away. – Tianzhen Lin Jul 11 '12 at 17:11
  • @TianzhenLin The fact is: I don't like any of these frameworks and I absolutely hate Cairngorm. If you want to make an application 10x bigger than it should be and 10x more complicated, by all means use Cairngorm. IMO Flex _is_ an MVC framework already (especially Flex4) and hardly needs any third party help to get the job done. Just use some good coding conventions and you'll go a long way. The one thing I really wish for, is a lightweight IoC container that does one thing and one thing only. If you ever come across such a library, please let me know. I'm keeping my eyes on RobotLegs2 for now – RIAstar Jul 11 '12 at 17:27
0

You may read the this discussion that compares Singleton and Static. Though it is in C#, the similar Object-Oriented philosophies apply.

In short, Singleton gives you better flexibility. If you use Databinding for your Flex application, using Singleton would allow you to inherit EventDispatcher, with which you can dispatch custom change events. Custom change events would give your application better performance.

Community
  • 1
  • 1
Tianzhen Lin
  • 2,404
  • 1
  • 19
  • 19