Android design mode--singleton mode (Singleton)

Source: Internet
Author: User

There are three main categories of 23 design patterns:

Create five types of models: Factory method mode, abstract Factory mode, singleton mode, builder mode, prototype mode.

Structure mode, a total of seven kinds: Adapter mode, adorner mode, proxy mode, appearance mode, bridging mode, combined mode, enjoy the meta-mode.

There are 11 types of behavioral Patterns: Strategy mode, template method mode, observer mode, iteration sub-mode, responsibility chain mode, Command mode, Memo mode, state mode, visitor mode, mediator mode, interpreter mode.

1  PackageCom.example.main;2 3 Importandroid.app.Activity;4 ImportAndroid.content.Context;5 ImportAndroid.os.Bundle;6 Importandroid.widget.LinearLayout;7 ImportAndroid.widget.TextView;8 9 /*Ten * Android design mode--singleton mode (Singleton) One  */ A  -  Public classSingletonextendsActivity { -  the     PrivateLinearLayout ly; -     Privatelinearlayout Sly; -      - @Override +     protected voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.create); A          atly =(LinearLayout) Findviewbyid (r.id.creately); -Sly =(LinearLayout) Findviewbyid (r.id.singly); -          -Android Google =NewAndroid ("Google", Ly, This); - google.setname (); -          inAndroid Huawei =NewAndroid ("Huawei", Ly, This); - huawei.setname (); to          +         //First instantiation -iOS iOS = ios.getinstance ("Apple", Sly, This); the ios.setname (); *          $         //Second CallPanax NotoginsengIOS Samsung = ios.getinstance ("Samsung", Ly, This); - samsung.setname (); the     } +      A     /* the * Android Manufacturer +      */ -      $     classandroid{ $          -         PrivateString name; -         PrivateLinearLayout ly; the         PrivateTextView TV; -         Privatecontext context;Wuyi          the          PublicAndroid (String name,linearlayout ly,context Context) { -              This. Name =name; Wu              This. ly =ly; -              This. Context =context; About         } $          -          Public voidSetName () { -TV =NewTextView (context); -              This. Tv.settext (name + "Android Device"); A              This. Ly.addview ( This. TV); +         } the     } -      $     /* the * Apple Manufacturers the      */ the      the     Static classios{ -          in         PrivateString name; the         PrivateLinearLayout ly; the         PrivateTextView TV; About         Privatecontext context; the          the         //Prohibit references the          +         Private StaticIOS instance =NULL; -          the         //private constructors to prevent instantiation. Bayi          the         PrivateIOS () {} the          -         PrivateIOS (String name,linearlayout ly,context Context) { -              This. Name =name; the              This. ly =ly; the              This. Context =context; the         } the          -         //Create an instance the          the          Public StaticIOS getinstance (String name,linearlayout ly,context Context) { the             94             if(Instance = =NULL) { theInstance =NewIOS (name,ly, context); the             } the             returninstance;98         } About          -          Public voidSetName () {101TV =NewTextView (context);102Tv.settext ("iOS only belongs to" +name+ "Company");103 Ly.addview (TV);104         } the     }106}
View Code

Android design mode--singleton mode (Singleton)

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.