1. 引子

本文通过模拟人使用手机发短信的场景,展示了面向对象编程如何简化复杂性,并引入Spring框架解决对象间的耦合问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

尽管现在看起来用面向对象的方法给世界建模是理所当然的,但是从面向过程到面向对象的转变依然是很突破性思维转变,它使得给事物建模的复杂性降低了很多,你无法想象只使用面向过程的方式去给事物建模有多复杂。但是面向对象并不是没有缺点。

例子:假设模拟人使用手机发短信这个场景,一开始,我们可能会这么写:

手机的接口:

public interface Phone {
     void sendMessage(String txt);
}

手机的建模:

public class IPhone implements Phone
{
    private String name;

    public IPhone(String name)
    {
        this.name = name;
    }

    public void sendMessage(String txt)
    {
        System.out.println(txt + " ---from " + this.name);
    }
}

人的建模:

public class Person {
    private String name;
    private int age;
    private Phone phone;

    public Person(String name, int age,Phone phone)
    {
        this.name = name;
        this.age = age;
        this.phone = phone;
    }

    public void  sendMessage()
    {
        this.phone.sendMessage("hello,this is " + this.name);
    }
}

调用代码:

public class Main {

    public static void main(String[] args) {
   Person xiaoming = new Person("xiaoming",21,new IPhone("iPhone"));
   xiaoming.sendMessage();
    }
}
结果:

hello,this is xiaoming ---from iPhone


看起来一切都很好,成功发送了短信,并且还有手机的签名信息。尽管人与手机只是接口依赖,但是客户调用代码与人及手机耦合在了一起。当代码的规模很大的时候,这种依赖维护起来会非常棘手,常常会发生牵一发而动全身的情况。

怎么办呢?Spring来了。

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="xiaoming" class="com.learnspring.Person">
        <constructor-arg value="xiaoming"/>
        <constructor-arg value="21"/>
        <constructor-arg ref="iPhone"/>
    </bean>
    <bean id="iPhone" class="com.learnspring.IPhone">
        <constructor-arg value="iPhone"/>
    </bean>

</beans>

客户调用:

public class Main {

    public static void main(String[] args) {
// Person xiaoming = new Person("xiaoming",21,new IPhone("iPhone"));
// xiaoming.sendMessage();
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");
        Person p = (Person)context.getBean("xiaoming");
        p.sendMessage();

    }
}
这时候,如果你想修改手机或者人属性就可以在xml中进行了。

那么这一切都是怎么回事呢?虽然本人已经使用了一段时间的spring了,但是对于其深入的原理依然没时间细究,那么希望在以后的时间,能把这个怎么回事搞清楚。



附:代码结构:



POM

<properties>
    <spring.version>4.1.1.RELEASE</spring.version>
    <jackson.version>2.1.4</jackson.version>
</properties>

<dependencies>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
</dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值