Design Principles
Dependency Injection (DI)
With dependency injection, objects accept dependencies in their constructors. The core principle is to separate behaviour from dependency resolution.
Compared with Direct constructor calls:
| direct constructor calls | Dependency Injection |
|---|---|
| direct, compile-time dependency | dependencies injection |
| problems for modularity and testability | dependencies are not hidden in codes |
See Motivation of guice.
Sample: https://github.com/google/guice/wiki/GettingStarted
(Inversion of Control) IoC
参考维基百科 控制反转:
早在2004年,Martin Fowler就提出了“哪些方面的控制被反转了?”这个问题。他总结出是依赖对象的获得被反转了,因为大多数应用程序都是由两个或是更多的类通过彼此的合作来实现企业逻辑,这使得每个对象都需要获取与其合作的对象(也就是它所依赖的对象)的引用。如果这个获取过程要靠自身实现,那么这将导致代码高度耦合并且难以维护和调试。
- Common implementation methods
- Dependency Injection
- Dependency Lookup (ServiceReistry)
AOP
In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code (an advice) without modifying the code itself, instead separately specifying which code is modified via a "pointcut" specification, such as "log all function calls when the function's name begins with 'set'". This allows behaviors that are not central to the business logic (such as logging) to be added to a program without cluttering the code, core to the functionality. AOP forms a basis for aspect-oriented software development.
cross-cutting concerns are aspects of a program that affect other concerns. These concerns often cannot be cleanly decomposed from the rest of the system in both the design and implementation, and can result in either scattering (code duplication), tangling (significant dependencies between systems), or both.
aspectj
aspectj enables clean modularization of crosscutting concerns, such as error checking and handling, synchronization, context-sensitive behavior, performance optimizations, monitoring and logging, debugging support, and multi-object protocols