Coding: Understanding Encapsulation
When I started learning programming in college I had no idea what I was doing, and to be honest, the teaching style was not very effective. Most of my classmates were in the same boat, we had to google a lot and work with each other to try to understand the principles of programming. After many years of coding and learning I was able to grasp the concepts. I am going to attempt to explain these principles in the simplest terms.
As mentioned in this other post [Programming Languages: Differences] one of the core principles of Object-Oriented programming is the concept of encapsulation (The process of combining elements to create a new entity. It also hides data implementation and restricts access to reduce complexity).
Very similar to abstraction [Understanding Abstraction] but easier to understand in my opinion, encapsulation hides data implementation in the object, it also bundles attributes of an object.
In the case of the Coffee Machine example presented in my previous post about abstraction, the CoffeeMachine class contains several methods, some of them public for other classes to access and other ones private. The process of bundling all these methods and attributes into one class is encapsulation.
public class CoffeeMachine
{
private Coffee _coffee;
private string _cupSizeName;
public CoffeeMachine(Coffee coffee, CupSizeEnum cupSizeEnum)
{
_coffee = coffee;
switch (cupSizeEnum)
{
case CupSizeEnum.Small:
_cupSizeName = "Small";
break;
case CupSizeEnum.Medium:
_cupSizeName = "Medium";
break;
case CupSizeEnum.Large:
_cupSizeName = "Large";
break;
}
}
public Coffee BrewCoffee()
{
Coffee brewedCoffee = _coffee;
switch (_coffee.CoffeeSelection)
{
case CoffeeSelectionEnum.EspressoShot:
brewedCoffee = BrewEspressoShot();
break;
case CoffeeSelectionEnum.Americano:
brewedCoffee = BrewAmericano();
break;
case CoffeeSelectionEnum.Drip:
brewedCoffee = BrewDrip();
break;
case CoffeeSelectionEnum.PourOver:
brewedCoffee = BrewPourOver();
break;
default:
Console.WriteLine("Coffee Selection not chosen.");
break;
}
int count = 0;
while (count <= 4)
{
Thread.Sleep(1000);
Console.Write(".");
count++;
}
brewedCoffee.IsBrewed = true;
Console.WriteLine("Enjoy your fresh cup of joe!");
return brewedCoffee;
}
private Coffee BrewEspressoShot()
{
DisplayProcessStep();
//Do whatever needs to be done to brew espressoShot
return _coffee;
}
private Coffee BrewAmericano()
{
DisplayProcessStep();
//Do whatever needs to be done to brew americano
return _coffee;
}
private Coffee BrewDrip()
{
DisplayProcessStep();
//Do whatever needs to be done to brew drip coffee
return _coffee;
}
private Coffee BrewPourOver()
{
DisplayProcessStep();
//Do whatever needs to be done to brew pour over coffee
return _coffee;
}
private void DisplayProcessStep()
{
Console.WriteLine($"Brewing {_cupSizeName} cup of {_coffee.Name}");
}
}
Just like in medicine field, all the ingredients that makes the medicine work and are bundled into one capsule, in programming the classes contains all the attributes that make an object function.
I hope that this small code snippet helps understand the principle of abstraction a little bit better.
You check out the whole code in this repository.