美文网首页
2018-07-07

2018-07-07

作者: 小西西222号 | 来源:发表于2018-07-07 18:24 被阅读0次

首先,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

相关文章

  • 2018-07-07

    2018-07-07 哈利波特二代 2018-07-07 18:13 · 字数 686 · 阅读 0 · 日记本 ...

  • 2018-07-09

    2018-07-07 孟傑萨霸 2018-07-07 22:46 · 字数 116 · 阅读 23 · 日记本 1...

  • 路过看花开花落

    原作:最美诗行 2018-07-07 21:30 · 字数 277 · 阅读 0 · 日...

  • (码友推荐)2018-07-07 .NET及相关开发资讯速递

    (码友推荐)2018-07-07 .NET及相关开发资讯速递: 1.Different Ways to Compa...

  • 无标题文章

    --- title: 使用Chrome来做一张网页长截图 tag: code date: 2018-07-07 -...

  • 📖2018-07-07 江城笔记7

    ?2018-07-07 江城阅读笔记7 ✏️表达积累: 1. 社会主义事业Socialism’s undertak...

  • 日精进打卡(第365)

    2018-07-07 姓名:李义 公司:........ 组别:259期利他二组 【知~学习】 背诵 六项精进大纲...

  • 2018-07-07

    讨厌变喜欢 哈利波特二代 2018-07-07 17:50 · 字数 614 · 阅读 0 · 日记本 我...

  • 2018-07-08

    文章标题:2018-07-07《为何家会伤人》之二——认识爱文章作者:嘟宝妈妈_悠悠文章链接:https://ww...

  • 关于43-44节《时间管理100讲》听后笔记

    2018-07-07 一、做正确的事是根本。--“高空” 文摘: “人生高空”,你也可以把这个理念称为人生的宗旨、...

网友评论

      本文标题:2018-07-07

      本文链接:https://www.haomeiwen.com/subject/aneguftx.html