首先,android 设计模式的六大原则:
1.单一职责原则(一个类尽量只干一件事)
2.开放封闭原则(指的是一个类,模块对修改关闭对扩展开放,自己觉得这个是比较难以兼顾的,面对需求的变动很把模型搭建的是对修改关闭的)
3.里氏替换原则(使用父类的地方能够用子类替换,反过来,则不行)
4.依赖倒置原则(抽象不应该依赖细节,细节应该依赖抽象,即面向接口编程,而不应该面向具体的实现编程)
5.接口隔离原则(一个类对另一个类的依赖应该建立在最小的接口上)
6.迪米特法则(一个类尽量不要和其他类发生关系,即一个类尽量和另一个类的耦合少)
1.单例模式
一个类在程序运行起来后就只有一个对象,当把程序杀死后,这个对象消失,运用它的好处是它就像一个全局变量,一处修改了这个类里面的方法,那么其他的修改会在它的基础继续修改,可以保证数据的连贯性。
我自己通常写的是线程不安全:
public class Singleton {private static SingletonmSingleton =null; public static SingletongetInstance(){if(mSingleton ==null){return new Singleton(); }return mSingleton; }}
在阅读了相关的文档后发现,比较好的写法是静态了内部类的方式:
public class Singleton {private static SingletonmSingleton =null; public static SingletongetInstance(){return SingletonHolder.sInstance; }//静态内部类 private static class SingletonHolder{private final static SingletonsInstance =new Singleton(); }}
2.简单工厂模式
具体的工厂,抽象的产品,很多具体的产品,适用于处理由一个变量分散出来不同的处理结果的问题。
具体的例子如下:
public class Factory {public static final StringTAG ="Factory"; Factprocess(String factName){switch (factName){case "a":return new FactA(); case "b":return new FactB(); default:break; }return null; }}
public class FactAextends Fact{@Override void show() { Log.d(Factory.TAG,"I am fact a"); }}
public class FactBextends Fact{@Override void show() { Log.d(Factory.TAG,"I am fact b"); }}
public abstract class Fact {abstract void show();}
调用点:
new Factory().process("a").show();new Factory().process("b").show();
结果:
07-07 16:04:44.626 15656-15656/com.example.administrator.mycall D/Factory: I am fact a
07-07 16:04:44.627 15656-15656/com.example.administrator.mycall D/Factory: I am fact b
缺点:当有很多的类型的产品时,就会产生很多的具体的产品,并且工厂类的方法会出现很多if分支。
3.策略模式
抽象策略,具体的策略,策略的转换类。
例子如下:
public abstract class Stragety {abstract void talk();}
public class MeetColleagueStragetyextends Stragety {@Override void talk() { Log.d(Factory.TAG,"hello,Have you had dinner today?"); }}
public class MeetLeaderStragetyextends Stragety{@Override void talk() { Log.d(Factory.TAG,"xx leader , hello"); }}
public class StragetyProcesser { StragetymStragety; StragetyProcesser(Stragety mStragety){this.mStragety = mStragety; }public void process(){mStragety.talk(); }}
测试代码:
new StragetyProcesser(new MeetColleagueStragety()).process();new StragetyProcesser(new MeetLeaderStragety()).process();
结果输出:
07-07 16:28:37.032 28351-28351/com.example.administrator.mycall D/Factory: hello,Have you had dinner today?
07-07 16:28:37.032 28351-28351/com.example.administrator.mycall D/Factory: xx leader , hello
优点:满足开放封闭原则,可以很好的扩展。
4.观察者模式
被观察者和观察者,主要体现在观察者并不知道被观察者什么时候有动作,所以用观察的形式去监听动作的变化然后去做相应的处理
例子:
public interface TouchLinstener {void action();}
public class MainActivityextends AppCompatActivityimplements TouchLinstener {private TextVieweditText; private Buttonbutton; private View1mView1; @Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (TextView)findViewById(R.id.edit_number); editText.setText("131xxxx8662"); button = (Button) findViewById(R.id.button); mView1= (View1) findViewById(R.id.view1); mView1.registerTouchListener(this); }@Override protected void onResume() {super.onResume();// new Thread(new Runnable() {// @Override// public void run() {//// button.setOnClickListener(new View.OnClickListener() {//// @Override//// public void onClick(View v) {//// while (true) {// String callNumber = editText.getText().toString().trim();// Intent intent = new Intent();// intent.setAction(Intent.ACTION_CALL);// intent.setData(Uri.parse("tel:" + callNumber));// startActivity(intent);//// }//// });//// }// }// }).start(); new Factory().process("a").show(); new Factory().process("b").show(); new StragetyProcesser(new MeetColleagueStragety()).process(); new StragetyProcesser(new MeetLeaderStragety()).process(); }@Override public void action() { Log.d(Factory.TAG,"user touch view1"); }@Override protected void onDestroy() {mView1.unregisterTouchListener(); }}
结果:
在用户触摸了屏幕上紫色的区域就会打出log:
07-07 16:44:42.721 5748-5748/com.example.administrator.mycall D/Factory: user touch view1
07-07 16:44:42.741 5748-5748/com.example.administrator.mycall D/Factory: user touch view1
07-07 16:44:42.758 5748-5748/com.example.administrator.mycall D/Factory: user touch view1
07-07 16:44:42.809 5748-5748/com.example.administrator.mycall D/Factory: user touch view1
07-07 16:44:42.828 5748-5748/com.example.administrator.mycall D/Factory: user touch view1
07-07 16:44:42.842 5748-5748/com.example.administrator.mycall D/Factory: user touch view1
07-07 16:44:42.859 5748-5748/com.example.administrator.mycall D/Factory: user touch view1
07-07 16:44:42.866 5748-5748/com.example.administrator.mycall D/Factory: user touch view1
5.工厂方法
6.抽象工厂模式
两者都是抽象的产品,多个具体的产品,抽象的工厂,多个具体的工厂,区别在于工厂方法中的工厂只生产唯一一种产品,而抽象工厂模式中的工厂可以生产很多产品。
工厂方法的代码示例:
public abstract class Factory {abstract Productcreate();}
public class FactoryAextends Factory{@Override Productcreate() {return new ProoduceA(); }}
public class FactoryBextends Factory {@Override Productcreate() {return new ProductB(); }}
public abstract class Product {abstract void show();}
public class ProductAextends Product{@Override void show() { Log.d(Factory.TAG,"ProductA"); }}
public class ProductBextends Product{@Override void show() { Log.d(Factory.TAG,"ProductB"); }}
测试代码:
public class Test {public static void method(){ Factory factory =null; factory =new FactoryA(); factory.create().show(); factory =new FactoryB(); factory.create().show(); }}
测试结果:
07-07 17:30:45.992 32199-32199/com.example.administrator.mycall D/Factory: ProductA
07-07 17:30:45.993 32199-32199/com.example.administrator.mycall D/Factory: ProductB
抽象工厂方法的代码示例:
有两个产品,product和product1.生产出product的有两个具体的产品productA,productB,
生产出product1的两个具体的产品product1A,product1B.抽象的工厂生产两个产品分别是product和product1
public abstract class Factory {abstract Productcreate(); abstract Product1create1();}
public class FactoryAextends Factory{@Override Productcreate() {return new ProductA(); }@Override Product1create1() {return new Product1A(); }}
public class FactoryBextends Factory {@Override Productcreate() {return new ProductB(); }@Override Product1create1() {return new Product1B(); }}
public abstract class Product {abstract void show();}
public abstract class Product1 {abstract void process();}
public class Product1Aextends Product1{@Override void process() { Log.d(Factory.TAG,"Product1A"); }}
public class Product1Bextends Product1{@Override void process() { Log.d(Factory.TAG,"Product1B"); }}
public class ProductAextends Product{@Override void show() { Log.d(Factory.TAG,"ProductA"); }}
public class ProductBextends Product{@Override void show() { Log.d(Factory.TAG,"ProductB"); }}
测试代码:
public class Test {public static void method(){ Factory factory =null; factory =new FactoryA(); factory.create().show(); factory.create1().process(); factory =new FactoryB(); factory.create().show(); factory.create1().process(); }}
测试结果:
07-07 17:38:34.447 5129-5129/com.example.administrator.mycall D/Factory: ProductA
07-07 17:38:34.448 5129-5129/com.example.administrator.mycall D/Factory: Product1A
07-07 17:38:34.449 5129-5129/com.example.administrator.mycall D/Factory: ProductB
07-07 17:38:34.450 5129-5129/com.example.administrator.mycall D/Factory: Product1B