From an architecture perspective, a lot of the ideas are the same, and it doesn’t matter what language you’re using, what platform you’re on, whether you’re front-end or back-end.
I first encountered MVVM in an RN project, followed by small applications that were also MVVM, followed by Flutter, as well as Android native LiveData.
After the advent of responsive programming, various language versions of Rx came out, Rxjava, Rxjs, Rxdart, Rxswift, Rxpy, Rxgo, and so on.
Looking at the concept of distribution, how similar is the componentization of plug-ins on the Android side, allowing each business to run debugging separately. Microservices on the server side, each service is isolated from each other and does not affect independent development. Huawei’s Hongmeng systems are all distributed systems.
I have studied Java server recently, and many of the technical ideas are the same.
The Spring framework
Spring is an MVC mode development, mainly divided into three layers, controller+ Service + DAO layer.
@Controller @Api(tags = "UmsMemberController", @requestMapping ("/ SSO ") Public class UmsMemberController {@value ("${jwt.tokenHeader}") private String tokenHeader; @Value("${jwt.tokenHead}") private String tokenHead; @Autowired private UmsMemberService memberService; @apiOperation (" login") @requestMapping (value = "/login", method = RequestMethod.POST) @ResponseBody public CommonResult login(@RequestParam String username, @RequestParam String password) { String token = memberService.login(username, password); If (token = = null) {return CommonResult. ValidateFailed (" user name or password error "); } Map<String, String> tokenMap = new HashMap<>(); tokenMap.put("token", token); tokenMap.put("tokenHead", tokenHead); return CommonResult.success(tokenMap); }}Copy the code
@Service public class UmsMemberServiceImpl implements UmsMemberService { @Override public String login(String username, String password) { String token = null; try { UserDetails userDetails = loadUserByUsername(username); if(! PasswordEncoder. Matches (password, populated userDetails. GetPassword ())) {throw new BadCredentialsException (" password incorrect "); } UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()); SecurityContextHolder.getContext().setAuthentication(authentication); token = jwtTokenUtil.generateToken(userDetails); } catch (AuthenticationException e) {logger.warn (" Login exception :{}", LLDB message ()); } return token; }}Copy the code
As you can see from the code above, in controller, we do not create a new service, but we can use it directly, which must be related to the autowired annotation above. The idea of IOC, DI and AOP is mainly used in Spring. IOC and DI actually mean the same thing on different levels.
Control Inversion means that the container controls the relationship between programs, giving Control to the external container, from the program code direct Control, to the Control of the external container management, the transfer of Control is called Inversion of IOC three injection methods: Interface injection: If an interface is used to inject a Bean, then the injected Bean must implement the interface… Set injection: If you use a set to inject a Bean, then you only need to provide a set method for the required components of the Bean. Constructor injection: With constructor injection, the Bean is first provided with a custom constructor whose parameters are component instances in the class.
DI (Dependency Injection) is a process-focused method of injecting an object into another object as a member variable of that object via setter, contruct, ARGS, etc
Aop (Aspect Oriented Programming) : Aop can isolate each part of the business logic, so that the degree of coupling between the parts of the business logic is reduced, improve the reusability of the program, and improve the efficiency of development. AOP Aspect: Usually a class in which pointcuts and Advice can be defined JointPoint: an explicit point during the execution of a program, typically a call to a method. Advice: An enhanced processing that AOP performs on a particular pointcut, Before, after, afterReturning afterThrowing, around Pointcut (point) : is a notice of join points, mainly reflected in the program for writing Angle expression of AOP agent: The proxy is an enhancement of the object created by the AOP framework. AOP proxies in Spring can be either JDK dynamic proxies, based on interfaces, or CGLIB proxies, based on subclasses.
As an Android developer, you’ll be able to pick up some familiar keywords. You’ll have seen the words Inversion of control and dependency injection when you’ve used Dagger2 or ButterKnife. Anyone who has used Retrofit knows about dynamic proxies. The AOP aspect in android mainly uses AspectJ, which makes non-invasive changes to the code.
You can easily see this spring-like code style in Dagger2 / ButterKnife
public class MainActivity extends AppCompatActivity { @Inject ApiServer mApiServer; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mApiServer.register(); } } public class ApiServer { @Inject public ApiServer(){ Log.e("Howard","ApiServer--->start"); } public void register() { Log.e("Howard","ApiServer--->register"); } } class ExampleActivity extends Activity { @BindView(R2.id.user) EditText username; @BindView(R2.id.pass) EditText password; . }Copy the code
The principles are pretty much the same, and now the framework is decoupled, basically annotations, reflection, dynamic proxy, Javapoet. We can see from the framework of most of them, except butterknife dagger2 these two typical, and such as retrofit, eventbus, arouter, wmrouter etc., etc.