Click here to Skip to main content
15,895,142 members
Articles / Desktop Programming / WPF

Object Composition Over Inheritance

Rate me:
Please Sign up or sign in to vote.
4.75/5 (5 votes)
2 May 2011CPOL4 min read 28.3K   287   15  
This article explains the use of Composition in WPF applications, but it can very well be used in others as well.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace WrongInheritance.ClassesAndInterfaces
{
    public class MainViewModel : INotifyPropertyChanged
    {
        private string selectedEmployeeType;
        private List<string> employeeTypes;
        private UserBase employee;

        public string SelectedEmployeeType
        {
            get
            {
                return selectedEmployeeType;
            }
            set
            {
                selectedEmployeeType = value;
                OnPropertyChanged("SelectedEmployeeType");
                OnEmployeeSelected();
            }
        }

        public List<string> EmployeeTypes
        {
            get
            {
                return employeeTypes;
            }
            set
            {
                employeeTypes = value;
                OnPropertyChanged("EmployeeTypes");
            }
        }

        public UserBase Employee
        {
            get
            {
                return employee;
            }
            set
            {
                employee = value;
                OnPropertyChanged("Employee");
            }
        }
        #region INotifyPropertyChanged Members

        public MainViewModel()
        {
            employeeTypes = new List<string> { "Manager", "Leader", "Coder" };
            selectedEmployeeType = "Manager";
            Employee = new Manager();
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        private void OnEmployeeSelected()
        {
            if (!string.IsNullOrEmpty(selectedEmployeeType))
            {
                switch (selectedEmployeeType)
                {
                    case "Manager":
                        Employee = new Manager();
                        break;
                    case "Leader":
                        Employee = new Leader();
                        break;
                    case "Coder":
                        Employee = new Coder();
                        break;
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions