Loading presentation...

Present Remotely

Send the link below via email or IM

Copy

Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.

DeleteCancel

Encapsulate What Varies

No description
by

Ema Fălămaș

on 15 July 2014

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Encapsulate What Varies

Encapsulate What Varies
Change
no matter how well you design an application, over time it must grow and change or it will die.

change is the only constant in software development. This is a truism and leads to the encapsulate what varies design principle.
Better
Good
Bad
"Change is INEVITABLE, progress is OPTIONAL."
Tony Robbins
What is encapsulation?
one of the 4 fundamental OOP concepts (among inheritance, polymorphism, and abstraction)

the technique of making the fields in a class private and providing access to the fields via public methods

Design Patterns That Use Encapsulation
bridge
factory method
abstract factory
builder
strategy
Benefits of Encapsulation
the fields of a class can be made read-only or write-only

a class can have total control over what is stored in its fields

the users of a class do not know how the class stores its data

it allows for easier testing and maintenance.
Steps
identify the aspects of your application that may vary

take the parts that vary and encapsulate them: hide the details of what can change behind the public interface of a class
public class Animal
{
private String type;

public Animal(String type)
{
this.type = type;
}

public String MakeSound()
{
if(type.Equals("Cat"))
return "Meow!";
else if(type.Equals("Dog"))
return "Ham!";
else if(type.Equals("Cow"))
return "Moo!";
else
return "Animal not supported";
}
}

class Program
{
static void Main()
{
Animal cow = new Animal("Cow");
Console.WriteLine(cow.MakeSound());
}
}
public interface Animal
{
String MakeSound();
}

public class Cat : Animal
{
public String MakeSound()
{
return "Meow!";
}
}

public class Dog : Animal
{
public String MakeSound()
{
return "Ham!";
}
}

public class Cow : Animal
{
public String MakeSound()
{
return "Moo!";
}
}

class Program
{
static void Main()
{
Animal cow = new Cow();
Console.WriteLine(cow.MakeSound());
}
}
interface AnimalSound
{
string MakeSound();
}

class CatSound : AnimalSound
{
public string MakeSound()
{
return "Miau!";
}
}

class DogSound : AnimalSound
{
public string MakeSound()
{
return "Ham!";
}
}

class CowSound : AnimalSound
{
public string MakeSound()
{
return "Moo!";
}
}

class Animal
{
private AnimalSound sound;

public Animal(AnimalSound sound)
{
this.sound = sound;
}

public void PrintSound()
{
Console.WriteLine(sound.MakeSound());
}
}

class Program
{
static void Main()
{
AnimalSound sound = new CowSound();

Animal animal = new Animal(sound);
animal.PrintSound();
}
}
Full transcript