From ed7d912cc2f7c3e1e838c54fe6d8e15649fb21d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20Sepp=C3=A4l=C3=A4?= Date: Fri, 17 May 2024 11:58:53 +0300 Subject: [PATCH] docs: update private class data --- private-class-data/README.md | 67 +++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/private-class-data/README.md b/private-class-data/README.md index 9426f273f..0d1bbf4a6 100644 --- a/private-class-data/README.md +++ b/private-class-data/README.md @@ -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 @@ -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 { @@ -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)