Design Principles--Hollywood Principles

      "Don't call us, we'll call you" is the famous Hollywood principle. In Hollywood, after submitting your resume to the show company, you have to go home and wait. The entertainment company has complete control over the entire entertainment item, and the actors can only passively accept the company's errands and complete their own performances in the required links.

   Hollywood, a place where many handsome men and beautiful women can't stop. On the road to success, who is unwilling to take a shortcut and become a hit, in which film and television are one of the ways that many people will try. 
     However, Hollywood is not an ordinary place. It is not a place where all cats and dogs can enter. They have no shortage of handsome men and beautiful women. So if you have a handsome face, you don't show it to them, they don't care. You might say, my acting is terrific, oh no, there are people like that all over Hollywood. 
     People who dream of going to Hollywood, don't look for Hollywood. then what should we do? The answer is, let Hollywood come to you! 
This is the very famous Hollywood principle.

 

   The template method pattern fully embodies the "Hollywood" principle. IOC is the abbreviation of Inversion of Control. The principle of IOC is based on the Hollywood principle. All components are passive, and all components are initialized and called by the container.

All frameworks are designed in accordance with Hollywood principles, otherwise it is not called framework. The purpose of framework using IoC:
1. Support for interface-based programming 
2. Reduce dependency on singleton and abstract factory 
3. Reduce the coupling between business and framework 
4. Business components are reusable and pluggable

 

 

Dependency Injection (IoC)

     

      如果对象A包含对象B的话,就需要在A里new一个B 
      依赖注入从具体类B里抽象出接口IB——IB的具体实现可能有很多B,B1,B2…很多种——这样A可以不用再new具体的B了,而是跟IoC容器说:我要一个IB(getBean("IB"))。然后,由容器根据配置文件来做具体的new的工作。具体new的是哪个,由配置文件从代码外部决定,要更换成B,B1,或是B2…修改配置文件就能做到,不用再改代码了

 

例:
假设你编写了两个类,一个是人(Person),一个是手机(Mobile)。 
人有时候需要用手机打电话,需要用到手机的dialUp方法。 
传统的写法是这样:

//code
public class Person{ 
    public boolean makeCall(long number){ 
        Mobile mobile=new Mobile(); 
        return mobile.dialUp(number); 
    } 
}

 

     也就是说,类Person的makeCall方法对Mobile类具有依赖,必须手动生成一个新的实例new Mobile()才可以进行之后的工作。 
依赖注入的思想是这样,当一个类(Person)对另一个类(Mobile)有依赖时,不再该类(Person)内部对依赖的类(Moblile)进行实例化,而是之前配置一个beans.xml,告诉容器所依赖的类(Mobile),在实例化该类(Person)时,容器自动注入一个所依赖的类(Mobile)的实例。

 
接口:

//code
public Interface MobileInterface{ 
    public boolean dialUp(long number);
}

 

 Person类:

//code
public class Person{
    private MobileInterface mobileInterface;
    public boolean makeCall(long number){
        return this.mobileInterface.dialUp(number);
    }
    public void setMobileInterface(MobileInterface mobileInterface){
        this.mobileInterface=mobileInterface;
    }
}

 

在xml文件中配置依赖关系

//code
<bean class="Person" id="person">
     <property name="mobileInterface">
         <ref local="mobileInterface"></ref>
     </property>
</bean> 
<bean class="Mobile" id="mobileInterface"></bean>

  

    这样,Person类在实现拨打电话的时候,并不知道Mobile类的存在,它只知道调用一个接口MobileInterface,而MobileInterface的具体实现是通过Mobile类完成,并在使用时由容器自动注入,这样大大降低了不同类间相互依赖的关系。

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326300520&siteId=291194637