Java---设计模式app小软件汇总应用

写了一个app小软件,重点不在于软件,软件bug挺多,也没去修改。
这个小软件只是为了更好的说明和了解设计模块而做的。
Java 程序设计–包结构
Java程序设计的系统体系结构很大一部分都体现在包结构上
大家看看我的这个小软件的分层:

结构还是挺清楚的。
一种典型的Java应用程序的包结构:
前缀.应用或项目的名称.模块组合.模块内部的技术实现
说明:
1、前缀:是网站域名的倒写,去掉www(如,Sun公司(非JDK级别)的东西:com.sun.* )。
2、其中模块组合又由系统、子系统、模块、组件等构成(具体情况根据项目的大小而定,如果项目很大,那么就多分几层。
3、模块内部的技术实现一般由:表现层、逻辑层、数据层等构成。
对于许多类都要使用的公共模块或公共类,可以再独立建立一个包,取名common或base,把这些公共类都放在其中。
对于功能上的公用模块或公共类可建立util或tool包,放入其中。
如本例的util包。

设计与实现的常用方式、DAO的基本功能
设计的时候:从大到小
先把一个大问题分解成一系列的小问题。或者说是把一个大系统分解成多个小系统,小系统再继续进行往下分解,直到分解到自己能够掌控时,再进行动手实现。

实现的时候:从小到大
先实现组件,进行测试通过了,再把几个组件实现合成模块,进行测试通过,然后继续往上扩大。

最典型的DAO接口通常具有的功能
新增功能、修改功能、删除功能、按照主要的键值进行查询、获取所有值的功能、按照条件进行查询的功能。

一个通用DAO接口模板

UserVO 和 UserQueryVO的区别
UserVO封装数据记录,而UserQueryVO用于封装查询条件。

下面的为那个小软件实现这些设计模式的简单汇总:
(含分层思想,值对象,工厂方法,Dao组件,面向接口编程)

main方法类:
UserClient :

package cn.hncu.app;

import cn.hncu.app.ui.UserAddPanel;

public class UserClient extends javax.swing.JFrame {

    public UserClient(){
        super("chx");
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setBounds(100, 100, 800, 600);
        this.setContentPane(new UserAddPanel(this));

        this.setVisible(true);

    }

    public static void main(String[] args) {
        new UserClient();
    }

}

公用模块类 utils类:
FileIO :

package cn.hncu.app.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class FileIO {
    public static Object[] read(String fileName){
        List<Object> list = new ArrayList<Object>();
        ObjectInputStream objIn=null;
        try {
            objIn = new ObjectInputStream(new FileInputStream(fileName));
            Object obj;
            //※※对象流的读不能用available()来判断,而应该用异常来确定是否读到结束
            while(true){
                obj=objIn.readObject();
                list.add(obj);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            //读到文件末尾,就是出异常,通过这来判断是否读到结束。
            //因此,本程序中,这里是正常的文件读取结束,不是我们之前认为的出异常--所以不输出异常信息
        } catch (ClassNotFoundException e) {
            //读到文件末尾,就是出异常,通过这来判断是否读到结束。
            //因此,本程序中,这里是正常的文件读取结束,不是我们之前认为的出异常--所以不输出异常信息
        }finally{
            if(objIn!=null){
                try {
                    objIn.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

        Object[] objs = list.toArray();
        if(objs==null){
            objs=new Object[0];
        }
        return objs;
    }

    public static boolean write(String fileName,Object obj){
        ObjectOutputStream objOut =null;
        try {
            objOut=new ObjectOutputStream(new FileOutputStream(fileName,true));
            //new FileOutputStream(fileName,true),有true的存在代表是添加而不是覆盖
            objOut.writeObject(obj);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }finally{
            if(objOut!=null){
                try {
                    objOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return true;
    }

}

值对象:
封装数据记录:
User:

package cn.hncu.app.vo;

import java.io.Serializable;

public class User implements Serializable{//一定要实现这个接口啊!!!
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public User() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (age != other.age)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return  name + "," + age;
    }
}

封装查询条件:

UserQueryVO :

package cn.hncu.app.vo;

public class UserQueryVO extends User{
    private String age2;//年龄段查找

    public String getAge2() {
        return age2;
    }

    public void setAge2(String age2) {
        this.age2 = age2;
    }

}

Dao类(数据层):

接口UserDAO :

package cn.hncu.app.dao.dao;

import java.util.Collection;

import cn.hncu.app.vo.User;
import cn.hncu.app.vo.UserQueryVO;

public interface UserDAO {
    public Object[] getAllUsers();

    //增删改
    public boolean addUser(User user);

    public boolean delete(User user);//有时参数也可以用:id
    public boolean update(User user);

    //3个查询
    public User getByUserId(String uuid);//查单个
    public Collection<User> getAll();//查全
    public Collection<User> geByCondition(UserQueryVO qvo);
    //只写了接口,并没有具体去应用。。。。这里主要学方法
}

工厂方法UserDaoFactory :

package cn.hncu.app.dao.factory;

import cn.hncu.app.dao.dao.UserDAO;
import cn.hncu.app.dao.impl.UserDaoFileIO;

public class UserDaoFactory {
    public static UserDAO getUserDAO(){
        return new UserDaoFileIO();
    }

}

实现类 UserDaoFileIO :

package cn.hncu.app.dao.impl;

import java.util.Collection;

import cn.hncu.app.dao.dao.UserDAO;
import cn.hncu.app.utils.FileIO;
import cn.hncu.app.vo.User;
import cn.hncu.app.vo.UserQueryVO;

public class UserDaoFileIO implements UserDAO{
    private static final String FILE_NAME = "user.txt";
    @Override
    public Object[] getAllUsers() {
        return FileIO.read(FILE_NAME);
    }

    @Override
    public boolean addUser(User user) {
        return FileIO.write(FILE_NAME, user);
    }

    @Override
    public boolean delete(User user) {
        return false;
    }

    @Override
    public boolean update(User user) {
        return false;
    }

    @Override
    public User getByUserId(String uuid) {
        return null;
    }

    @Override
    public Collection<User> getAll() {
        return null;
    }

    @Override
    public Collection<User> geByCondition(UserQueryVO qvo) {
        return null;
    }

}

逻辑层:
接口UserEbi :

package cn.hncu.app.business.ebi;

import cn.hncu.app.vo.User;

public interface UserEbi {
    public boolean addUser(User user);
    public Object[] getAllUser();
}

工厂类UserEbiFactory :

package cn.hncu.app.business.factory;

import cn.hncu.app.business.ebi.UserEbi;
import cn.hncu.app.business.impl.UserEbo;
public class UserEbiFactory {
    public static UserEbi getUserEbi(){
        return new UserEbo();
    }
}

实现类UserEbo :

package cn.hncu.app.business.impl;

import cn.hncu.app.business.ebi.UserEbi;
import cn.hncu.app.dao.dao.UserDAO;
import cn.hncu.app.dao.factory.UserDaoFactory;
import cn.hncu.app.vo.User;

public class UserEbo implements UserEbi{

    @Override
    public boolean addUser(User user) {
        UserDAO userDao = UserDaoFactory.getUserDAO();
        return userDao.addUser(user);
    }

    @Override
    public Object[] getAllUser() {
        UserDAO userDao = UserDaoFactory.getUserDAO();
        return userDao.getAllUsers();
    }

}

表现层:

UserAddPanel 类:

/*
 * UserAddPanel.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.hncu.app.ui;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import cn.hncu.app.business.ebi.UserEbi;
import cn.hncu.app.business.factory.UserEbiFactory;
import cn.hncu.app.vo.User;

/**
 *
 * @author  __USER__
 */
public class UserAddPanel extends javax.swing.JPanel {
    private JFrame mainFrame = null;

    /** Creates new form UserAddPanel */
    public UserAddPanel(JFrame mainFrame) {
        this.mainFrame = mainFrame;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //GEN-BEGIN:initComponents
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        tfdAge = new javax.swing.JTextField();
        tfdName = new javax.swing.JTextField();

        setMinimumSize(new java.awt.Dimension(800, 600));
        setLayout(null);

        jLabel1.setText("\u5e74\u9f84\uff1a");
        add(jLabel1);
        jLabel1.setBounds(170, 200, 50, 40);

        jLabel2.setText("\u59d3\u540d\uff1a");
        add(jLabel2);
        jLabel2.setBounds(170, 120, 50, 40);

        jButton1.setText("\u6dfb\u52a0");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        add(jButton1);
        jButton1.setBounds(240, 330, 170, 60);
        add(tfdAge);
        tfdAge.setBounds(210, 210, 170, 30);
        add(tfdName);
        tfdName.setBounds(210, 130, 170, 30);
    }// </editor-fold>
    //GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        //1收集参数
        String name = tfdName.getText();
        String strAge = tfdAge.getText();
        int age = Integer.parseInt(strAge);//简单的转换成一个整型数了。

        //2组织参数
        //User user = new User(name, age);
        User user = new User();
        user.setAge(age);
        user.setName(name);

        //3调用逻辑层---通过接口
        UserEbi userEbi = UserEbiFactory.getUserEbi();
        boolean isSuccess=userEbi.addUser(user);

        //4导向不同页面
        if(isSuccess){
            JOptionPane.showMessageDialog(this, "恭喜,添加成功!");
            this.mainFrame.setContentPane(new UserListPanel(mainFrame));
            this.mainFrame.validate();
        }else{
            JOptionPane.showMessageDialog(this, "祝贺,添加失败!");
        }

    }

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField tfdAge;
    private javax.swing.JTextField tfdName;
    // End of variables declaration//GEN-END:variables

}

UserListPanel 类:

/*
 * UserListPanel.java
 *
 * Created on __DATE__, __TIME__
 */

package cn.hncu.app.ui;

import javax.swing.JFrame;

import cn.hncu.app.business.ebi.UserEbi;
import cn.hncu.app.business.factory.UserEbiFactory;

/**
 *
 * @author  __USER__
 */
public class UserListPanel extends javax.swing.JPanel {
    private JFrame mainFrame = null;

    /** Creates new form UserListPanel */
    public UserListPanel(JFrame mainFrame) {
        this.mainFrame = mainFrame;
        initComponents();
        myInitDatas();

    }

    private void myInitDatas() {
        UserEbi userEbi = UserEbiFactory.getUserEbi();
        Object[] objs = userEbi.getAllUser();
        this.listUsers.setListData(objs);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    //GEN-BEGIN:initComponents
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        listUsers = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(800, 600));
        setLayout(null);

        listUsers.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "" };

            public int getSize() {
                return strings.length;
            }

            public Object getElementAt(int i) {
                return strings[i];
            }
        });
        jScrollPane1.setViewportView(listUsers);

        add(jScrollPane1);
        jScrollPane1.setBounds(170, 170, 410, 210);

        jLabel1.setText("\u4fe1\u606f");
        add(jLabel1);
        jLabel1.setBounds(350, 90, 70, 50);
    }// </editor-fold>
    //GEN-END:initComponents

    //GEN-BEGIN:variables
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listUsers;
    // End of variables declaration//GEN-END:variables

}
时间: 2024-07-29 19:13:49

Java---设计模式app小软件汇总应用的相关文章

【1】JAVA---地址App小软件(AddressApp.class)(初步接触项目开发的分层思想)(表现层)

这个是表现层的main方法. 实现的地址信息有: 姓名,性别,年龄,电话,地址. 实现的功能有: 增加地址: 删除地址: 修改地址: 查找地址:其中年龄的查找为年龄段的查找. 数据存储的方式为文件存储和读写. 分层的思想是:表现层调用逻辑层,逻辑层调用数据层.不可以反过来 每个class文件都带了包名字,建好文件就可以了. /* * AddressApp.java * */ package cn.hncu.addr; import java.awt.Panel; import javax.swi

【6】JAVA---地址App小软件(QueryPanel.class)(表现层)

查找模块: 年龄可进行段查找. 其他的都是模糊匹配. 空格为无用字符,会屏蔽的(除年龄). (如果在年龄中输入空格,会出现异常,当时没想到这点,要防护这点很容易的,但因为在这个小软件的编写过程,我主要学的是java项目开发的分层思想,软件可能bug比较多,望见谅.) /* * QueryPanel.java * */ package cn.hncu.addr.ui; import javax.swing.JFrame; import javax.swing.JOptionPane; import

【8】JAVA---地址App小软件(AddrDaoFile .class)(数据层)

实现数据进行文件的存储和读写. 本软件也就到此结束了. 没多少可以讲的. 因为这个小软件也就8个类,主要学习的也就是一个分层思想的简单应用. package cn.hncu.addr.dao; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectI

【3】JAVA---地址App小软件(AddPanel.class)(表现层)

添加地址信息界面. 年龄和地址必须是数字,否则会弹出窗口提示. 地址信息不能为空. /* * AddPanel.java * * Created on __DATE__, __TIME__ */ package cn.hncu.addr.ui; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import cn.hncu.addr.business.AddrBusiness

【2】JAVA---地址App小软件(ListPanel.class)(表现层)

这个是表现层的主界面. /* * ListPanel.java * */ package cn.hncu.addr.ui; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JOptionPane; import cn.hncu.addr.business.AddrBusiness; /** * * @author __chx__ */ public

【4】JAVA---地址App小软件(UpdatePanel.class)(表现层)

修改地址信息的一个表现层类. 必须选中地址,才能修改,否则会弹出窗口提示, 修改地址界面: /* * UpdatePanel.java * */ package cn.hncu.addr.ui; import javax.swing.JFrame; import javax.swing.JOptionPane; import cn.hncu.addr.business.AddrBusiness; /** * * @author __chx__ */ public class UpdatePane

【5】JAVA---地址App小软件(DeletePanel.class)(表现层)

删除地址的表现层类. 如果没有选中要删除的地址信息,会出现窗口提示: 删除地址界面:(无法修改数据,只能看) /* * DeletePanel.java * */ package cn.hncu.addr.ui; import javax.swing.JFrame; import javax.swing.JOptionPane; import cn.hncu.addr.business.AddrBusiness; /** * * @author __chx__ */ public class D

【7】JAVA---地址App小软件(AddrBusiness.class)(逻辑层)

这个...没多少好解释的... 表现层的增删改查的具体实现类. package cn.hncu.addr.business; import javax.swing.JOptionPane; import cn.hncu.addr.dao.AddrDaoFile; import sun.security.util.Length; public class AddrBusiness { //静态方法.访问的是同一个对象--也就是说就算是new这个对象,也只是引用之前的那个静态对象 private s

每周聚划算 超值软件汇总第三期:厉害了word哥!“小京东+”商城0.6折立省2681元!

总担心好价格稍纵即逝?云市场头条每周聚划算!超值软件汇总,致力于为大家发现.比价.筛选性价比最高的靠谱软件,让企业更加省心省力! 距离云市场12.21促销还有一周时间,预算花的差不多的小伙伴们,最后一次特价采购机会准备好了么?本期最值得推荐的是商之翼的"小京东+"SaaS商城,云市场特价第一年只要0.6折199元,立刻节省2681元!厉害了word哥! > 阿里云官方实验平台+云中沙箱沙箱点八折优惠 云中沙箱,基于阿里云真实环境的官方实验平台,深度体验阿里云真实环境中的操作,根据