Learning the modular development of company projects

Refer to the company’s modular development

Previously in a small company, how did not contact the so-called tall on the modular development, such as drops taxi announced modularity, Ali announced modular.

Came to the company, found the application, a modular Anxi…. in his spare time, and gradually find out this is a system of routine, then give some do not know friends to share.

Modular one, for each module can be used as an abstraction

@protocol XxxModuleManager < NSObject> / / / this method is implemented, the current registration module to module manager (void) - registerServices; @optional

Modular two, the use of objc_getClassList to read all the current engineering objc class, to achieve the realization of the agreement to become a separate objc class Module

Int numClasses; Class *classes = NULL; classes = NULL; numClasses = objc_getClassList (NULL, 0); NSLog ("Number of classes: @%d, numClasses); if (numClasses > 0) {/ / memory (classes = __unsafe_unretained * malloc (Class) sizeof (Class) * numClasses); / / read all the data into the Class memory objc_getClassList (classes, numClasses); / / get the traversal of each Class, for (int i = 0; I < numClasses; i++) {Class CLS = classes[i]; / / to see if the Module module protocol, if (class_conformsToProtocol (CLS, @protocol (XxxModuleManager))) {/ / if the implementation of the agreement, that is a Module, id< XxxModuleManager> module; [cls = alloc]; / / and The callback method registered Module the [module registerServices]; / / will complete the registration Module saved [modules addObject:module];}} / / release system memory free (classes);}

Modular three, add a specific module

First of all, give this module a separate Manager entry class, and the realization of the module protocol XxxModuleManager, with the ability to become a Module

The above protocol module / @interface module to complete the registration of XxxBreakfastModuleManager: NSObject < XxxModuleManager> @end; @implementation XxxBreakfastModuleManager (void registerServices) {/ / regist call module manager Router object to complete the registration of //1. sub modules each module has a unique URL //2. set the current module is main module or other main App calls block when [XxxExternalRouter (registerRoute:@) eleme://breakfast forBlock:^ (XxxRouteURL * _Nonnull routeURL) {/ / ViewController XxxRetailHomeViewController *vc created the current module [[XxxRetailHomeViewController alloc] = init]; / / call a good package, access to topViewController and complete pushVC code to complete a VC [XxxUIKit showViewController:vc animationType:XxxAnimationTypePush];}];}

Structure diagram for a specific module module:

Learning the modular development of company projects

Note that each module project is a library library that can be created using the pod lib create.

The most important part of the two, one is the module of the entry class ModuleManager, and the other is the module of all sub function options VC.

Each module is set in a demo app project, the function development.

Learning the modular development of company projects

Modular four, each module must be assigned a unique scheme, as well as the packaging of a single Router object management module

Each ModuleManager implementation class object must have a unique URL

Router has done all these things

Between different modules, you can also pass the corresponding URL to complete the Router call each other
NSURL *url = [NSURL "eleme://home"]; [XxxRouter handleOpenURL:url error:NULL]; URLWithString:@;
The parameters passed in the back of URL
NSURL *url = [NSURL URLWithString:@ "id=111"]; [XxxRouter handleOpenURL:url error:NULL]; eleme://breakfast/share;
As above by Router jump, will be divided into two cases:
1 if the main App project, pods has all the code module, the protocol method directly callback ModuleManager to achieve 2 App if the main project, there is no pods module code, this time through the URL scheme to open the module where the demo app

In the first case, the direct callback of the ModuleManager implementation of the protocol method is invoked through the code level between the modules

@implementation + XxxRetailModuleManager (void load) {RegisterModule (self);} - {[Router (void) registerServices (registerRoute:@) eleme://xxxx.xxxx forBlock:^ (XxxRouteURL * _Nonnull routeURL) {NSInteger shopId = "routeURL.params objectForKey:@" parameter 1] integerValue] NSInteger; groupId = -1; NSString = *groupIdNum [routeURL.params objectForKey:@ "parameter 2]; if (groupIdNum) {groupId} = [groupIdNum integerValue]; NSInteger goodsId = -1; NSString = *goodsIdNum [routeURL.params objectForKey:@" parameter 3]; if (goodsIdNum) {goodsId = [goodsIdNum integerValue]; XxxViewController *shopVC = [[XxxViewController alloc] initWithSho} PId:shopId groupId:UIImageOrientationUp goodsId:goodsId]; [Tools showViewController:shopVC animationType:XxxAnimationTypePush];}]; @end}

In the second case, by setting the demo app project for each individual module, the corresponding scheme. The scheme should be consistent with the previous implementation of the ModuleManager protocol when specifying the scheme. In this case, you can configure the URL scheme to open the module demo app program to test.

For example, the following is the structure of each module of the demo project:

Learning the modular development of company projects

To configure the demo project URL scheme, with the sub module registered scheme URL is best consistent:

1 modules of the scheme URL > > > eleme://xxxxx 2 so demo engineering URL schem > > > > xxxxx

This configuration, when the following code

NSURL *url = [NSURL "eleme://home"]; [XxxRouter handleOpenURL:url error:NULL]; URLWithString:@;

The main App project, there is no pods module to achieve the code, you can open the demo app program in the form of URL scheme

Learning the modular development of company projects

I found that each module of eleme was written to a specific scheme, so that it could not open multiple modules at the same time. I don’t know why I wrote it.

The general idea is to achieve this, there are some specific details. For example, the realization of Router, as well as the logic of the VC jump between each module.

If you want to own App project modular development, you can refer to these two things:

  • (1) MJRouter
  • (2) BeeHive

Finally, the modular development seems is a drag, but UI can not reuse, business logic can not be universal, not universal, network layer and so on are not high degree of general situation, modular development becomes the case:

  • (1) a master App into a very high similarity in the UI code, copy come over, change the name, change the UI style, frame layout, loading data…
  • (2) each module, their own package of a more frustrating network library, JSON parsing, cache…
  • (3) in the main App project, actually a lot of high similarity code, had to take over there

So, the modular development, as well as the necessity of his existence?