Singleton mode/factory method mode/abstract Factory mode

Source: Internet
Author: User

Single-Case mode:

public class Person2 {//      a hungry man mode//private static Person2 person = new Person2 ();//private Person2 () {}//public static Per Son2 Getinstacne () {//return person;//}//static inner class--and external class to invoke//static--as the class loads and generates private static class personholder{ private static final Person2 INSTANCE = new Person2 ();} Private Person2 () {}public static Person2 GetInstance2 () {return personholder.instance;}      lazy mode//private static Person2 person2;//private Person2 () {}//public static synchronized Person2 getinstace () {// if (person==null) {//person = new Person2 ();//}//return person;//}}



The factory method takes a class inheritance mechanism, generates a subclass, overrides the factory method, and generates an object within the method.


Abstract factories employ an object-grouping mechanism that specifically defines "factory" objects to be responsible for the creation of objects

Simple Use cases:

interface icar {void Docar ();} Class BWM implements icar{@Overridepublic void Docar () {System.out.println ("BWM");}} Class BK implements icar{@Override//Factory method takes a class inheritance mechanism (generates a subclass, overrides the factory method, produces an object in the method) public void Docar () {System.out.println ("BK");}} Interface Icarfac{icar Createcar ();} Class BWMFAC implements icarfac{@Override//Abstract factory employs an object composition mechanism that specifically defines a "factory" object to be responsible for creating the object public  BWM Createcar () {return new BWM ();}} Class BKFAC implements icarfac{@Overridepublic  BK Createcar () {return new BK ();}} Abstract factory for multiple product grades results public class Test{public static void Main (string[] args) {ICARFAC MFAC = new Bwmfac (); icar m = mfac.create Car ();//The factory class is responsible for creating an instance of the specific subclass of the abstract product M.docar (); MFAC = new Bkfac (); icar k = Mfac.createcar (); K.docar ();}}


Singleton mode/factory method mode/abstract Factory mode

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.