Class composition : Composition « Class « Flash / Flex / ActionScript






Class composition

 

package
{
    public class Employee
    {
        public function doWork():int
        {
            return 1;
        }
    }
}
    class Manager
    {
        protected var employees:Array;

        public function Manager(numberOfEmployees:int)
        {
            employees = new Array();
            for (var i:int = 0; i < numberOfEmployees; i++)
            {
                employees[i] = new Employee();
            }
        }

        public function delegateWork():int
        {
            var totalWork:int = 0;
            for each (var employee:Employee in employees)
            {

                totalWork += employee.doWork();
            }
            return totalWork;
        }
    }

    class Company
    {
        public function Company()
        {
            var manager:Manager = new Manager(10);
            var completedWork:int = manager.delegateWork();
            trace(completedWork); //10
        }
    }

        








Related examples in the same category

1.Inheritance vs. Composition