☕️JAVA Language Community
2.91K subscribers
144 photos
7 videos
31 files
42 links
☕️ Software, IT, Java, news
💻 IT highlights
🎯 AI update
🖥⌨️🖱
Download Telegram
#Design_Patterns

#Object_Pool_Design_Pattern

👉Check list

1️⃣Create ObjectPool class with private array of Objects inside

2️⃣Create acquire and release methods in ObjectPool class

3️⃣Make sure that your ObjectPool is Singleton


👉Rules of thumb

▪️The Factory Method pattern can be used to encapsulate the creation logic for objects. However, it does not manage them after their creation, the object pool pattern keeps track of the objects it creates.

▪️Object Pools are usually implemented as Singletons.

@javaCode☕️
#Design_Patterns

#Creational_patterns

#Prototype_Design_Pattern

👉Intent

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Co-opt one instance of a class for use as a breeder of all future instances.
The new operator considered harmful.

@javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Problem

Application "hard wires" the class of object to create in each "new" expression.

@javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Discussion

Declare an abstract base class that specifies a pure virtual "clone" method, and, maintains a dictionary of all "cloneable" concrete derived classes. Any class that needs a "polymorphic constructor" capability: derives itself from the abstract base class, registers its prototypical instance, and implements the clone() operation.

The client then, instead of writing code that invokes the "new" operator on a hard-wired class name, calls a "clone" operation on the abstract base class, supplying a string or enumerated data type that designates the particular concrete derived class desired.

@javaCode☕️
☕️JAVA Language Community
#Prototype_Design_Pattern @javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Example

The Prototype pattern specifies the kind of objects to create using a prototypical instance. Prototypes of new products are often built prior to full production, but in this example, the prototype is passive and does not participate in copying itself. The mitotic division of a cell - resulting in two identical cells - is an example of a prototype that plays an active role in copying itself and thus, demonstrates the Prototype pattern. When a cell splits, two cells of identical genotype result. In other words, the cell clones itself.

@javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Check list

1️⃣Add a clone() method to the existing "product" hierarchy.

2️⃣Design a "registry" that maintains a cache of prototypical objects. The registry could be encapsulated in a new Factory class, or in the base class of the "product" hierarchy.

3️⃣Design a factory method that: may (or may not) accept arguments, finds the correct prototype object, calls clone() on that object, and returns the result.

4️⃣The client replaces all references to the new operator with calls to the factory method.

@javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Rules of thumb

1️⃣Sometimes creational patterns are competitors: there are cases when either Prototype or Abstract Factory could be used properly. At other times they are complementary: Abstract Factory might store a set of Prototypes from which to clone and return product objects. Abstract Factory, Builder, and Prototype can use Singleton in their implementations.

2️⃣Abstract Factory classes are often implemented with Factory Methods, but they can be implemented using Prototype.

3️⃣Factory Method: creation through inheritance. Prototype: creation through delegation.

4️⃣Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.

5️⃣Prototype doesn't require subclassing, but it does require an "initialize" operation. Factory Method requires subclassing, but doesn't require Initialize.
Designs that make heavy use of the Composite and Decorator patterns often can benefit from Prototype as well.

6️⃣Prototype co-opts one instance of a class for use as a breeder of all future instances.

7️⃣Prototypes are useful when object initialization is expensive, and you anticipate few variations on the initialization parameters. In this context, Prototype can avoid expensive "creation from scratch", and support cheap cloning of a pre-initialized prototype.

8️⃣Prototype is unique among the other creational patterns in that it doesn't require a class – only an object. Object-oriented languages like Self and Omega that do away with classes completely rely on prototypes for creating new objects.

@javaCode☕️
#Design_Patterns

#Singleton_Design_Pattern

👉Intent

Ensure a class has only one instance, and provide a global point of access to it.
Encapsulated "just-in-time initialization" or "initialization on first use".

@javaCode☕️
#Design_Patterns

#Singleton_Design_Pattern

👉Problem

Application needs one, and only one, instance of an object. Additionally, lazy initialization and global access are necessary.

@javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern

👉Discussion

Make the class of the single instance object responsible for creation, initialization, access, and enforcement. Declare the instance as a private static data member. Provide a public static member function that encapsulates all initialization code, and provides access to the instance.

The client calls the accessor function (using the class name and scope resolution operator) whenever a reference to the single instance is required.

Singleton should be considered only if all three of the following criteria are satisfied:

▪️Ownership of the single instance cannot be reasonably assigned

▪️Lazy initialization is desirable

▪️Global access is not otherwise provided for

If ownership of the single instance, when and how initialization occurs, and global access are not issues, Singleton is not sufficiently interesting.

The Singleton pattern can be extended to support access to an application-specific number of instances.

The "static member function accessor" approach will not support subclassing of the Singleton class. If subclassing is desired, refer to the discussion in the book.

Deleting a Singleton class/instance is a non-trivial design problem. See "To Kill A Singleton" by John Vlissides for a discussion.

@javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern

👉Structure

Make the class of the single instance responsible for access and "initialization on first use". The single instance is a private static attribute. The accessor function is a public static method.

@javaCode☕️
☕️JAVA Language Community
#Singleton_Design_Pattern @javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern

👉Example

The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton. The United States Constitution specifies the means by which a president is elected, limits the term of office, and defines the order of succession. As a result, there can be at most one active president at any given time. Regardless of the personal identity of the active president, the title, "The President of the United States" is a global point of access that identifies the person in the office.

@javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern

👉Check list

1️⃣Define a private static attribute in the "single instance" class.

2️⃣Define a public static accessor function in the class.

3️⃣Do "lazy initialization" (creation on first use) in the accessor function.

4️⃣Define all constructors to be protected or private.

5️⃣Clients may only use the accessor function to manipulate the Singleton.

@javaCode☕️
#Design_Patterns
#Singleton_Design_Pattern

👉Rules of thumb

1️⃣Abstract Factory, Builder, and Prototype can use Singleton in their implementation.

2️⃣Facade objects are often Singletons because only one Facade object is required.

3️⃣State objects are often Singletons.

4️⃣The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, you can change your mind and manage any number of instances.

5️⃣The Singleton design pattern is one of the most inappropriately used patterns. Singletons are intended to be used when a class must have exactly one instance, no more, no less. Designers frequently use Singletons in a misguided attempt to replace global variables. A Singleton is, for intents and purposes, a global variable. The Singleton does not do away with the global; it merely renames it.

6️⃣When is Singleton unnecessary? Short answer: most of the time. Long answer: when it's simpler to pass an object resource as a reference to the objects that need it, rather than letting objects access the resource globally. The real problem with Singletons is that they give you such a good excuse not to think carefully about the appropriate visibility of an object. Finding the right balance of exposure and protection for an object is critical for maintaining flexibility.

7️⃣Our group had a bad habit of using global data, so I did a study group on Singleton. The next thing I know Singletons appeared everywhere and none of the problems related to global data went away. The answer to the global data question is not, "Make it a Singleton." The answer is, "Why in the hell are you using global data?" Changing the name doesn't change the problem. In fact, it may make it worse because it gives you the opportunity to say, "Well I'm not doing that, I'm doing this" – even though this and that are the same thing.

@javaCode☕️
#Design_Patterns
#Creational_patterns

👉Creational patterns

In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design patterns solve this problem by somehow controlling this object creation.


1️⃣ #Abstract_Factory_Design_Pattern

▪️Creates an instance of several families of classes

2️⃣ #Builder_Design_Pattern

▪️Separates object construction from its representation
3️⃣ #Factory_Method_Design_Pattern

▪️Creates an instance of several derived classes

4️⃣ #Object_Pool_Design_Pattern

▪️Avoid expensive acquisition and release of resources by recycling objects that are no longer in use

5️⃣ #Prototype_Design_Pattern

▪️A fully initialized instance to be copied or cloned

6️⃣ #Singleton_Design_Pattern

▪️A class of which only a single instance can exist

@javaCode☕️
#Design_Patterns

#Structural_patterns

▪️In Software Engineering, Structural Design Patterns are Design Patterns that ease the design by identifying a simple way to realize relationships between entities.


1️⃣ #Adapter_Design_Pattern
Match interfaces of different classes

2️⃣ #Bridge_Design_Pattern
Separates an object's interface from its implementation

3️⃣ #Composite_Design_Pattern
A tree structure of simple and composite objects

4️⃣ #Decorator_Design_Pattern
Add responsibilities to objects dynamically

5️⃣ #Facade_Design_Pattern
A single class that represents an entire subsystem

6️⃣ #Flyweight_Design_Pattern
A fine-grained instance used for efficient sharing

7️⃣ #PrivateClassData_Design_Pattern
Restricts accessor/mutator access

8️⃣ #Proxy_Design_Pattern
An object representing another object

@javaCode☕️
#Design_Patterns
#Adapter_Design_Pattern

👉Intent

Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

▪️Wrap an existing class with a new interface.

▪️Impedance match an old component to a new system

@javaCode☕️
☕️JAVA Language Community
#Adapter_Design_Pattern @javaCode
#Design_Patterns
#Adapter_Design_Pattern

👉Discussion

Reuse has always been painful and elusive. One reason has been the tribulation of designing something new, while reusing something old. There is always something not quite right between the old and the new. It may be physical dimensions or misalignment. It may be timing or synchronization. It may be unfortunate assumptions or competing standards.

It is like the problem of inserting a new three-prong electrical plug in an old two-prong wall outlet – some kind of adapter or intermediary is necessary.

Adapter is about creating an intermediary abstraction that translates, or maps, the old component to the new system. Clients call methods on the Adapter object which redirects them into calls to the legacy component. This strategy can be implemented either with inheritance or with aggregation.

Adapter functions as a wrapper or modifier of an existing class. It provides a different or translated view of that class.

@javaCode☕️
#Design_Patterns

#Bridge_Design_Pattern

👉Intent

▪️Decouple an abstraction from its implementation so that the two can vary independently.

▪️Publish interface in an inheritance hierarchy, and bury implementation in its own inheritance hierarchy.

▪️Beyond encapsulation, to insulation

@javaCode☕️
#Design_Patterns

#Structural_patterns

#Composite_Design_Pattern

👉Intent

▪️Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

▪️Recursive composition

▪️"Directories contain entries, each of which could be a directory."

▪️1-to-many "has a" up the "is a" hierarchy

@javaCode☕️