Skip to content

Commit

Permalink
docs: update private class data
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed May 17, 2024
1 parent 0dd466c commit ed7d912
Showing 1 changed file with 50 additions and 17 deletions.
67 changes: 50 additions & 17 deletions private-class-data/README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,39 @@
---
title: Private Class Data
category: Idiom
category: Structural
language: en
tag:
- Data access
- Abstraction
- Encapsulation
- Security
---

## Also known as

* Data Hiding
* Encapsulation

## Intent

Private Class Data design pattern seeks to reduce exposure of attributes by limiting their
visibility. It reduces the number of class attributes by encapsulating them in single Data object.
The Private Class Data design pattern aims to restrict access to the internal state of an object by providing controlled access through methods, thereby increasing security and reducing accidental data corruption.

## Explanation

Real world example

> Imagine you are cooking a stew for your family for dinner. You want to prevent your family members
> from consuming the stew by tasting it while you are cooking, otherwise there will be no more stew
> for dinner later.
> Imagine you are cooking a stew for your family dinner. You want to stop your family members from tasting the stew while you're still preparing it. If they do, there might not be enough stew left for dinner.
In plain words

> Private class data pattern prevents manipulation of data that is meant to be immutable by
> separating the data from the methods that use it into a class that maintains the data state.
> Private class data pattern prevents manipulation of data that is meant to be immutable by separating the data from the methods that use it into a class that maintains the data state.
Wikipedia says

> Private class data is a design pattern in computer programming used to encapsulate class
> attributes and their manipulation.
> Private class data is a design pattern in computer programming used to encapsulate class attributes and their manipulation.
**Programmatic Example**

Taking our stew example from above. First we have a `Stew` class where its data is not protected by
private class data, making the stew's ingredient mutable to class methods.
Taking our stew cooking example from above. First, we have a `Stew` class where its data is not protected by private class data, making the stew's ingredient mutable to class methods.

```java
@Slf4j
Expand Down Expand Up @@ -69,8 +70,7 @@ public class Stew {
}
```

Now, we have `ImmutableStew` class, where its data is protected by `StewData` class. Now, the
methods in are unable to manipulate the data of the `ImmutableStew` class.
Now, we have `ImmutableStew` class, where its data is protected by `StewData` class. The methods in `ImmutableStew` are unable to manipulate the data of the `StewData` class.

```java
public class StewData {
Expand Down Expand Up @@ -124,10 +124,43 @@ immutableStew.mix(); // Mixing the immutable stew we find: 2 potatoes, 4 carrot

## Class diagram

![alt text](./etc/private-class-data.png "Private Class Data")
![Private Class Data](./etc/private-class-data.png "Private Class Data")

## Applicability

Use the Private Class Data pattern when

* You want to prevent write access to class data members.
* When you want to protect the integrity of an object’s state.
* When you need to limit the visibility of the internal data of an object to prevent unintended modification.
* In scenarios where multiple classes need to share access to some common data without exposing it directly.

## Known Uses

* Java Beans, where properties are accessed via getters and setters.
* In many Java libraries where the internal state is hidden from the user to ensure consistency and security.
* Enterprise applications where sensitive data needs to be protected from direct access.

## Consequences

Benefits:

* Enhanced Security: Reduces the risk of unintended data corruption by encapsulating the data.
* Ease of Maintenance: Changes to the internal representation of data do not affect external code.
* Improved Abstraction: Users interact with a simplified interface without worrying about the complexities of data management.

Trade-offs:

* Performance Overhead: Additional method calls (getters/setters) can introduce slight performance overhead.
* Complexity: May increase the complexity of the class design due to the additional layer of methods for data access.

## Related Patterns

* [Proxy](https://java-design-patterns.com/patterns/proxy/): Both patterns restrict access to the underlying object but Proxy controls access to the object itself, while Private Class Data controls access to the data.
* [Singleton](https://java-design-patterns.com/patterns/singleton/): Ensures that a class has only one instance and provides a global point of access to it; often used to manage shared data with controlled access.
* [Decorator](https://java-design-patterns.com/patterns/decorator/): Adds behavior to an object without altering its structure; can be combined with Private Class Data to manage additional state privately.

## Credits

* [Clean Code: A Handbook of Agile Software Craftsmanship](https://amzn.to/3UJTZJk)
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
* [Effective Java](https://amzn.to/4cGk2Jz)

0 comments on commit ed7d912

Please sign in to comment.