|By Steve Parks||
|November 19, 2002 12:00 AM EST||
In computer terms, objects are abstracts of real-world entities that are all around us in everyday life. Examples of such objects could be people, organizations, cars, animals, or even less tangible entities such as math or time.
Of these examples, only the first four can be considered collections. One or more persons is a collection of people; one or more organizations is a collection of organizations, and so on. However, math is not a collection of math and time is not a collection of time. They are just simple objects with methods and properties. Simply defined, a collection is a set of related objects.
Collections come with many advantages. First, they are objects, so any and all advantages of objects, such as encapsulation and reusability, are inherent to the collection. Second, you can access a group of objects easily by looping through the collection and executing the same code against each object in the collection. Finally, you can simplify a lot of the external data manipulation by using collections. For instance, instead of returning a query containing users (which might change in six months) you could use a Users collection. The User object that is stored in the Users collection would encapsulate all the code that goes into retrieving the user data. That way, when the database changes, you can change your User object while your collection and the code accessing the collection remain the same.
What Makes a Collection?
There are three hard and fast rules that an object (or in ColdFusion, a component) has to adhere to in order to be a collection:
1. Every object in the collection can be accessed individually. Each object in the collection is unique in instance, if not data. If there are five objects in the collection, you should be able to call properties and methods from each object.
2. Every object in the collection is the same type of object and has the same properties and methods. This means that you wouldn't have a collection called People with two types of objects: People and Computers. You would, in fact, have two collections, the People collection and the Computers collection.
3. A collection usually has at least four methods: Add, Remove, Count, and Item.
Most collections can be completely accessed through the use of just four methods:
Creating the Collection Files
In order to create a collection we need to create two files, both of which are CFCs. The first CFC we'll create will be named ActiveUsers.cfc; the second will be named ActiveUser.cfc. The ActiveUser.cfc file will be the actual object that is contained in our collection. The collection filename is usually the plural term for the name of our object, hence, ActiveUser object and ActiveUsers collection. The ActiveUser.cfc file is shown in Listing 1.
The ActiveUsers collection will have four methods: Add(strFirstName, strLastName), Remove(index), Item(index), and Count(). Our ActiveUser object will contain five methods: GetName(), SetName(strFirstName, strLastName), SetLoginTime(), GetLoginTime(), and GetMinutesActive().
Our ActiveUsers collection will be used to display only users who are currently active on our Web site, similar to a "who's on" interface. Anytime you access a variable in a shared scope, such as the Application scope, you should lock it using CFLOCK.
The first step is to set up a data structure to store our data in the ActiveUsers collection. We need only one private (or member) variable called arItems, which is an array. You can add that code just inside of your CFCOMPONENT tag as shown below.
<CFSET arItems = ArrayNew(1)>
Next we're going to add four methods to the component. The first method is the Add method. The Add method will take two arguments, FirstName and LastName, both string literals. The Add function simply adds an element to the private variable arItems using the intrinsic function, ArrayAppend(). The tricky part is that our Add method needs to add a value that represents an instance of an object. Since we can't use the CFOBJECT tag inside the ArrayAppend() function, we will use the CreateObject() function, shown below. After we add that element to the array, we also need to get the length of the array so we know the index of our recently added user. Again, locking is important here, because we want to add an element to the array and then get the length of the array before anyone else adds another element to it. This is sometimes called a race or race to commit, in database terminology. If we don't lock the actions, we will be racing against other requests to add elements to the array, potentially returning the wrong values to the calling page.
<CFSET ArrayAppend(arItems, CreateObject("component","ActiveUser")>
<CFSET idx = ArrayLen(arItems)>
The next two lines of code in our function will be calling methods from the prebuilt ActiveUser object. These methods set three internal variables in the ActiveUser object: the user's first name, the user's last name, and the date the user logged in.
The last thing we do is return the current element of the array for the programmer to use in the calling page, if necessary. That was the hard part. The next three functions are short and sweet, so I'm going to spend more time describing why you need to do this, rather than stepping through the code line by line. Listing 2 shows the code for the entire collection object.
With the Remove method, we require one argument that is an integer representing the index of the internal array, arItems. We need to check to make sure the value passed is less than or equal to the length of the array so there aren't any errors produced, and then we simply remove that element using the ArrayDeleteAt() function. Remember, you'll need to lock this action to prevent the wrong index of the array from being deleted. In a situation where the method is called twice, there will be a race to commit the deletion. In the event the deletion was not locked, the array would be resized after the first deletion and then the wrong index could potentially be deleted during the second deletion.
The Item method also requires an index value, but the result is different. Instead of removing an item (or doing any type of action), we're just going to return the instance of the ActiveUser object that was stored at that index. This allows us to access each individual object from external ColdFusion templates easily, as you'll see later.
The Count method accepts no arguments and returns an integer describing the number of objects in our collection. We use the ArrayLen() function to determine the return value.
That's it for our collection. All of the internal code is written for our ActiveUsers collection to work properly. It's relatively easy to generate code for collections once you understand the concepts. Now we're going to look at how to use collections in ColdFusion templates.
Making the Collection Persist
In order for our "who's on" application to persist, we need to set up the ActiveUsers collection in the Application scope. Open the Application.cfm file in Listing 3 and add the following code:
<CFIF NOT IsDefined("Application.objActiveUsers")>
<CFOBJECT NAME="Application.objActiveUsers" COMPONENT="ActiveUsers">
This code basically says to check whether the Application.objActiveUsers collection is defined at the beginning of every request. If it isn't defined, it will create the collection using the CFOBJECT tag.
Accessing the Collection
In the index.cfm file (see Listing 4) we're going to output the current number of users online and then loop through the ActiveUsers collection and output data pertaining to the users that are online. You can do all this using the code below:
<CFSET iCount = Application.objActiveUsers.Count()>
As you'll see, we've set the iCount variable to the value of the ActiveUsers collection's Count method. This lets us know how many active users are online right now. Now we can loop through the collection using a simple FROM/TO (or FOR) loop where the TO attribute is equal to the number of users online, or iCount.
We can access the individual objects in the collection using the Item(index) property such as Application.objActiveUsers.Item(i). In the code below, we use that notation to call different methods of the object, such as GetName(), GetLoginTime(), and GetMinutesActive().
<CFLOOP FROM="1" TO="#iCount#" INDEX="i">
<CFSET stName = Application.objActive
#i# - #stName["FirstName"]#
#TimeFormat(Application.objActiveUsers.Item(i).GetLoginTime(), 'h:mm tt')# -
Running index.cfm at this point in time would result in the printing of "Number of Users: 0", since we haven't added any users to the collection. In order to add users to the collection we have to call the Add() method of the ActiveUsers collection. In this example, we'll make a ColdFusion page called AddUser.cfm (see Listing 5) that requires two URL variables called FirstName and LastName. The following code is all we'll use for our AddUser.cfm file:
<CFSET objActiveUser = Application.objActive
If we access the AddUser.cfm page passing "Steve" and "Parks" through the query string such as
our page will then add the user to the collection and refresh us to the index.cfm page. You should now see that there is one user named Steve Parks in the collection. If your browser caches the index.cfm page, you may have to manually refresh the page. In the index.cfm file we called the GetName(), GetLoginTime(), and GetMinutesActive() methods from the ActiveUser object in the collection. You'll now see that data outputted through the collection. Try it again, adding another user using your name. You should now see that you are in the collection as well.
The last file that needs to be created is the RemoveUser.cfm file, which will remove users from the collection. Instead of passing the FirstName or LastName of the user, you'll need to pass a valid index. In this example, we'll just remove the first index, but in theory you would remove users based on an algorithm possibly determined by a method called GetMinutesIdle(). To remove the first user from the application, call the file RemoveUser.cfm, shown in Listing 6, passing a value of 1 in the attribute index, as shown below:
We've now created our ActiveUsers collection in a simplified form. You can add properties and methods to the ActiveUser object to make the system more robust. I recommend adding locking and validation to the Application scope and methods, respectively.
Your homes and cars can be automated and self-serviced. Why can't your storage? From simply asking questions to analyze and troubleshoot your infrastructure, to provisioning storage with snapshots, recovery and replication, your wildest sci-fi dream has come true. In his session at @DevOpsSummit at 20th Cloud Expo, Dan Florea, Director of Product Management at Tintri, will provide a ChatOps demo where you can talk to your storage and manage it from anywhere, through Slack and similar services ...
Mar. 28, 2017 07:00 PM EDT Reads: 4,436
In his session at @ThingsExpo, Eric Lachapelle, CEO of the Professional Evaluation and Certification Board (PECB), will provide an overview of various initiatives to certifiy the security of connected devices and future trends in ensuring public trust of IoT. Eric Lachapelle is the Chief Executive Officer of the Professional Evaluation and Certification Board (PECB), an international certification body. His role is to help companies and individuals to achieve professional, accredited and worldw...
Mar. 28, 2017 06:00 PM EDT Reads: 819
SYS-CON Events announced today that Linux Academy, the foremost online Linux and cloud training platform and community, will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Linux Academy was founded on the belief that providing high-quality, in-depth training should be available at an affordable price. Industry leaders in quality training, provided services, and student certification passes, its goal is to c...
Mar. 28, 2017 03:45 PM EDT Reads: 4,097
SYS-CON Events announced today that SoftLayer, an IBM Company, has been named “Gold Sponsor” of SYS-CON's 18th Cloud Expo, which will take place on June 7-9, 2016, at the Javits Center in New York, New York. SoftLayer, an IBM Company, provides cloud infrastructure as a service from a growing number of data centers and network points of presence around the world. SoftLayer’s customers range from Web startups to global enterprises.
Mar. 28, 2017 03:00 PM EDT Reads: 2,082
"delaPlex is a software development company. We do team-based outsourcing development," explained Mark Rivers, COO and Co-founder of delaPlex Software, in this SYS-CON.tv interview at 18th Cloud Expo, held June 7-9, 2016, at the Javits Center in New York City, NY.
Mar. 28, 2017 03:00 PM EDT Reads: 9,602
SYS-CON Events announced today that CA Technologies has been named “Platinum Sponsor” of SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY, and the 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. CA Technologies helps customers succeed in a future where every business – from apparel to energy – is being rewritten by software. From ...
Mar. 28, 2017 02:15 PM EDT Reads: 2,175
SYS-CON Events announced today that Technologic Systems Inc., an embedded systems solutions company, will exhibit at SYS-CON's @ThingsExpo, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Technologic Systems is an embedded systems company with headquarters in Fountain Hills, Arizona. They have been in business for 32 years, helping more than 8,000 OEM customers and building over a hundred COTS products that have never been discontinued. Technologic Systems’ pr...
Mar. 28, 2017 02:15 PM EDT Reads: 3,650
In his keynote at @ThingsExpo, Chris Matthieu, Director of IoT Engineering at Citrix and co-founder and CTO of Octoblu, focused on building an IoT platform and company. He provided a behind-the-scenes look at Octoblu’s platform, business, and pivots along the way (including the Citrix acquisition of Octoblu).
Mar. 28, 2017 02:00 PM EDT Reads: 14,245
SYS-CON Events announced today that Loom Systems will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Founded in 2015, Loom Systems delivers an advanced AI solution to predict and prevent problems in the digital business. Loom stands alone in the industry as an AI analysis platform requiring no prior math knowledge from operators, leveraging the existing staff to succeed in the digital era. With offices in S...
Mar. 28, 2017 01:15 PM EDT Reads: 1,617
SYS-CON Events announced today that HTBase will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. HTBase (Gartner 2016 Cool Vendor) delivers a Composable IT infrastructure solution architected for agility and increased efficiency. It turns compute, storage, and fabric into fluid pools of resources that are easily composed and re-composed to meet each application’s needs. With HTBase, companies can quickly prov...
Mar. 28, 2017 12:45 PM EDT Reads: 3,135
SYS-CON Events announced today that Cloud Academy will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Cloud Academy is the industry’s most innovative, vendor-neutral cloud technology training platform. Cloud Academy provides continuous learning solutions for individuals and enterprise teams for Amazon Web Services, Microsoft Azure, Google Cloud Platform, and the most popular cloud computing technologies. Ge...
Mar. 28, 2017 11:30 AM EDT Reads: 4,693
SYS-CON Events announced today that T-Mobile will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. As America's Un-carrier, T-Mobile US, Inc., is redefining the way consumers and businesses buy wireless services through leading product and service innovation. The Company's advanced nationwide 4G LTE network delivers outstanding wireless experiences to 67.4 million customers who are unwilling to compromise on ...
Mar. 28, 2017 11:30 AM EDT Reads: 2,479
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 20th International Cloud Expo, which will take place on June 6–8, 2017, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buyers...
Mar. 28, 2017 11:00 AM EDT Reads: 3,720
SYS-CON Events announced today that Infranics will exhibit at SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Since 2000, Infranics has developed SysMaster Suite, which is required for the stable and efficient management of ICT infrastructure. The ICT management solution developed and provided by Infranics continues to add intelligence to the ICT infrastructure through the IMC (Infra Management Cycle) based on mathemat...
Mar. 28, 2017 10:45 AM EDT Reads: 3,310
SYS-CON Events announced today that Interoute, owner-operator of one of Europe's largest networks and a global cloud services platform, has been named “Bronze Sponsor” of SYS-CON's 20th Cloud Expo, which will take place on June 6-8, 2017 at the Javits Center in New York, New York. Interoute is the owner-operator of one of Europe's largest networks and a global cloud services platform which encompasses 12 data centers, 14 virtual data centers and 31 colocation centers, with connections to 195 add...
Mar. 28, 2017 10:00 AM EDT Reads: 1,556
SYS-CON Events announced today that SD Times | BZ Media has been named “Media Sponsor” of SYS-CON's 20th International Cloud Expo, which will take place on June 6–8, 2017, at the Javits Center in New York City, NY. BZ Media LLC is a high-tech media company that produces technical conferences and expositions, and publishes a magazine, newsletters and websites in the software development, SharePoint, mobile development and commercial UAV markets.
Mar. 28, 2017 09:45 AM EDT Reads: 4,443
SYS-CON Events announced today that Cloudistics, an on-premises cloud computing company, has been named “Bronze Sponsor” of SYS-CON's 20th International Cloud Expo®, which will take place on June 6-8, 2017, at the Javits Center in New York City, NY. Cloudistics delivers a complete public cloud experience with composable on-premises infrastructures to medium and large enterprises. Its software-defined technology natively converges network, storage, compute, virtualization, and management into a ...
Mar. 28, 2017 09:45 AM EDT Reads: 2,254
Now that the world has connected “things,” we need to build these devices as truly intelligent in order to create instantaneous and precise results. This means you have to do as much of the processing at the point of entry as you can: at the edge. The killer use cases for IoT are becoming manifest through AI engines on edge devices. An autonomous car has this dual edge/cloud analytics model, producing precise, real-time results. In his session at @ThingsExpo, John Crupi, Vice President and Eng...
Mar. 28, 2017 09:15 AM EDT Reads: 4,088
There are 66 million network cameras capturing terabytes of data. How did factories in Japan improve physical security at the facilities and improve employee productivity? Edge Computing reduces possible kilobytes of data collected per second to only a few kilobytes of data transmitted to the public cloud every day. Data is aggregated and analyzed close to sensors so only intelligent results need to be transmitted to the cloud. Non-essential data is recycled to optimize storage.
Mar. 28, 2017 08:15 AM EDT Reads: 3,190
"I think that everyone recognizes that for IoT to really realize its full potential and value that it is about creating ecosystems and marketplaces and that no single vendor is able to support what is required," explained Esmeralda Swartz, VP, Marketing Enterprise and Cloud at Ericsson, in this SYS-CON.tv interview at @ThingsExpo, held June 7-9, 2016, at the Javits Center in New York City, NY.
Mar. 28, 2017 08:00 AM EDT Reads: 4,459