KAAMIRLI
KAAMIRLI

Reputation: 19

OOAD Head First --Encapsulation

public class Guitar 
{
    private String SerialNumber ;
    private double Price;
    private String Model;
    private Type Type;
    private Builder Builder;
    private Wood BackWood;
    private Wood TopWood;
} 

Firstly I have this class in the OOAD -HeadFirst book.

This Guitar class used also Search(Guitar) method for searching guitars. Then create new class GuitarSpec as:

internal class GuitarSpec
{
    public Builder Builder { get; set; }
    public string Model { get; set; }
    public Enums.Type Type { get; set; }
    public Wood BackWood { get; set; }
    public Wood TopWood { get; set; }
}

The Guitar class uses this class like this:

internal class Guitar
{
    public string SerialNumber { get; set; }
    public double Price { get; set; }
    public GuitarSpec GuitarSpec { get; set; }
} 

In the SearchMethod(GuitarSpec) method, it's using the author also explain that as encapsulation. But I can't understand that why this encapsulation. For example mostly encapsulation example that private field public methods or constructor parameters. This code how become encapsulation of OOP?

Upvotes: 0

Views: 76

Answers (1)

Mark Seemann
Mark Seemann

Reputation: 233337

For the record, here's what the book says:

Q: I still am confused about how this is a form of encapsulation. Can you explain that again?

A: *The idea behind encapsulation is to protect information in one part of your application from the other parts of your application. In its simplest form, you can protect the data in your class from the rest of your app by making that data private. But sometimes the information might be an entire set of properties—like the details about a guitar—or even behavior—like how a particular type of duck flies.

When you break that behavior out from a class, you can change the behavior without the class having to change as well. So if you changed how properties were stored, you wouldn’t have to change your Guitar class at all, because the properties are encapsulated away from Guitar.

That’s the power of encapsulation: by breaking up the different parts of your app, you can change one part without having to change all the other parts. In general, you should encapsulate the parts of your app that might vary away from the parts that will stay the same.*

You're excused if this doesn't fully satisfy you, but I think the underlying issue is that encapsulation is a nebulous term. What one person means by 'encapsulation' may not correspond one-to-one with what another person means.

The authors seem to operate with a particular notion of encapsulation mostly concerned with decoupling. That's an important quality of well-designed code, but I would personally consider this more closely associated with the Open Closed Principle (OCP), originally introduced by Bertrand Meyer.

When discussing encapsulation, I find it more operative to use Meyer's definition, from e.g. Object-Oriented Software Construction. Here, encapsulation is defined from the idea of a contract: A set of preconditions, postconditions, and invariants that describe, in abstract terms, how client code successfully interacts with an object, without being aware of how the object is implemented. This is loosely connected to David Parnas' notion of information hiding.

And, if you ask me (or James Coplien) C# properties have little relation to information hiding.

Upvotes: 4

Related Questions