制作视图时获得最佳性能
我正在开发一个基于 TabBarController
的应用程序。 我有两个执行类似操作的视图。 我的问题是,我应该创建两个不同的类,还是应该只使用一个类,并带有一些“if”语句,询问一个类是否是其中之一。我需要在这方面发挥最大的性能。 这两个视图加载一个 MKMapView
,因此我需要知道是只加载一个完成整个任务的对象更好,还是加载两个完成类似任务的对象更好。
谢谢!
I'm developing an app based on a TabBarController
.
I have 2 views that do some similar actions.
My question is, should I make 2 different classes or should I use only one class with some "if" statements asking if is a class is one or the other. I need maximum performance on this.
The 2 views load a MKMapView
, so I need to know if it is better to load just one object that does the entire thing, or two objects that do similar things.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在面向对象中,记住类和该类的实例之间的区别很重要。
如果您发现自己正在考虑在类中编写一些代码,上面写着“我是什么类?如果我是 X 类,则执行 A 操作;否则执行 B 操作”——不要这样做!这是一个典型的问题,需要一个好的面向对象的解决方案。在这种情况下,有两种常见的解决方案:
1) 编写一个类,在实例化时传递一些重要信息,以便稍后使用。然后代码的另一部分是以正确的方式配置此类的实例 - 例如,在您的问题中,创建了此类的两个实例,每个实例都有不同的信息位(也许是地图位置?)传递到 init 方法中
2) 编写一个超类,它有两个子类,专门处理超类的一般行为。因此,您的大部分逻辑和代码都位于超类中 - 假设它称为 MapDisplayViewController - 但随后您使用两个名为(例如)MapDisplayViewControllerA 和 MapDisplayViewControllerB 的子类扩展该类,这两个子类以重要的不同方式重写一个或多个方法以区分它们。
对于你的问题,听起来方法 1) 会很好。
代码中写着“我属于哪一类?”通常是“代码异味”的一个很好的例子——换句话说,这是某件事的迹象可以设计得更好。
In Object Orientation it's important to bear in mind the difference between a class and instances of that class.
If you ever find yourself thinking of writing some code in a class that says "What class am I? If I'm class X, do thing A; otherwise do thing B" -- don't! It's a classic problem begging for a nice object oriented solution. There are two common solutions in this kind of situation:
1) Write a single class that at instantiation time gets passed in some vital information that it then uses later. Then another part of your code is making instances of this class configured in the correct way -- e.g. in your problem, two instances of this class get made, each with a different bit of info (map location perhaps?) passed into the init method
2) Write a superclass that has two subclasses that specialise the general bahaviour of the superclass. So most of your logic and code goes in the superclass - suppose it's called MapDisplayViewController - but then you extend this class with two subclasses called (for example) MapDisplayViewControllerA and MapDisplayViewControllerB that override one or more methods in important, different ways to differentiate them.
For your problem it sounds like approach 1) would be good.
Having code which says "What class am I?" is often a good example of a 'code smell' -- in other words, a sign that something could be designed much better.
我会说加载两个对象。如果 iOS 需要更多资源,它会自动卸载当前未显示的视图。 (当然,假设您正确实现了 viewDidLoad 和 viewDidUnload )。
此外,如果您的视图在切换选项卡时需要初始化/加载大量数据,并且常见流程涉及用户频繁地从一个选项卡切换到另一个选项卡,则应用程序可能会在频繁加载期间出现滞后,如果您使用只有 1 个对象。没有人喜欢漫长而频繁的加载时间。
不过,根据您原始帖子提供的信息,这只是我的意见。许多其他因素仍然会发挥作用。
I would say load two objects. iOS will automatically unload your currently not displayed views if it needs more resources anyway. (Assuming you implemented viewDidLoad and viewDidUnload properly, of course).
In addition, if in case your view needs to initialize/load a lot of data when tab is switched, and the common flow involves the user switching from one tab to another frequently, the app may appear to lag during frequent loading, if you use only 1 object. No one likes long and frequent loading times.
Just my opinion though, based on the information your original post provides. A lot of additional factors can still come into play.