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
using System;
//**********************************************************************************
// Your namespace may be different
// To keep the command screen running press ctrl+F5
//**********************************************************************************
namespace Encapsulation
{
/******************************************************************************
* A class that will determine which sensor is in use
* Keep in mind that this is an example of encapsulation
* not the way that is an example of good architecture
******************************************************************************/
class CO2Devices
{
//**************************************************************************
// This method is used to determine which sensor is in use
// This is the public method that everyone can use
// The implementation is in the derived class
// The protected methods can be used in the derived class
//**************************************************************************
public void CO2Sensor(String Sensor)
{
/**********************************************************************************
* Switch that can be used to set the calibration factor
* In this example I don't do the simple multiplication
*********************************************************************************/
switch (Sensor)
{
case "Siemens":
Siemens();
break;
case "Hitachi":
Hitachi();
break;
default:
Junk();
break;
}
}
protected virtual void Siemens()
{
}
protected virtual void Hitachi()
{
}
protected virtual void Junk()
{
}
}
/*******************************************************************************
* The following class is the derived class from the CO2Devices
* Here you implement the functionality that isn't in the base class CO2Devices
* ****************************************************************************/
class BrandNames : CO2Devices
{
protected override void Siemens()
{
Console.WriteLine("\n\n\n*************Don't Drink and Drive*************\n\n\n");
Console.WriteLine("The Sensor type is: Siemens sensor");
Console.WriteLine("\n\n\nDon't Drink and Drive");
}
protected override void Hitachi()
{
Console.WriteLine("\n\n\nDon't Drink and Drive\n\n\n");
Console.WriteLine("The Sensor type is: Hitachi sensor");
Console.WriteLine("\n\n\nDon't Drink and Drive");
}
protected override void Junk()
{
Console.WriteLine("\n\n\nDon't Drink and Drive\n\n\n");
Console.WriteLine("Management needed cash for their bonus so bought the cheap sensor");
Console.WriteLine("\n\n\nDon't Drink and Drive");
}
}
/***************************************************************************
* The program class is used to start the program as it includes the
* required Main function. You could move it elsewhere, but good idea
* to keep it in the "Program.cs" class
*
* ************************************************************************/
class Program
{
static void Main(string[] args)
{
BrandNames Beer_Sensor = new BrandNames();
//To test change the input to Hitachi or Junk between the quotes
Beer_Sensor.CO2Sensor("Siemens");
}
}
}