Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is singleton pattern and prototype pattern

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is singleton pattern and prototype pattern". Friends who are interested might as well take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is singleton pattern and prototype pattern"!

Singleton mode

The singleton pattern should be regarded as the most commonly used design pattern, and the famous double check is also an implementation of the singleton pattern. The so-called singleton pattern is used to ensure that an object can only create one instance, in addition, it also provides global access to the instance.

Singleton pattern UML class diagram

Single case model hungry Han style

The so-called hungry Han style, people want to eat everything when they are hungry. Similarly, whether this example is needed or not, I will create it for you first.

Common implementation

Public class SingletonHungry {/ / has created a unique instance private static final SingletonHungry instance = new SingletonHungry () when the class is loaded; / / ensures that the constructor private SingletonHungry () {} / / external calls this method and returns the instance public static SingletonHungry getInstance () {return instance Other methods in the class, static public static void func () {}} as far as possible

Static code block

Public class SingletonHungryStatic {/ / actually moves the new part to the static code block private static SingletonHungryStatic instance = null; static {instance = new SingletonHungryStatic ();} private SingletonHungryStatic () {} public static SingletonHungryStatic getInstance () {return instance;} / / other methods in the class, trying to be static public static void func () {}} singleton mode slackers.

A lazy man is one who refuses to work unless he is urged. By the same token, it can only be created if you ask me for this.

Common implementation

As public class SingletonLazy {/ / volatile has said before, to prevent reordering of instructions, this volatile is private static volatile SingletonLazy instance = null; private SingletonLazy () {} public static SingletonLazy getInstance () throws Exception {if (instance = = null) {synchronized (SingletonLazy.class) {if (instance = = null) {instance = new SingletonLazy () } return instance;}}

Static inner class

Public class SingletonLazyStatic {/ * static inner class note * 1. When the class is loaded, the static inner class does not load with * 2. Static inner classes are initialized only once * 3. Thread safe * / private static class StaticClassInstance {private static final SingletonLazyStatic INSTACE = new SingletonLazyStatic ();} private SingletonLazyStatic () {} public static SingletonLazyStatic getInstance () {return StaticClassInstance.INSTACE;}}

In fact, there is a more elegant way to implement it, and that is to use enumerations, but those articles seem to say less, so this article will not be glued out to increase the cost of learning.

Client

Public class Client {public static void main (String [] args) throws Exception {/ / hungry (); / / hungryStatic (); / / lazy (); lazyStatic ();} public static void lazyStatic () {SingletonLazyStatic instance = SingletonLazyStatic.getInstance (); SingletonLazyStatic instance1 = SingletonLazyStatic.getInstance (); System.out.println (instance); System.out.println (instance1) } / / the following 3 are similar to the above implementation, except that the class name public static void lazy () throws Exception {} public static void hungry () {} public static void hungryStatic () {} prototype pattern needs to be changed.

The prototype pattern sounds classy, but it is actually a way to clone objects.

When it comes to cloning, we have to briefly talk about shallow copy and deep copy. Shallow copy means that two pointers point to the same object. As long as one of the original object and the copied object is modified, the other is modified. Deep copy refers to the re-creation of a copy object that has exactly the same content as the original object, and the two are independent. Basic data types do not participate in the copying process

Prototype: abstract prototype class that declares the interface or base class of the clone method, where the clone method must be implemented by a derived object.

Concrete Prototype: concrete implementation classes, mainly classes used to implement or extend clone methods.

Prototype pattern UML class diagram

Prototype model

Prototype

Public abstract class PrototypePhone implements Cloneable {private String cpu; private int price; @ Override public Object clone () throws CloneNotSupportedException {return super.clone ();} / / omit constructor, get,set method}

Concrete Prototype

Public class ConcretePrototypeOnePlus extends PrototypePhone {public ConcretePrototypeOnePlus (String cpu, int price) {super (cpu, price); System.out.println ("constructor called");} @ Override public Object clone () throws CloneNotSupportedException {return super.clone ();}}

Client

Public class Client {public static void main (String [] args) throws CloneNotSupportedException {PrototypePhone phone = new ConcretePrototypeOnePlus (3999); System.out.println ("prototype:" + phone); for (int I = 0; I < 100; iTunes +) {System.out.println ("production" + (I + 1) + "mobile phone:" + phone.clone ();})

If you run it, you will find that the constructed phone has only passed the constructor once, and other mobile phone instance objects can also be constructed using phone.clone ().

At this point, I believe you have a deeper understanding of "what is singleton pattern and prototype pattern". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report