Quite a broad question. I'm looking to refactor an existing game app so that we have a base framework for games, which are extended with some kind of modular plugin architecture. So we have:
GameCore - lots of base classes, etc.
GameBundle A/B/C... - bundles with lots of subclasses of those in the core, along with lots of resources
The game won't function at all without a concrete bundle which implements the game itself. In theory it would also be possible to break down these bundles further and mix and match. This is a very appealing design.
Using NSBundle seems to be the neatest solution, but I'm hitting some snags: The code in the GameBundle must be linked with the GameCore or it won't compile, but then when the GameCore code is loaded from the NSBundle, you get duplicates of all the core classes. This suggests that I'm way off the mark with how I'm approaching this!
This could easily be implemented with static libs, but then it's not possible to easily include resources...
So, could anyone suggest a workable architecture that would do as I wish? To summarise;
Abstract game core
Indeterminate number of GameBundles added to the target, which are (ideally) loaded dynamically at runtime.
Hope someone can help!
Related
I am new to iOS development, so please excuse me in case my question sounds trivial.
I am planning to introduce MVP or VIPER pattern. I am from Web background and in web application we used to follow different layers/dlls (for model, view, common etc.). Does it make sense to follow the same layered approach (frameworks in this case) in iOS and or it will be overkill? I have not come across any sample project (based on MVP) which create different framework for model, View and Presenters? It would be great if someone can share any sample project (based on MVP) which we can use as a starter project.
Thanks In Advance
In iOS, we generally use MVC - Model/View/Controller. So, if you're looking for the analog in the iOS world, that's it. If you're saying that you've really dug into MVC and there's something that you find deeply unsatisfying, and therefore want to introduce a different pattern, then that's a different question.
So, no, the sort of logical separation of responsibilities that you see in approaches like MVC is not overkill. In fact, the division of "view" and "controller" classes are fairly central to everything we do (e.g. UIView subclasses, UIViewController subclasses, etc.), so the only thing you really have to do is design appropriate "model" classes and you're off to the races.
But, in answer to your question, you don't generally use separate framework targets for the model, the views, and the controllers. That probably would be overkill. Usually you can keep track of everything through through judicious use of Xcode groups/folders within your project. We generally only pull stuff into frameworks for more technical or logistical reasons (live views, app extensions, sharing code between multiple apps, etc. ... see WWDC 2015 Building Modern Frameworks for discussions of when you'd generally use frameworks).
But to the question of MVC vs MVP or VIPER (or MVVM or whatever). I'd suggest that you simply embrace the shared spirit of all of these patterns (namely the separation of responsibilities) and apply it to your iOS MVC code. Once you've got some iOS experience under your belt, you can then re-evaluate this pattern question.
If you're starting with iOS developement and is looking for a good architecture, go with VIPER. It's not at all an overkill as long as you:
1 - Automate VIPER files generation
2 - Automate VIPER modules initialization
To accomplish both requirements above, use this Xcode plugin (https://github.com/natangr/ViperTemplate) to generate and intialize VIPER files automatically. It works like charm!
And read this post https://www.ckl.io/blog/best-practices-viper-architecture to get more tips on how to automate things when using VIPER.
I do use it, even for very small projects (160h of development)
I'd like to create components which have a similar structure (all of them having a controller class, a XIB file and language resources). I've figured I could use bundles for this purpose. However, it seems there are some limitations on iOS (for example you cannot use loadable bundles which pretty much correspond to what I'd like to do). For me this would be important for better organisation/packaging of code and better reusability provided by such components.
Does anyone have a good pointer to an arcticle, etc dealing with similar issues?
If you can wait for iOS8 this is made easier as you can now use Frameworks, which are an easier way of bundling code and resources.
These are still tied to the app at build time, so you can't use them for plug-ins at run time, but it should make sharing code and resources between projects simpler.
I read several questions on programmers.stackexchange.com to determine if this question is better asked there. I think this question belongs on SO, but I'm not completely sure.
I have a basic knowledge of Objective-C and UIKit. I have read, a few years ago, most of Cocoa Programming for OS X (2nd Edition) by Aaron Hillegass; this is where the majority of my knowledge stems. The rest is from various sites, SO questions, the SDK documentation, and small personal projects (both OS X and iOS).
I am starting an iOS (specifically, iPhone) project for the university at which I work. This application will have distinct sections with distinct functionality. Some examples of "sections" would be: a virtual student ID card, current semester's class schedule (for the user), a campus map, etcetera. I want to be able to add new sections easily in future versions of the application. Thus, I want to design the project such that a section is an independent, from the main project, group of code (as much as possible).
In addition to being able to add new features by creating new modules, I want the project to be usable by more developers that just myself. That is, I want other developers on my team to be able to develop new modules for the application by following a "best practice."
Currently, I am basing my project around a UITabBarController with my "modules" being new UIViewControllers and NIBs. I get the feeling that this might not be an optimal way of establishing my project. Thus my question:
What are some best practices for such a project as I have described?
I think you're on the right track. UIViewControllers are a good way to break this up.
UITabBarControllers are often awkward when you have a lot of options; a welcome screen with a table view of possibilities might be more expandable. See, for example, Facebook's left panel or the built-in Settings app.
I typically find that the hard part about reuse in iOS projects is not the view controllers (which are typically well isolated) but the underlying infrastructure such as that for network requests, data storage, account management, etc. You might want to start with an existing framework for this (like, say, Parse or RestKit), or at least look at how they break things up.
On the topic of good, reusable iOS design, you might find Matt Gemmell's post about API design helpful, if not exactly what you're looking for.
establish conventions for coding style and resource location.
develop static libraries.
if your static libraries require their own resources (which can be a necessary pain), you may want to use bundles for your resources to minimize resource collisions.
centralize your build configuration files, rather than attempting to define each setting in each project (use xcconfigs).
then you can configure these libraries as dependencies in your app projects, and build, link, and copy appropriately.
one important note: i often err towards C and C++ symbols in these cases (where applicable) because ObjC symbols (and what they reference)cannot be stripped. this can reduce a lot of 'binary fat' where not used. so your 'core' libraries and libraries which deal mostly with symbols declared in other libraries (e.g. system libraries) may be in C and C++, and your higher level abstractions and derived types may be in ObjC.
To make a projet manageable, we break it into sub-projets (class libraries in C#, for instance). Now, in ASP.NET MVC 2, we do have Areas. I'd like to know if Areas have or can serve the same purposes as class libraries? It looks like both are meant to make a project manageable...
Personaly, I'm about to write something bigger. I don't know which way to go: Area vs class library...both?
Thanks for helping
Making a project manageable is really not the point of areas nor class libraries, though they do have that effect when used well.
Generally, the purpose of a class library is more about creating a stand-alone library of code that all serves some inter-related purpose. The point is really that a well used class library represents a collection of code that is maintained, developed, and distributed as a single unit. The big key is the distribution though, since class libraries can be distributed and used in many applications. It is usually a waste of time to split code out into class libraries if those libraries are never distributed, maintained, or developed independently. If they exist just to organize and group code that is otherwise dependent on other code in other libraries then you may be making your code less manageable in the long run; namespaces and folders alone can serve the purpose of keeping code grouped, organized, and manageable.
Areas in MVC are a tad different. Their purpose is to partition large web applications into semi-independent segments that are all hosted in a single project (and thus are part of the same class library; an MVC app is just a fancy kind of class library). So the entire purpose of areas tends to be responsibility. The biggest advantage of areas is that they are useful to split large applications into sections that are maintained and developed by separate teams of developers; or into sections that have widely different infrastructure requirements from other sections of the application.
So in terms of manageability alone, areas are a good idea if your MVC app is large and has distinct functional sections. Class libraries can only be justified if there are other benefits aside from code manageability.
At the most basic level your comparing how C# is compiled into a specific framework feature.
Areas are simply built in routing/finding/searching customizations against so you can separate your app into different folders. You could provide your MVC application with a VirtualPathProvider and use views embedded in class libraries to segment your application but it isn't the standard way of organizing things.
I was reading over Injection by Hand and Ninjection (as well as Why use Ninject ). I encountered two pieces of confusion:
The inject by hand technique I am already familiar with, but I am not familiar with Ninjection, and thus am not sure how the complete program would work. Perhaps it would help to provide a complete program rather than, as is done on that page, showing a program broken up into pieces
I still don't really get how this makes things easier. I think I'm missing something important. I can kind of see how an injection framework would be helpful if you were creating a group of injections and then switching between two large groups all at once (this is useful for mocking, among other things), but I think there is more to it than that. But I'm not sure what. Or maybe I just need more examples of why this is exciting to drive home the point.
When injecting your dependencies without a DI framework you end up with arrow code all over your application telling classes how to build their dependencies.
public Contact()
: this(new DataGateWay())
{
}
But if you use something like Ninject, all the arrow code is in one spot making it easier to change a dependency for all the classes using it.
internal class ProductionModule : StandardModule
{
public override void Load()
{
Bind<IDataGateway>().To<DataGateWay>();
}
}
I still don't really get how this makes things easier. I think I'm missing something important.
Wouldn't it would be great if we only had to develop discrete components where each provided distinct functionality we could easily understand, re-use and maintain. Where we only worked on components.
What prevents us from doing so, is we need some infrastructure that can somehow combine and manage these components into a working application automatically. Infrastructure that does this is available to us - an IOC framework.
So an IOC framework isn't about managing dependencies or testing or configuration. Instead it is about managing complexity, by enabling you to only work and think about components.
It allows you to easily test your code by mocking the interfaces that you need for a particular code block. It also allows you to easily swap functionality without breaking other parts of the code.
It's all about cohesion and coupling.
You probably won't see the benefit on small projects, but once you get past small it becomes really apparent when you have to make changes to the system. It's a breeze when you've used DI.
I really like the autowiring aspect of some frameworks ... when you do not have to care about what your types need to be instantiated.
EDIT:
I read this article by Ayende # Rahien. And I really support his point.
Dependency injection using most frameworks can be configured at runtime, without requiring a recompile.
Dependency injection can get really interesting if you get your code to the point where there are very few dependencies in the code at all. Some dependency injection frameworks will allow you define your dependencies in a configuration file. This can be very useful if you need a really flexible piece of software that needs to be changed without modifying the code. For example, workflow software is a prime candidate for this type of solution.
Dependency Injection is essential for the Component Driven Development. The latter allows to build really complex applications in a much more efficient and reliable manner.
Also, it allows to separate common cross-cutting concerns cleanly from the other code (this results in more reusable and flexible codebase).
Related links:
Inversion of Control and Dependency Injection - Wiki
Component-Driven Development - Wiki
Cross-cutting concerns - Wiki