使用AppDelegate进行数据操作和处理是一种好习惯吗? [英] Is it good practice to use AppDelegate for data manipulation and Handling?

查看:89
本文介绍了使用AppDelegate进行数据操作和处理是一种好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个 AppDelegate 的对象,并在整个程序中使用它,我已经声明了所有的setter和getter,还插入,选择,删除,更新了查询其中包含数据库。

I am making an object of AppDelegate and using it throughout my program, and I have declared all setters and getters, and also insert, select, delete, update queries of database in it.

我想问一下这是一个好习惯,如果是,那么是
,然后是如何,如果不是,为什么它是不是一个好习惯?

I want to ask that is it a good practice to do so, if yes, then how, and if no then why it is not a good practice?

我希望我的问题很清楚,如果你有问题,请询问相关问题。

I hope my question is clear, please ask relating questions if you have any.

推荐答案

把你的AppDelegate变成一个包含一百万个方法和属性的泥球大并不是一个好策略(尽管它可能很诱人)。

It's not a good strategy to turn your AppDelegate into a "big ball of mud" that contains a million methods and properties (although it might be tempting).

一种更好,更面向对象的方法,可以将功能分成精心设计的对象 - 例如,您可能有一个类DatabaseManager来处理所有数据库交互。然后,您可能需要应用程序的一些部分需要DatabaseManager向app委托实例请求对DatabaseManager的引用。

A better and more object oriented approach to section off bits of functionality into well-designed objects -- for example you might have a class DatabaseManager which handles all database interactions. You might then have bits of your app which need the DatabaseManager ask the app delegate instance for a reference to a DatabaseManager.

或者,您可以将对DatabaseManager的引用传递给需要它的应用程序部分。然而,最后一种方法会导致更多的界面污染,您必须在许多地方修改接口才能传入DatabaseManager。

Alternatively, you can pass around a reference to the DatabaseManager to the parts of the app that need it. This last approach does however cause more 'interface pollution', where you have to modify interfaces in lots of places in order to pass in the DatabaseManager.

还有另一种选择有效地使您的DatabaseManager本身成为单例 - 通过类上的类方法访问它的实例。以这种方式工作的单身人士往往不满意,并且通常有充分的理由(使测试变得更难,那种事情)。我倾向于避免让对象将单身性质直接烘焙到对象中 - 如果我需要那种东西,我更喜欢拥有一个已知的访问点(如果你喜欢的那种'工厂')你在哪里可以去获取共享实例。

And yet another alternative would be to effectively make your DatabaseManager itself be a 'singleton' -- whereby an instance of it is accessed via a class method on the class. Singletons that work in this way are often frowned upon, and usually for good reasons (makes testing harder, that sort of thing). I tend to avoid having objects have their 'singleton' nature baked right into the object -- I prefer, if I need that sort of thing, to have a known point of access (a kind of 'factory' if you like) where you can go to obtain a shared instance.

这篇关于使用AppDelegate进行数据操作和处理是一种好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆