Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Encapsulation problems
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Classes - VCX
Divers
Thread ID:
00104274
Message ID:
00104480
Vues:
24
>>My first difficulty is using protected methods and properties. I find myself unable to access these functions and properties from instantiations of the class. Is this correct behavior? Is this reaasonable behavior? I often find myself declaring properties and methods public, even when they should not be.
>
>Yes, this is the correct and reasonable behavior, David. Protected properties are normally used for internal processing inside an object. Anything that contains sensitive information or would break the functionality of the object if some outside process changes it's value. For those properties which need this level of protection, but still need to be accessed outside of the object, the standard OOP course of action is to define methods in the class which would allow access to the protected property, such as AccessProtProperty() or AssignProtProperty(). If your property needs to be visible everywhere AND does not need this level of protection from things outside the object, then is should not be defined as protected.


I wanted to add one more general thing to this discussion. If protected properties are such a pain to deal with, why even use them at all? The answer is to make your code more modular, resuable and robust. For instance, if you have a class which does some business logic, but the properties used in this business logic are public. You can then write code which accesses or even worse changes the values of these properties directly. Okay, everything works great so far and you do this kind of stuff in 50 dirrerent places in your application. Later, the rules in the business logic change. You change the code in your class, but now you potentially broke your code in those 50 different places. Maybe some of those properties are no longer needed and therefore removed or have changes types. Maybe, the order of processing changed. If you set up the business logic and all its properties to be protected, you would have had to define specific interfaces to the object right from the start. Then when the business rules change, all you whould have to do is make sure the interface code is backward compatible and nothing gets broken elsewhere.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform