Encapsulation Patterns
Fundamental Encapsulation Strategies
Encapsulation Levels
Level |
Description |
Implementation |
Basic |
Simple attribute protection |
Naming conventions |
Intermediate |
Controlled access |
Property decorators |
Advanced |
Complex data management |
Descriptor protocols |
Basic Encapsulation Techniques
Using Private Attributes
class BankAccount:
def __init__(self, initial_balance):
self.__balance = initial_balance ## Private attribute
def deposit(self, amount):
if amount > 0:
self.__balance += amount
def get_balance(self):
return self.__balance
Property-Based Encapsulation
class SecureConfiguration:
def __init__(self):
self.__settings = {}
@property
def settings(self):
return self.__settings.copy()
@settings.setter
def settings(self, new_settings):
## Validate and set settings
self.__settings = dict(new_settings)
Advanced Encapsulation Patterns
Descriptor Protocol
class ValidatedAttribute:
def __init__(self, min_value=None, max_value=None):
self.min_value = min_value
self.max_value = max_value
def __set_name__(self, owner, name):
self.name = name
def __set__(self, instance, value):
if self.min_value is not None and value < self.min_value:
raise ValueError(f"Value too low for {self.name}")
if self.max_value is not None and value > self.max_value:
raise ValueError(f"Value too high for {self.name}")
instance.__dict__[self.name] = value
class User:
age = ValidatedAttribute(min_value=0, max_value=120)
def __init__(self, name, age):
self.name = name
self.age = age
Encapsulation Flow
graph TD
A[Raw Data Input] --> B{Validation}
B -->|Pass| C[Set Attribute]
B -->|Fail| D[Raise Exception]
C --> E[Controlled Access]
Design Patterns for Encapsulation
Factory Method Pattern
class SecurityFactory:
@staticmethod
def create_secure_object(object_type):
if object_type == 'user':
return UserSecureObject()
elif object_type == 'config':
return ConfigSecureObject()
class UserSecureObject:
def __init__(self):
self.__private_data = {}
def set_data(self, key, value):
self.__private_data[key] = value
Best Practices
- Minimize direct attribute access
- Use properties for complex attribute management
- Implement validation in setters
- Protect sensitive data
At LabEx, we emphasize creating robust encapsulation strategies that balance security and flexibility.
Error Handling in Encapsulation
class SecureDataContainer:
def __init__(self):
self.__data = {}
def add_data(self, key, value):
try:
## Implement secure data addition
if not isinstance(key, str):
raise TypeError("Key must be a string")
self.__data[key] = value
except Exception as e:
print(f"Encapsulation error: {e}")
Key Takeaways
- Encapsulation is about controlled data access
- Multiple techniques exist for different scenarios
- Always validate and protect internal state
- Use Python's built-in mechanisms effectively