Click here to Skip to main content
15,885,278 members
Articles / Web Development / ASP.NET

Object Composition Over Inheritance

Rate me:
Please Sign up or sign in to vote.
3.78/5 (8 votes)
1 May 2011CPOL3 min read 61.6K   470   12  
Why to prefer object composition over inheritance
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using WPFSuppliments.CommandArchitecture;
using System.Windows;

namespace RightComposition.ClassesAndInterfaces
{
    public class AveragePrinter:IPrinterCommand
    {
        ICommand printCommand;
        ICommand faxCommand;
        ICommand scanCommand;
        ICommand emailCommand;

        #region IPrinterCommand Members

        public System.Windows.Input.ICommand PrintCommand
        {
            get
            {
                return printCommand;
            }
            set
            {
                printCommand = value;
            }
        }

        public System.Windows.Input.ICommand FaxCommand
        {
            get
            {
                return faxCommand;
            }
            set
            {
                faxCommand = value;
            }
        }

        public System.Windows.Input.ICommand ScanCommand
        {
            get
            {
                return scanCommand;
            }
            set
            {
                scanCommand = value;
            }
        }

        public System.Windows.Input.ICommand EmailCommand
        {
            get
            {
                return emailCommand;
            }
            set
            {
                emailCommand = value;
            }
        }

        #endregion

        public void Initialize()
        {
            PrintCommand = new DelegateCommand(ExecutePrintCommand);
            FaxCommand = new DelegateCommand(ExecuteFaxCommand);
            ScanCommand = new DelegateCommand(ExecuteScanCommand);
            EmailCommand = new DelegateCommand(ExecuteEmailCommand);
        }


        private void ExecutePrintCommand(object parameter)
        {
            MessageBox.Show("Executing average printer print command.");
        }
        private void ExecuteFaxCommand(object parameter)
        {
            MessageBox.Show("Average printer does not support fax command.");
        }
        private void ExecuteScanCommand(object parameter)
        {
            MessageBox.Show("Executing average printer scan command.");
        }
        private void ExecuteEmailCommand(object parameter)
        {
            MessageBox.Show("Average printer does not support email command.");
        }
    }
}

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