Several simple design patterns of Java

Source: Internet
Author: User

"Singleton Mode"

The primary purpose of the singleton design pattern is to have only one instance of the class present in the entire system. such as global configuration information, a factory, or a master control class

 Public class Singleton {   privatestaticnew  Singleton;            Private Singleton () {}     Public Static Singleton getinstance () {        return  Singleton;   }    }

"Factory mode"

Three elements: interfaces, classes that implement interfaces, factory classes

InterfaceCar { Public voidrun ();  Public voidstop ();}classBenzImplementsCar { Public voidrun () {System.out.println ("Benz started----------------."); }     Public voidStop () {System.out.println ("Benz stopped----------------."); }  }classFordImplementsCar { Public voidrun () {System.out.println ("Ford starts----------------."); }     Public voidStop () {System.out.println ("Ford stopped----------------."); }  }classFactory { Public Staticcar getcarinstance (String type) {car car=NULL; Try{car= (Car) class.forname ("package.") +type). newinstance (); } Catch(Exception e) {e.printstacktrace (); }    }}            

Several simple design patterns for Java

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: [email protected] 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.