Spring Study Guide - Chapter 2 -Spring frame basis

Chapter Spring Framework foundation

Oriented Programming Interface Design Method

In the last chapter, we saw a dependency contains a reference to a specific class of its dependencies on other classes POJO class. For example, FixedDepositController FixedDepositService class contains a reference to class, FixedDepositService FixedDepositDao class contains a reference to the class. If this is dependent on other classes of classes refer directly to its dependency classes will result in tight coupling between classes. This means that if you want to replace other to achieve its dependencies, you need to change this depends on other classes of the class itself.

We know that Java interface defines the contract to be followed by its implementation class. Thus, if a class item depending on its implementation dependent interfaces, then when replacing achieve different dependencies, class does not need to change. A class depends on its dependency entry interfaces implemented application programming method is called "programming to an interface." This design approach that relies loose coupling between items and class dependent. Implemented by interfaces called dependency dependency class interface.

And "oriented programming class" than "programming to an interface" is a more favorable design practice, the figure shows ABean class depends on BBean BBeanImpl interface rather than class (interface implemented BBean).

image

The figure below, FixedDepositJdbcDao simple to use JDBC, and FixedDepositHibernateDao use Hibernate ORM database interaction. If FixedDepositService directly dependent on FixedDepositJdbcDao or FixedDepositHibernateDao, when the need to switch tactics and interact with the database, you need to make the necessary changes in FixedDepositService class. FixedDepositService depends on FixedDepositJdbcDao and FixedDepositHibernateDao class implements interfaces FixedDepositDao (dependent interface). Now, with a simple JDBC or Hibernate ORM framework, you can provide FixedDepositJdbcDao or FixedDepositHibernateDao instance to FixedDepositService instance.

image

Since FixedDepositService dependent on FixedDepositDao interface, so the future may support other database interaction strategies. If you decide to use iBATIS (mybaits) persistence framework for database interaction, you can use IBATIS, without the need for any changes to the FixedDepositService class, just create a class FixedDepositIbatisDao FixedDepositDao interface, and provide examples to FixedDepositService FixedDepositIbatisDao instance.

Now let's look at the "interface-oriented programming" is how to improve the testability of the dependent classes.

Improving testability dependent class

In the figure above, FixedDepositSerivce class retains a reference to the FixedDepositDao interface. FixedDepositJdbcDao and FixedDepositHibernateDao are FixedDepositDao interface implementation class. Now, in order to simplify FixedDepositService class unit test, we can achieve the original database operations to remove concrete, with a FixedDepositDao implements the interface, but does not require the code to replace the database.

If FixedDepositService class direct reference FixedDepositJdbcDao or FixeDepositHibernateDao class, then you need to set the test FixedDepositService class database for testing. This indicates that by reliance on simulation dependent class that implements the interface, you can reduce the workload for infrastructure unit test setup.

Now let's look at how to use the design method Spring "programming to an interface" in your application, you need to do the following:

1. Create a reference dependent interfaces, rather than the bean class dependencies particular implementation;

2. Definitions Element, to be injected and specifies the implementation class dependent bean dependencies in the element.

Use "Oriented Programming Interface" method of application design the MyBank

Unfinished, continued ......

Guess you like

Origin www.cnblogs.com/train99999/p/11915113.html