Android设计模式——单例模式(Singleton)

二十三种设计模式分为三大类:

创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。

结构型模式,共七种:适配器模式、装饰器模式、代理模式、外观模式、桥接模式、组合模式、享元模式。

行为型模式,共十一种:策略模式、模板方法模式、观察者模式、迭代子模式、责任链模式、命令模式、备忘录模式、状态模式、访问者模式、中介者模式、解释器模式。 

  1 package com.example.main;
  2 
  3 import android.app.Activity;
  4 import android.content.Context;
  5 import android.os.Bundle;
  6 import android.widget.LinearLayout;
  7 import android.widget.TextView;
  8 
  9 /*
 10  * Android设计模式——单例模式(Singleton)
 11  */
 12 
 13 public class Singleton extends Activity {
 14 
 15     private LinearLayout ly;
 16     private LinearLayout sly;
 17     
 18     @Override
 19     protected void onCreate(Bundle savedInstanceState) {
 20         super.onCreate(savedInstanceState);
 21         setContentView(R.layout.create);
 22         
 23         ly = (LinearLayout) findViewById(R.id.creately);
 24         sly = (LinearLayout) findViewById(R.id.singly);
 25         
 26         Android google = new Android("谷歌",ly,this);
 27         google.setName();
 28         
 29         Android huawei = new Android("华为",ly,this);
 30         huawei.setName();
 31         
 32         //第一次实例化
 33         IOS ios = IOS.getInstance("苹果", sly, this);
 34         ios.setName();
 35         
 36         //第二次调用
 37         IOS samsung = IOS.getInstance("三星", ly, this);
 38         samsung.setName();
 39     }
 40     
 41     /*
 42      * Android厂商
 43      */
 44     
 45     class Android{
 46         
 47         private String name;
 48         private LinearLayout ly;
 49         private TextView tv;
 50         private Context context;
 51         
 52         public Android(String name,LinearLayout ly,Context context){
 53             this.name = name;
 54             this.ly = ly;
 55             this.context = context;
 56         }
 57         
 58         public void setName() {
 59             tv = new TextView(context);
 60             this.tv.setText(name + "的Android设备");
 61             this.ly.addView(this.tv);
 62         }
 63     }
 64     
 65     /*
 66      * 苹果厂商
 67      */
 68     
 69     static class IOS{
 70         
 71         private String name;
 72         private LinearLayout ly;
 73         private TextView tv;
 74         private Context context;
 75         
 76         //禁止引用
 77         
 78         private static IOS instance = null;
 79         
 80         //私有构造函数,防止被实例化。
 81         
 82         private IOS(){}
 83         
 84         private IOS(String name,LinearLayout ly,Context context){
 85             this.name = name;
 86             this.ly = ly;
 87             this.context = context;
 88         }
 89         
 90         //创建实例
 91         
 92         public static IOS getInstance(String name,LinearLayout ly,Context context){
 93             
 94             if (instance == null) {
 95                 instance = new IOS(name,ly, context);
 96             }
 97             return instance;
 98         }
 99         
100         public void setName() {
101             tv = new TextView(context);
102             tv.setText("IOS只属于"+name+"公司");
103             ly.addView(tv);
104         }
105     }
106 }
View Code

 

转载于:https://www.cnblogs.com/yuge790615/p/4783621.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值