Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7953605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:11:42+00:00 2026-06-04T03:11:42+00:00

I am trying to build a simple Composition pattern sample. Basically the output will

  • 0

I am trying to build a simple Composition pattern sample. Basically the output will be each department employees’ total intelligent. I have created 3 types of employee and 2 departments.Please see the following codes:

Employee Class

abstract class Employee {

    function addEmployee(Employee $employee){

    }

    function removeEmployee(){

    }

    abstract function showIntelligent();

}

Minion class

class Minion extends Employee {

       function showIntelligent(){
          return '100';
    } 


}

Manager class

class Manager extends Employee {

    function showIntelligent(){
           return '150';
    } 



}

Sales Department Class

class SalesDept extends Employee {

     private $_deptEmployee=array();
    function addEmployee(Employee $employee){

        $this->_deptEmployee[]=$employee;

    }

    function removeEmployee(){
        if(!empty($this->_deptEmployee)){
            array_pop($this->_deptEmployee);
        }else{
            echo 'no employees in Sales Department';
        }
    }

    function showIntelligent() {

        $totalInt=0;
        foreach ($this->_deptEmployee as $employee){

            $totalInt += $employee->showIntelligent();
        }
        echo 'Total Intelligent of the Sales Department Employees is: '.$totalInt;
    }

}

Design Department Class

class DesignDept extends Employee {

     private $_deptEmployee=array();
    function addEmployee(Employee $employee){

        $this->_deptEmployee[]=$employee;

    }

    function removeEmployee(){
        if(!empty($this->_deptEmployee)){
            array_pop($this->_deptEmployee);
        }else{
            echo 'no employees in Design Department';
        }
    }

   function showIntelligent() {

        $totalInt=0;
        foreach ($this->_deptEmployee as $employee){

            $totalInt += $employee->showIntelligent();
        }
        echo 'Total Intelligent of the Design Department Employees is: '.$totalInt;
    }


}

My index

    $salesDpt=new SalesDept();
    $salesDpt->addEmployee(new Manager());
    $salesDpt->addEmployee(new Minion());
    $salesDpt->addEmployee(new Minion());
    $salesDpt->addEmployee(new GeneralManager());

    $salesDpt->showIntelligent();



    $DesignDpt=new DesignDept();
    $DesignDpt->addEmployee(new Manager());
    $DesignDpt->addEmployee(new Manager());
    $DesignDpt->addEmployee(new Minion());
    $DesignDpt->addEmployee(new Minion());
    $DesignDpt->addEmployee(new Minion());
    $DesignDpt->addEmployee(new Minion());
    $DesignDpt->addEmployee(new Minion());
    $DesignDpt->addEmployee(new Minion());
    $DesignDpt->addEmployee(new Minion());
    $DesignDpt->addEmployee(new GeneralManager());

    $DesignDpt->showIntelligent();

It seems like that I have to use lots of codes to add new employees. Is this a good practice? Anyway to improve it? Thanks for any advises.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:11:44+00:00Added an answer on June 4, 2026 at 3:11 am

    I have a number of comments – some of which have already been mentioned.

    1) Having an Employee class with subclasses of Minion and Manager isn’t a good idea. The problem here is that Minions can be promoted to Managers and that shouldn’t require you to throw away the old objects and replace them with a new ones. Instead, have one class for Employee with a “role” instance variable containing a MinionRole or a ManagerRole. With this design, you can replace the role without replacing the whole Employee object. In fact, from what you’ve described so far, you don’t need roles at all – just an instance variable for the intelligence.

    2) The department classes shouldn’t inherit from Employee. A department isn’t an employee and can’t be used as a replacement for an employee. Create a new class called Department. Do you really need subclasses? Would it be enough to have instances of Department where each department has a name (Sales, Design, etc?) Do you really want to have to modify, recompile, repackage and redeploy the application if you add an Accounting department?

    Subclassing is only useful when the subclasses have different behavior. In your case, the subclasses have the same behavior or close enough that a simple instance variable would make it work. Don’t get too excited about inheritance. Inheritance isn’t the answer to all problems and should be used carefully. Alan Kay, the creator of the Smalltalk programming language and the person who coined the term “object oriented” never liked the concept of inheritance and only reluctantly accepted it to allow better code sharing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build a simple Jabber client. I have downloaded this sample
I'm trying to build the simple layout that I have got in the image
Im trying to build a simple, lightweight product viewer using jquery. I have a
I'm trying to build a simple component extending spark.components.Group to have a background color,
I'm trying to build simple facebook application with 4.2.1 C# SDK. But I have
I'm trying to build a simple parser, and since InputStream doesn't have some peek-like
I am trying to build a simple horizontal list, where each list item is
I am trying to build a simple GUI app in java that will show
I am trying to build a simple URL shortner and I have come across
I'm trying to build a simple Magento Module that needs to connect to the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.