Encapsulation at Duff’s Beer brewery

Continuing from yesterday, we created an encapsulation like program, except everything was public.  Is that true encapsulation?

 

 

 

 

I copied this table from the URL below:

Access Modifier Description (who can access)
private Only members within the same type. (default for type members)
protected Only derived types or members of the same type.
internal Only code within the same assembly. Can also be code external to object as long as it is in the same assembly. (default for types)
protected internal Either code from derived type or code in the same assembly. Combination of protected OR internal.
public Any code. No inheritance, external type, or external assembly restrictions.

When the public access modifier is used on a type member, calling code accesses the type member. This type of variable is a party animal and everyone has access to them, sort of like a blogger.

In the previous blog, if you checked out my program, it wasn’t really representative of the goals of encapsulation, mainly because everything was public.

The following code demonstrates encapsulation with the “features” of the program hidden from an external user.  The way that you write the calibration code is protected from the external calls and if you do just a little architectural considerations then you code should be easily documented and maintained.  What happens when you change the line:

protected virtual void Siemens()

to

internal virtual void Siemens()

What happens?
 

Code Snippet

  1. using System;

  2.  

  3. //**********************************************************************************

  4. // Your namespace may be different

  5. // To keep the command screen running press ctrl+F5

  6. //**********************************************************************************

  7. namespace Encapsulation

  8. {   

  9.     /******************************************************************************

  10.      * A class that will determine which sensor is in use

  11.      * Keep in mind that this is an example of encapsulation

  12.      * not the way that is an example of good architecture

  13.       ******************************************************************************/

  14.     class CO2Devices

  15.     {

  16.         //**************************************************************************

  17.         // This method is used to determine which sensor is in use

  18.         // This is the public method that everyone can use

  19.         // The implementation is in the derived class

  20.         // The protected methods can be used in the derived class

  21.         //**************************************************************************

  22.         

  23.         public void CO2Sensor(String Sensor)

  24.         {

  25.             /**********************************************************************************

  26.              * Switch that can be used to set the calibration factor

  27.              * In this example I don't do the simple multiplication

  28.              *********************************************************************************/

  29.             switch (Sensor)

  30.             {

  31.                 case "Siemens":

  32.                     Siemens();

  33.                     break;

  34.                 case "Hitachi":

  35.                     Hitachi();

  36.                     break;

  37.                 default:

  38.                     Junk();

  39.                     break;

  40.             }

  41.         }

  42.         protected virtual void Siemens()

  43.         {  

  44.         }

  45.         protected virtual void Hitachi()

  46.         {

  47.         }

  48.         protected virtual void Junk()

  49.         {

  50.         }

  51.     }

  52.  

  53.     /*******************************************************************************

  54.     * The following class is the derived class from the CO2Devices

  55.     * Here you implement the functionality that isn't in the base class CO2Devices

  56.     * ****************************************************************************/

  57.     class BrandNames : CO2Devices

  58.     {

  59.         protected override void Siemens()

  60.         {

  61.             Console.WriteLine("\n\n\n*************Don't Drink and Drive*************\n\n\n");

  62.             Console.WriteLine("The Sensor type is: Siemens sensor");

  63.             Console.WriteLine("\n\n\nDon't Drink and Drive");

  64.         }

  65.  

  66.         protected override void Hitachi()

  67.         {

  68.             Console.WriteLine("\n\n\nDon't Drink and Drive\n\n\n");

  69.             Console.WriteLine("The Sensor type is: Hitachi sensor");

  70.             Console.WriteLine("\n\n\nDon't Drink and Drive");

  71.  

  72.         }

  73.  

  74.         protected override void Junk()

  75.         {

  76.             Console.WriteLine("\n\n\nDon't Drink and Drive\n\n\n");

  77.             Console.WriteLine("Management needed cash for their bonus so bought the cheap sensor");

  78.             Console.WriteLine("\n\n\nDon't Drink and Drive");

  79.         }

  80.     }

  81.     /***************************************************************************

  82.      * The program class is used to start the program as it includes the

  83.      * required Main function. You could move it elsewhere, but good idea

  84.      * to keep it in the "Program.cs" class

  85.      *

  86.      * ************************************************************************/

  87.     class Program

  88.     {

  89.         static void Main(string[] args)

  90.         {            

  91.             BrandNames Beer_Sensor = new BrandNames();

  92.             //To test change the input to Hitachi or Junk between the quotes

  93.             Beer_Sensor.CO2Sensor("Siemens");

  94.         }

  95.     }

  96. }