Home > front end >  OOP Encapsulation: sorting/grouping list of objects without exposing object's data
OOP Encapsulation: sorting/grouping list of objects without exposing object's data

Time:09-22

I am trying to understand encapsulation and reading a lot of about it. Generally, it is told that properties(c#), getters/setters(java) are evil. I can understand that because consumers can use exposed data in an unexpected way.

But at the same time, I have problems on this perspective. For example, I have this class:

public class Ad
{
    private readonly long _groupId;
    private readonly string _path;
    private readonly bool _paused;
    private readonly string _label;

    public Ad(long groupId, string path, bool paused, string label)
    {
        _groupId = groupId;
        _path = path;
        _paused = paused;
        _label = label;
    }
    //some methods here
}

I don't have any properties or getters/setters here. But then I am creating a list of this objects and trying to group them by _groupId or sort them by _label. I cannot do that because I don't have access to this properties.

Another question about this class: Should I expose paused field? Or give it with a method like isPaused?

Could you please enlighten my way on understanding encapsulation?

Thanks in advance.

CodePudding user response:

Not sure where you read that automatic properties are evil. They are great, and my go to over fields. You get access modifiers to protect these properties.

For example:

public class Ad
{
    public long GroupId { get; private set; }
// the private access modifier ensures that the property can only be set within the class (IE: constructor)
    public string Path { get; private set; }
    public bool Paused { get; private set; }
    public string Label { get; private set; }

    public Ad(long groupId, string path, bool paused, string label)
    {
        GroupId = groupId;
        Path = path;
        Paused = paused;
        Label = label;
    }

    public static List<Ad> SortMe(List<Ad> listToSort)
        => listToSort.OrderBy(a => a.Label).ToList();
}

CodePudding user response:

In effect unlike a C program with structs and unions in global scope to its methods in the code, each data object and process is encapsulated separately in a class for each individual purpose, hence each of that class you made has a unique I'd. The class is also classified as being encapsulated with setter and getter methods because they only work inside that class on that class individual instance and the global variables can only be accessed by methods because the variables are private. Heavier encapsulation security "separation" would require use of keyword final on the class and or methods , and raising class to default or private level security. Of grouping classes, a private array or collection list to hold the classes and retrieve them is helpful for indexing and sorting each instance, but the main way is to not use a public class and access the class by extending it with a public class that has the public set get is methods only and a corresponding interface so the class when it is created instantiated is cast to its interface type and only the interface type is used to obtain and manage the class.

Class aD{

private var;

aD(){}//constructor

//private final get set is methods without get set or is in their signature names

}// end class


public class aDwrapper extends aD implements adManager{

public aDwrapper{
super();
}

// public get set is methods to call the private methods

}// end class


public interface aDmanager{

// all the public set get is methods

}// end interface


// create  instance
aDmanager instanceAd = new aDwrapper();
  • Related