Abstraction

Let's recap what we have known about OOP.

  • With inheritance, we can create subclass of a class that have same object with different behaviors.
  • With polymorphism, we can override how each property behave in its subclass with virtualand override keywords.
  • So long as these properties and methods behave as expected, we can implement them however we want, that's the idea behind encapsulation.
  • Thinking about an object in terms of its public interface also known as abstraction. When architecting software application, we don't need to care much about the concrete implementations of the class. We only need to think about them in abstract terms.

Our Invader class could be known as abstract base class which StrongInvader, ShieldedInvader, and FastInvader inherited from.

The first thing is change the name of Invader class to be:

abstract class Invader

But, it will return an error.

Let's create another class called BasicInvader and copy the code from StrongInvader class.

class BasicInvader : Invader
{
    public BasicInvader(Path path) : base(path)
    { }
}
// in Main class
Invader[] invaders =
{
    new ShieldedInvader(path),
    new FastInvader(path),
    new StrongInvader(path),
    new BasicInvader(path)
};

Actually, we haven't changed anything about the game. This is just refractoring the code to communicate the purpose of the invader base class more explicitly.

results matching ""

    No results matching ""