Home > Java > javaTutorial > What are the creational patterns in java?

What are the creational patterns in java?

coldplay.xixi
Release: 2020-06-30 10:18:54
Original
3023 people have browsed it

What are the creational patterns in java?

#What are the creational patterns in java?

java creation patterns include:

1. PROTOTYPE example: Chat with MM on QQ, be sure to say something affectionate Words, I have collected love words, and when needed, just copy them and put them in QQ.

This is the prototype of my love words.

Primitive model mode: Specify the type of object to be created by giving a prototype object, and then create more objects of the same type by copying the prototype object. The original model mode allows the dynamic addition or reduction of product categories. The product categories do not need to have any predetermined hierarchical structure. The original model mode is suitable for any hierarchical structure. The shortcoming is that every class must be equipped with a clone method.

2. FACTORY example:

It is indispensable to treat girls to a meal. McDonald’s chicken wings and KFC chicken wings are both girls’ favorite food. Although the taste is different, no matter what you bring

MM goes to McDonald's or KFC, just say to the waiter "Four chicken wings". McDonald's and KFC are factories that produce chicken wings. Factory pattern: client classes and factory classes are separated. Any time a consumer needs a certain product, they just need to request it from the factory. Consumers can accept new products without modification. The disadvantage is that when the product is modified, the factory class must also be modified accordingly. Such as: how to create and how to provide it to the client.

3. FACTORY METHOD example:

Invite MM to McDonald’s to eat hamburgers. Different MMs have different tastes. It is annoying to remember each one. I usually use

Factory Method mode, take MM to the waiter and say "I want a burger". As for what kind of burger you want, just ask MM to tell the waiter directly.

Factory method pattern: The core factory class is no longer responsible for the creation of all products, but leaves the specific creation work to subclasses, becoming an abstract factory role, only responsible for giving out instructionsThe interface that the body factory class must implement without touching the details of which product class should be instantiated.

4. SINGLETON example:

There are 6 beautiful wives, and their husbands are all me. I am the husband Sigleton in our family. As long as they say "husband", they all refer to it. The same

person, that’s me. Singleton mode: The singleton mode ensures that a certain class has only one instance, and it instantiates itself and provides this instance to the entire system. The singleton pattern should only be used when there is a true "single instance" requirement.

Recommended tutorial: "java video tutorial"

The above is the detailed content of What are the creational patterns in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template