Java Software design pattern only single example design mode

Source: Internet
Author: User

Overview

Design patterns are a set of reusable, most known, categorized purposes, code design experience Summary. Design patterns are used in order to reuse code, make code easier for others to understand, and ensure code reliability. There is no doubt that design patterns in others in the system are multi-win; Design patterns make code production truly engineering; Design patterns are the cornerstone of software engineering, like the structure of a building.

The design pattern is divided into three types, a total of 23
Create pattern: Singleton mode, abstract Factory mode, builder mode, Factory mode, prototype mode.
Structural mode: Adapter mode, bridge mode, decoration mode, combination mode, appearance mode, enjoy meta mode, proxy mode.

Behavioral Patterns : Template method mode, command mode, iterator mode, observer pattern, Mediator mode, Memo mode, interpreter mode, state mode, policy mode, responsibility chain mode, visitor pattern.

Let's take a look at the example of the first singleton pattern today.

Sometimes when our server accesses a huge amount of time, using the ordinary class method to create an object for each access user, while providing the same functionality, it will greatly occupy the storage space of the server, while the loss of CPU, this time, we would like to have only one object in the server for all method calls, This is the singleton design pattern.

The singleton design pattern is divided into two types:

    1. A Hungry man design mode
    2. Lazy design mode

Let's take a look at the A Hungry Man design mode, first look at the example

  

/** * @author MyPc single-case design mode */class Test {static test test = new test ();//Privatization construction Method Private Test () {System.out.println ("This is a singleton design Mode ");} public static Test Gettest () {return test;} Main method public static void main (string[] args) {Test T1 = test.gettest (); Test t2 = test.gettest (); if (t1 = = t2) {System.out.println ("same as");}}}

In this single design pattern, we can see a very important step, that is, the privatization of the construction method.

We know that when a class object is new, the constructor of the class is called at the same time, so the default construction method is public, and in singleton design mode, we set the class's construction method to private, which makes it impossible for other classes to create objects of that class. At the same time we create a static class of its own object within the class, and provide a static method to call this object, so that the object of the class will always have only one. This is a hungry man singleton mode, let's look at the Lazy Singleton mode:

/** * @author MyPc single case design mode */class Test {static Test test;//privatization construction Method Private test () {System.out.println ("This is a singleton design mode");} public static Test Gettest () {if (test = = null) {test = new test ();} return test; Main method public static void main (string[] args) {Test T1 = test.gettest (); Test t2 = test.gettest (); if (t1 = = t2) {System.out.println ("same as");}}}

Compared to a hungry man singleton mode, the hungry Hantanli mode will have a problem, that is always in memory of this object, whether or not used, if not used will also cause the waste of resources, this time lazy design mode in a single-threaded to avoid this problem, We can see in the example that we have not created an object in the beginning, just declare the object of the class, when the need for a specific call to determine whether the object of the class already exists, if present, return the object, if it does not exist, new one returns the object.

The singleton design pattern can be extremely resource-efficient in some cases, and is ideal for use in heavily visited websites.

Java Software design pattern only single example design mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.