Service设计模式

\*\*`面向用户操作`\*\*的功能代码封装,Service业务层封装了用户的操作功能,一个用户操作,对应Service的一个方法。

### (一)**场景**

![](image/image_5_2cr5ldrq.png)

### (二)Service编程

**Service编码规范**

-   ① 一个业务模块(相关的功能属于一个模块,比如管理相关功能、账户相关操作等)的方法放在一个Service类中,命名:**`XxxxService`**,保持望文生义。
-   ② 用户的一个操作功能对应service一个方法,例如:`开卡`、`编辑卡号`、`注销卡号`、`转账`,对应方法 `openCard`、`editCard`、`removeCard`、`transfer`。
-   ③ Service的java类,通常放在`service`包下或者biz包下。

**示例代码:**

- 转账功能:

  - 实体类:

    ~~~java
 

  package com.xx.entity;
    
    import java.io.Serializable;
    //映射t_account表
    public class Account implements Serializable {
        private Integer accountId;
        private String username;
        private String password;
        private Double balance;
        //省略getter、setter、构造
    }
    ~~~

  - DAO:

   

~~~java
    package com.xx.dao;
    
    import com.xx.entity.Account;
    
    public interface AccountDao {
        /**
         * 根据账户名查询指定信息
         * @param username 被查询的账户名
         * @return 对应的对象信息
         */
        Account selectAccountByUsername(String username);
    
        /**
         * 根据账户名修改余额
         * @param username 被修改的账户名
         * @param newBalance 修改后的余额
         * @return 受影响的行数
         */
        int updateAccountByUsername(String username, double newBalance);
    }
    
    ~~~

    ~~~java
   

 package com.xx.dao.impl;
    
    import com.xx.dao.AccountDao;
    import com.xx.entity.Account;
    import com.xx.util.JDBCUtils;
    import org.springframework.jdbc.core.BeanPropertyRowMapper;
    import org.springframework.jdbc.core.JdbcTemplate;
    
    import java.util.List;
    
    public class AccountDaoImpl implements AccountDao {
        private JdbcTemplate jdbcTemplate = JDBCUtils.getJDBCTemplate();
    
        @Override
        public Account selectAccountByUsername(String username) {
            String sql = "select * from t_account where username=?";
            List<Account> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Account.class), username);
            return list.isEmpty() ? null : list.get(0);
        }
    
        @Override
        public int updateAccountByUsername(String username, double newBalance) {
            String sql = "update t_account set balance=? where username=?";
            return jdbcTemplate.update(sql, newBalance, username);
        }
    }
    
    ~~~

  - Service:

 

  ~~~java
    package com.xx.service;
    
    public interface AccountService {
        /**
         * 转账功能
         * @param fromName 用户输入的转出人姓名
         * @param toName 用户输入的转入人姓名
         * @param money 转账金额
         * @return转账是否成功的结果
         */
        boolean transfer(String fromName,String toName,double money);
    }
    ~~~

    ~~~java
 

  package com.xx.service.impl;
    
    import com.xx.dao.AccountDao;
    import com.xx.dao.impl.AccountDaoImpl;
    import com.xx.entity.Account;
    import com.xx.service.AccountService;
    
    public class AccountServiceImpl implements AccountService {
        private AccountDao ad = new AccountDaoImpl();
        @Override
        public boolean transfer(String fromName, String toName, double money) {
            //判断转出人是否存在
            Account from = ad.selectAccountByUsername(fromName);
            if (from == null) {
                throw new RuntimeException("转出人不存在!");
            }
            //判断余额是否充足
            if (from.getBalance() < money) {
                throw new RuntimeException("余额不足!");
            }
            //判断转入人是否存在
            Account to = ad.selectAccountByUsername(toName);
            if (to == null) {
                throw new RuntimeException("转入人不存在!");
            }
            //执行转账
            int n = ad.updateAccountByUsername(fromName, from.getBalance() - money);
            n += ad.updateAccountByUsername(toName, to.getBalance() + money);
            if (n == 2) {
                return true;
            }
    
            return false;
    
    
    
        }
    }
    ~~~

  - 视图层:

 

  ~~~java
    package com.xx.view;
    
    import com.xx.service.AccountService;
    import com.xx.service.impl.AccountServiceImpl;
    
    import java.util.Scanner;
    
    public class TransferView {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入转出人的账户名:");
            String fromName = sc.next();
            System.out.println("请输入转账金额:");
            double money = sc.nextDouble();
            System.out.println("请输入转入人的账户名:");
            String toName = sc.next();
    
            //调用转账功能
            AccountService as = new AccountServiceImpl();
    
            try {
                if (as.transfer(fromName, toName, money)) {
                    System.out.println("转账成功!");
                } else {
                    System.out.println("转账失败");
                }
            } catch (Exception e) {
                System.out.println("转账失败!详细信息为:"+e.getMessage());
            }
        }
    }
    
    ~~~
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这孩子叫逆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值