mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 10:58:42 +00:00
docs: Content SEO updates (#2990)
* update yaml frontmatter format * update abstract document * update abstract factory * use the new pattern template * acyclic visitor seo * adapter seo * ambassador seo * acl seo * aaa seo * async method invocation seo * balking seo * bridge seo * builder seo * business delegate and bytecode seo * caching seo * callback seo * chain seo * update headings * circuit breaker seo * client session + collecting parameter seo * collection pipeline seo * combinator SEO * command seo * cqrs seo * commander seo * component seo * composite seo * composite entity seo * composite view seo * context object seo * converter seo * crtp seo * currying seo * dao seo * data bus seo * data locality seo * data mapper seo * dto seo * decorator seo * delegation seo * di seo * dirty flag seo * domain model seo * double buffer seo * double checked locking seo * double dispatch seo * dynamic proxy seo * event aggregator seo * event-based asynchronous seo * eda seo * event queue seo * event sourcing seo * execute around seo * extension objects seo * facade seo * factory seo * factory kit seo * factory method seo * fanout/fanin seo * feature toggle seo * filterer seo * fluent interface seo * flux seo * flyweight seo * front controller seo * function composition seo * game loop seo * gateway seo * guarded suspension seo * half-sync/half-async seo * health check seo * hexagonal seo * identity map seo * intercepting filter seo * interpreter seo * iterator seo * layers seo * lazy loading seo * leader election seo * leader/followers seo * lockable object seo * rename and add seo for marker interface * master-worker seo * mediator seo * memento seo * metadata mapping seo * microservice aggregator seo * api gw seo * microservices log aggregration seo * mvc seo * mvi seo * mvp seo * mvvm seo * monad seo * monitor seo * monostate seo * multiton seo * mute idiom seo * naked objects & notification seo * null object seo * object mother seo * object pool seo * observer seo * optimistic locking seo * page controller seo * page object seo * parameter object seo * partial response seo * pipeline seo * poison pill seo * presentation model seo * private class data seo * producer-consumer seo * promise seo * property seo * prototype seo * proxy seo * queue-based load leveling seo * reactor seo * registry seo * repository seo * RAII seo * retry seo * role object seo * saga seo * separated interface seo * serialized entity seo * serialized lob seo * servant seo * server session seo * service layer seo * service locator seo * service to worker seo * sharding seo * single table inheritance seo * singleton seo * spatial partition seo * special case seo * specification seo * state seo * step builder seo * strangler seo * strategy seo * subclass sandbox seo * table module seo * template method seo * throttling seo * tolerant reader seo * trampoline seo * transaction script seo * twin seo * type object seo * unit of work seo * update method seo * value object seo * version number seo * virtual proxy seo * visitor seo * seo enhancements * seo improvements * SEO enhancements * SEO improvements * SEO additions * SEO improvements * more SEO improvements * rename hexagonal + SEO improvements * SEO improvements * more SEO stuff * SEO improvements * SEO optimizations * SEO enhancements * enchance SEO * improve SEO * SEO improvements * update headers
This commit is contained in:
+28
-26
@@ -1,16 +1,18 @@
|
||||
---
|
||||
title: Type Object
|
||||
title: "Type Object Pattern in Java: Enhancing Flexibility with Dynamic Class Definitions"
|
||||
shortTitle: Type Object
|
||||
description: "Discover how the Type Object Pattern in Java allows for dynamic and flexible class creation without altering existing code. Ideal for developers looking to understand and apply this powerful design pattern in real-world Java applications."
|
||||
category: Creational
|
||||
language: en
|
||||
tag:
|
||||
- Abstraction
|
||||
- Code simplification
|
||||
- Data processing
|
||||
- Game programming
|
||||
- Extensibility
|
||||
- Instantiation
|
||||
- Object composition
|
||||
- Polymorphism
|
||||
- Abstraction
|
||||
- Code simplification
|
||||
- Data processing
|
||||
- Game programming
|
||||
- Extensibility
|
||||
- Instantiation
|
||||
- Object composition
|
||||
- Polymorphism
|
||||
---
|
||||
|
||||
## Also known as
|
||||
@@ -18,11 +20,11 @@ tag:
|
||||
* Type Descriptor
|
||||
* Type Safe Enumeration
|
||||
|
||||
## Intent
|
||||
## Intent of Type Object Design Pattern
|
||||
|
||||
Allow creation of flexible and extensible sets of related types.
|
||||
|
||||
## Explanation
|
||||
## Detailed Explanation of Type Object Pattern with Real-World Examples
|
||||
|
||||
Real-world example
|
||||
|
||||
@@ -30,15 +32,15 @@ Real-world example
|
||||
|
||||
In plain words
|
||||
|
||||
> The Type Object pattern allows for the creation and management of flexible and extensible sets of related types dynamically, without modifying existing code.
|
||||
> Explore how the Java Type Object pattern enables dynamic creation and management of flexible and extensible sets of related classes, ideal for Java developers seeking modularity without modifying existing codebase.
|
||||
|
||||
gameprogrammingpatterns.com says
|
||||
|
||||
> Define a type object class and a typed object class. Each type object instance represents a different logical type. Each typed object stores a reference to the type object that describes its type.
|
||||
|
||||
**Programmatic example**
|
||||
## Programmatic Example of Type Object Pattern in Java
|
||||
|
||||
The Type Object pattern is a design pattern that allows for the creation of flexible and reusable objects by creating a class with a field that represents the 'type' of the object. This pattern is useful when the types needed are not known upfront, or when there is a need to modify or add new types conveniently without recompiling repeatedly.
|
||||
The Type Object pattern is a design pattern that allows for the creation of flexible and reusable objects by creating a class with a field that represents the 'type' of the object. This design pattern proves invaluable for scenarios where anticipated Java types are undefined upfront, or when modifications or additions are required, ensuring efficient Java development without frequent recompilations.
|
||||
|
||||
In the provided code, the Type Object pattern is implemented in a mini candy-crush game. The game has many different candies, which may change over time as the game is upgraded.
|
||||
|
||||
@@ -205,12 +207,12 @@ public class App {
|
||||
Let's break down what happens in `App` class.
|
||||
|
||||
1. The `main` method is the entry point of the application. It starts by initializing several variables:
|
||||
- `givenTime` is set to 50 milliseconds. This is the time limit for the game.
|
||||
- `toWin` is set to 500 points. This is the target score to win the game.
|
||||
- `pointsWon` is initialized to 0. This variable keeps track of the total points won so far.
|
||||
- `numOfRows` is set to 3. This is the number of rows in the game grid.
|
||||
- `start` and `end` are both set to the current system time in milliseconds. These variables are used to track the elapsed time.
|
||||
- `round` is initialized to 0. This variable keeps track of the current round number.
|
||||
- `givenTime` is set to 50 milliseconds. This is the time limit for the game.
|
||||
- `toWin` is set to 500 points. This is the target score to win the game.
|
||||
- `pointsWon` is initialized to 0. This variable keeps track of the total points won so far.
|
||||
- `numOfRows` is set to 3. This is the number of rows in the game grid.
|
||||
- `start` and `end` are both set to the current system time in milliseconds. These variables are used to track the elapsed time.
|
||||
- `round` is initialized to 0. This variable keeps track of the current round number.
|
||||
|
||||
2. The game enters a loop that continues until either the player has won enough points (`pointsWon >= toWin`) or the time limit has been reached (`end - start < givenTime`).
|
||||
|
||||
@@ -292,7 +294,7 @@ Console output:
|
||||
|
||||
In this implementation, the Type Object pattern allows for the flexible creation of `Candy` objects. The type of each candy is determined at runtime by parsing a JSON file, which makes it easy to add, modify, or remove candy types without having to recompile the code.
|
||||
|
||||
## Applicability
|
||||
## When to Use the Type Object Pattern in Java
|
||||
|
||||
This pattern can be used when:
|
||||
|
||||
@@ -301,17 +303,17 @@ This pattern can be used when:
|
||||
* Suitable for situations where the number of types is large and may change over time.
|
||||
* The difference between the different 'types' of objects is the data, not the behaviour.
|
||||
|
||||
## Tutorials
|
||||
## Type Object Pattern Java Tutorials
|
||||
|
||||
* [Types as Objects Pattern (Jon Pearce)](http://www.cs.sjsu.edu/~pearce/modules/patterns/analysis/top.htm)
|
||||
|
||||
## Known uses
|
||||
## Real-World Applications of Type Object Pattern in Java
|
||||
|
||||
* Java Collections Framework: Utilizing various collection types like List, Set, and Map.
|
||||
* Graphics Libraries: Defining different shapes with specific properties and behaviors.
|
||||
* Game Development: Creating different types of characters or items with unique attributes and behaviors.
|
||||
|
||||
## Consequences
|
||||
## Benefits and Trade-offs of Type Object Pattern
|
||||
|
||||
Benefits:
|
||||
|
||||
@@ -324,13 +326,13 @@ Trade-offs:
|
||||
* Can increase complexity if not managed properly.
|
||||
* May lead to performance overhead due to dynamic type checking and handling.
|
||||
|
||||
## Related patterns
|
||||
## Related Java Design Patterns
|
||||
|
||||
* [Factory Method](https://java-design-patterns.com/patterns/factory-method/): Often used in conjunction with Type Object to create instances of the types.
|
||||
* [Strategy](https://java-design-patterns.com/patterns/strategy/): Similar in that it defines a family of algorithms or behaviors, but focuses more on interchangeable behaviors.
|
||||
* [Prototype](https://java-design-patterns.com/patterns/prototype/): Can be used to create new instances by copying existing ones, supporting dynamic and flexible type creation.
|
||||
|
||||
## Credits
|
||||
## References and Credits
|
||||
|
||||
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
|
||||
* [Effective Java](https://amzn.to/4cGk2Jz)
|
||||
|
||||
Reference in New Issue
Block a user