Java design pattern "II"

Source: Internet
Author: User

The previous article introduced a design pattern of the responsibility chain model, this chapter for you about the design pattern of the single-case pattern and template method mode. The role of the singleton mode is to ensure that an instance of the application has only one, and that the singleton pattern is divided into: the pattern of satiety and a hungry man, the difference between the two is that the A hungry man mode does not explicitly give an instance of the object. Let's take a look at the full-Han mode and the A Hungry man mode in the singleton mode.

1, full-han mode:

When we create the object, Java will default to provide us with a default construction method, since the singleton mode to ensure that an object in the program has only one instantiation, so we have to define our construction method: Private, how we get the object in the program? This is a way for us to provide a method to obtain an instance of the object, which is obtained by calling the method from the class when it needs to obtain an instance object externally.

/** Singleton mode---full-han mode * Ensure that an instance of the entire application has and can have only one*/ Public classOne {//The first step is to define a non-parametric construction method that is privatized and does not allow external direct creation of objects    PrivateOne () {}//define one object    Private StaticOne one =NewOne (); //provides a method for externally invoking an object that is defined by static as a global method     Public StaticOne GetOne () {returnOne ; }    }

2. A Hungry man mode:

The idea of a hungry man mode is basically the same as the full-Han mode, except that the A hungry man mode does not provide an explicit object instance.

/** Singleton Mode---Lazy mode * The role of Singleton mode: to ensure that an instance of the entire application has and can have only one*/ Public classBoth {//The first step is to define a non-parametric construction method that is privatized and does not allow external direct creation of objects    PrivateBoth () {}//declares a single object    Private Staticthe other ; //provides a method for externally invoking an object that is defined by static as a global method     Public StaticGettwo () {if(two!=NULL) {=Newboth (); }        returnboth ; }}

3. Test:

 Public classTest {/**     * @paramMode Test*/     Public Static voidMain (string[] args) {//Full-han modeOne one1 =One.getone (); One One2=One.getone (); if(one1==One2) {System.out.println ("Two objects consistent"); }Else{System.out.println ("Two objects Inconsistent"); }                //a hungry man modeTWO1 =Two.gettwo (); TWO2=Two.gettwo (); if(two1==TWO2) {System.out.println ("Two objects consistent"); }Else{System.out.println ("Two objects Inconsistent"); }            }}

Results:

  

Now let's learn about the template method pattern, the template method pattern is the same as when we design the building, we keep the traditional design process on the basis of some places to design innovation, and finally achieve our ideal design effect.

How can I introduce it to you? Below, we will introduce a template method pattern for you as a prototype of a beverage machine. Suppose our beverage machine can produce two kinds of drinks: one coffee, the other green tea, both of which have a refreshing effect and are essential for the application of apes. Haha, this is when you take this cup to the drink machine, click I want a cup of coffee, this is a drink machine to provide you with a cup of coffee; When you click on a cup of green tea, the beverage machine offers us a cup of green tea. What's the matter? Let's implement it in code:

1. Create a beverage machine class:

/** Template Method mode*/ Public Abstract classRobot { Public voidZhushui () {System.out.println ("Boil the water."); }     Public Abstract voidChong ();  Public voidDaoshui () {System.out.println ("Pour the drink into the cup."); }     Public Abstract voidAdd ();}

2. Create a coffee machine class:

 Public class extends Robot {    publicvoid  Add () {        System.out.println ("Add Milk");    }      Public void Chong () {        System.out.println ("Brew Coffee");}    }

3. Create a green Tea machine class:

 Public class extends Robot {    publicvoid  Add () {        System.out.println ("Add lemon");    }      Public void Chong () {        System.out.println ("Brew Tea");}    }

4. Test code:

 Public classTest {/**     * @paramTemplate Method Pattern testing*/     Public Static voidMain (string[] args) {Robot robot1=Newcoffer (); Robot1.        Zhushui (); Robot1.        Chong (); Robot1.        Daoshui (); Robot1.                ADD (); System.out.println ("**********************"); Robot Robot2=NewTea (); Robot2.        Zhushui (); Robot2.        Chong (); Robot2.        Daoshui (); Robot2.                ADD (); }}

Test results:

  

OK to here the singleton mode and template method pattern for the schema in Java is introduced. Next: Adapter mode and factory method mode

  

Java design pattern "II"

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.