Control inversion (IoC)? Factory mode?

Source: Internet
Author: User

do not know people still remember that year geoscience "big talk design mode", has been trying to understand the control reversal in the end is what is going on, just feel tall on a little, and then further to learn to compare the time only to find that before, have been exposed to this kind of thought, The design principle of the dependency reversal and design patterns of factory methods are very good embodiment of this idea, eager to share with you!

a, dependence reversal principle


A. high-level modules should not rely on low-layer modules. All two should be dependent on abstraction.

B. abstractions should not be dependent on details, and details should be dependent on abstraction. To be specific , do not program to implement the interface.

in the principle of inversion of control, we learned that we put an instantiation of an object into a container, and when the product is implemented, we call the interface directly, that is, the container passes the reference of the object it depends on to the product code. IoC manages dependencies between objects, and product code only needs to be programmed on the interface, rather than relying on a specific implementation. Is the same as the dependency reversal principle.


two, factory method mode


defines an interface for creating objects, letting subclasses decide which class to instantiate. The factory method defers the instantiation of a class to its subclasses.

Factory methods Follow open - closure principle, and preserves the Encapsulation object creation process the advantages.

We can think of the IOC model as a distillation of the factory model, seeing the IOC as a big factory, except that the objects in this big factory are XML file, and then uses Java's "reflection" programming to generate the corresponding object based on the class name given in the XML. In terms of implementation, the IOC generates code for objects previously written in the factory method, which is changed to be defined by an XML file, that is, separating the factory from the object generation, with the aim of increasing flexibility and maintainability.

three, Reflection


Factory methods also have shortcomings, that is, every addition of a product, you need to add a product factory class, add additional development volume. I don't know if you remember in the book, the chapter that wrote the Factory method, in the last paragraph, left a foreshadowing-reflection.

          When it comes to the abstract factory, it mentions the dependency injection noun. Take the abstract factory, if we need a new database type, we need to add a branch condition in the code, break the open and close principle, this time the dependency injection itself is not able to solve the problem, but if we use the language support reflection mechanism, using reflection to configure the data source, You can avoid the problem of branch judgment.

Let's think about what the reflection did, the first difficulty of our project was that at the end of the object we need newand instantiated, and we can only instantiate the existing classes, and we cannot deal with new classes that will be added in the future. So once a new class is added, we have to modify the code. However, if we have a way to Instantiate an object through the name of the class, not through new, then I just need to use the name of the class as a configuration item, and I can implement the class that will appear in the future without modifying the code. So, it is no exaggeration to say that reflection gives the ability of the language to "foresee the future", which increases the power of polymorphism and dependency injection .

Iv. concluding remarks

          looked at a lot of information, the previous design pattern also turned out to study, painting, drawing, thinking more clear. IoCIt feels like "abstract factory+Reflection+configuration File ", and we can putabstractfactoryas aIoCcontainer, and incorporate reflection mechanisms and configuration files to enable flexible configuration of class names. (Explanation, the configuration file supersedesConcreFactory1and theConcreFactory2, which can be used flexibly to configure the class name;abstractfactory, instead of branching judgment, avoiding the drawbacks of later extensions to modify code)

OK , object-oriented no matter what the principles, the road to Jane, still very long, Java study, just started, come on!


Control inversion (IoC)? Factory mode?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.