9日目企業仕入れ預金システム
オブジェクトに深く入り込む
ケース:エンタープライズ・ストレージ・システムインタフェース:Javaでは、インタフェースは挿入可能な特性を実現する保証です.インタフェースを定義するキーワードはinterfaceであり、インタフェースを実現するキーワードはimplementsであり、1つのクラスは複数のインタフェースを実現することができ、インタフェース間の継承は多重継承をサポートする. インタフェースと抽象クラスの異同キーワード対比インタフェース:宣言インタフェース:interface+インタフェース名(命名方式同類)、実装:実装クラス+implements+インタフェース名()の作成;実装クラス:インタフェースが実装されていないメソッドを書き換える必要があります.
抽象クラス:抽象クラスを宣言:abstract+class+クラス名.抽象メソッド:abstract+void+メソッド名()を宣言する必要があります.≪サブクラス|SubClass|emdw≫:抽象クラスを継承するサブクラスは、抽象メソッドを書き換える必要があります.クラス/クラスとクラス/インタフェースの関係 IS-A関係:継承/実現 HAS-A関係:関連/集約(集約)/合成 USE-A関係:依存 UML:統合モデリング言語(標準のグラフィックスシンボル) クラス図:クラスおよびクラスとクラスの関係を記述するグラフィック記号. 用例図:需要をキャプチャする. シーケンス図:オブジェクトの相互関係を説明します.
オブジェクト向け設計原則 単一職責原則(SRP):クラスの設計は高集約を実現し、1つのクラスは単一の職責(それをしてはいけないことをしない)だけを負う. 開閉原則(OCP):ソフトウェアシステムは拡張(拡張に対して開放)を受け入れるべきで、修正(修正に対して閉鎖)を受け入れない.開閉の原則を満たすには、抽象が鍵であり、パッケージが可変である. 依存逆転原則(DIP):インタフェース向けプログラミング.変数の参照タイプ、メソッドのパラメータタイプ、メソッドの戻りタイプを宣言する場合は、可能な限り特定のタイプではなく抽象タイプを使用します. リッツ置換の原則(LSP):親タイプを子タイプで置換することは問題なく、親タイプで置換することは通常できません. インターフェース分離の原則(ISP):インターフェースは小さくて専門的で、大きくて完全ではない. 合成集約多重化の原則(CARP):継承関係多重化コードではなく、強い関連関係多重化コードを優先的に考慮する. ディミット法則(LoD):対象間でできるだけ連絡が少ない.
ユーザークラス
メインウィンドウ
ユーザー・クラスおよびサービス・クラス
ケース:エンタープライズ・ストレージ・システム
抽象クラス:抽象クラスを宣言:abstract+class+クラス名.抽象メソッド:abstract+void+メソッド名()を宣言する必要があります.≪サブクラス|SubClass|emdw≫:抽象クラスを継承するサブクラスは、抽象メソッドを書き換える必要があります.
package com.lovoinfo.ui;
/**
/*
*/
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
/**
/*
@SuppressWarnings("serial")
public class LoginFrame extends JDialog {
private JLabel uidLabel, pwdLabel;
private JTextField uidField;
private JPasswordField pwdField;
private JButton loginButton, cancelButton;
private List userList = new ArrayList();//
public LoginFrame() {
this.setTitle(" ");
this.setSize(300, 180);
this.setResizable(false);
this.setLocationRelativeTo(null);
// this.setDefaultCloseOperation(HIDE_ON_CLOSE);//
initComponents();
}
@Override
public void paint(Graphics g) {
super.paint(g);
}
private boolean checkLogin(String username, String password) {
for(User user : userList) {
if(user.getUsername().equals(username)) {
return user.getPassword().equals(password);
}
}
return false;
}
private void initComponents() {
userList.add(new User("admin", "123456"));
userList.add(new User("hellokitty", "1qaz2wsx"));
userList.add(new User("jack", "123123"));
uidLabel = new JLabel(" : ");
pwdLabel = new JLabel(" : ");
uidField = new JTextField();
pwdField = new JPasswordField();
loginButton = new JButton(" ");
loginButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String username = uidField.getText().trim();
String password = new String(pwdField.getPassword());
if(checkLogin(username, password)) {
LoginFrame.this.setVisible(false);
new MainFrame(LoginFrame.this).setVisible(true);
}
else {
JOptionPane.showMessageDialog(null, " ", " ", 0);
}
}
});
cancelButton = new JButton(" ");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
this.setLayout(null);
uidLabel.setBounds(40, 20, 50, 30);
pwdLabel.setBounds(40, 60, 50, 30);
uidField.setBounds(100, 25, 150, 20);
pwdField.setBounds(100, 65, 150, 20);
loginButton.setBounds(80, 100, 60, 30);
cancelButton.setBounds(160, 100, 60, 30);
this.add(uidLabel);
this.add(pwdLabel);
this.add(uidField);
this.add(pwdField);
this.add(loginButton);
this.add(cancelButton);
}
}
ユーザークラス
package com.lovoinfo.ui;
public class User {
private String username;
private String password;
public User(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
メインウィンドウ
package com.lovoinfo.ui;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.lovoinfo.biz.impl.UserServiceImpl;
import com.lovoinfo.domain.User;
/**
* ( )
* @author jackfrued
*
*/
@SuppressWarnings("serial")
public class RegInternalFrame extends JInternalFrame {
private class ButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == regButton) {
String username = uidField.getText().trim();
String password = new String(pwdField.getPassword());
String email = emailField.getText().trim();
User user = new User(username, password, email);
user.setUserService(new UserServiceImpl());
if(user.register()) {
JOptionPane.showMessageDialog(null, " !");
reset();
RegInternalFrame.this.setVisible(false);
}
else {
JOptionPane.showMessageDialog(null, " !", " ", 0);
}
}
else if(e.getSource() == resetButton) {
reset();
}
}
}
private static Font defaultFont = new Font(" ", Font.PLAIN, 14);
private String[] labelNames = {" : ", " : ", " : ", " : "};
private JLabel[] labels = new JLabel[4];
private JTextField uidField, emailField;
private JPasswordField pwdField, rePwdField;
private JButton regButton, resetButton;
private RegInternalFrame() {
super(" ", false, true, false, false);
this.setSize(270, 300);
this.setLocation(50, 50);
this.setVisible(true);
this.setDefaultCloseOperation(HIDE_ON_CLOSE);
initComponents();
}
private static RegInternalFrame instance = new RegInternalFrame();
public static RegInternalFrame getInstance() {
return instance;
}
private void initComponents() {
this.setLayout(null);
for(int i = 0; i < labels.length; i++) {
labels[i] = new JLabel(labelNames[i]);
labels[i].setBounds(20, 30 + 48 * i, 70, 24);
this.add(labels[i]);
}
uidField = new JTextField();
uidField.setBounds(100, 30, 120, 24);
this.add(uidField);
pwdField = new JPasswordField();
pwdField.setBounds(100, 78, 120, 24);
this.add(pwdField);
rePwdField = new JPasswordField();
rePwdField.setBounds(100, 126, 120, 24);
this.add(rePwdField);
emailField = new JTextField();
emailField.setBounds(100, 174, 150, 24);
this.add(emailField);
ActionListener handler = new ButtonHandler();
regButton = new JButton(" ");
regButton.setBounds(60, 230, 70, 24);
regButton.addActionListener(handler);
this.add(regButton);
resetButton = new JButton(" ");
resetButton.setBounds(150, 230, 70, 24);
resetButton.addActionListener(handler);
this.add(resetButton);
//
for(Component c : this.getContentPane().getComponents()) {
c.setFont(defaultFont);
}
}
private void reset() {
//
for(Component c : RegInternalFrame.this.getContentPane().getComponents()) {
if(c instanceof JTextField) {
((JTextField) c).setText("");
}
else if(c instanceof JPasswordField) {
((JPasswordField) c).setText("");
}
}
}
}
package com.lovoinfo.ui;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
@SuppressWarnings("serial")
public class SysManPanel extends JPanel {
private class MouseHandler extends MouseAdapter {
private int index;
public MouseHandler(int index) {
this.index = index;
}
@Override
public void mouseEntered(MouseEvent e) {
labels[index].setIcon(new ImageIcon(buttonRollImageNames[index]));
}
@Override
public void mouseExited(MouseEvent e) {
labels[index].setIcon(new ImageIcon(buttonImageNames[index]));
}
@Override
public void mousePressed(MouseEvent e) {
labels[index].setIcon(new ImageIcon(buttonDownImageNames[index]));
}
@Override
public void mouseReleased(MouseEvent e) {
labels[index].setIcon(new ImageIcon(buttonRollImageNames[index]));
}
@Override
public void mouseClicked(MouseEvent e) {
RegInternalFrame frame = RegInternalFrame.getInstance();
if(mainFrame.containsInternalFrame(frame)) {
mainFrame.showInternalFrame(frame);
}
else {
mainFrame.addInternalFrame(frame);
}
}
}
private static String[] buttonImageNames = {
" .png", " .png", " .png"
};
private static String[] buttonRollImageNames = {
" _roll.png", " _roll.png", " _roll.png"
};
private static String[] buttonDownImageNames = {
" _down.png", " _down.png", " _down.png"
};
private JLabel[] labels = new JLabel[3];
private MainFrame mainFrame;
public SysManPanel(MainFrame mainFrame) {
this.mainFrame = mainFrame;
this.setLayout(new FlowLayout(FlowLayout.LEFT));
for(int i = 0; i < labels.length; i++) {
labels[i] = new JLabel(new ImageIcon(buttonImageNames[i]));
labels[i].addMouseListener(new MouseHandler(i));
this.add(labels[i]);
}
}
}
package com.lovoinfo.ui;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JDesktopPane;
@SuppressWarnings("serial")
public class MyDesktopPane extends JDesktopPane {
private static ImageIcon bgImage = new ImageIcon("welcome.jpg");
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage.getImage(), 0, 0, this.getWidth(), this.getHeight(), null);
}
}
ユーザー・クラスおよびサービス・クラス
package com.lovoinfo.domain;
import com.lovoinfo.biz.UserService;
public class User {
private String username;
private String password;
private String email;
private UserService userService;
public User() {
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
public User(String username, String password, String email) {
this.username = username;
this.password = password;
this.email = email;
}
public boolean login() {
return userService.doLogin(this);
}
public boolean register() {
return userService.doRegister(this);
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
package com.lovoinfo.biz;
import com.lovoinfo.domain.User;
/**
*
* @author jackfrued
*
*/
public interface UserService {
/**
*
* @param user
* @return true false
*/
public boolean doLogin(User user);
/**
*
* @param user
* @return true false
*/
public boolean doRegister(User user);
}
package com.lovoinfo.biz.impl;
import java.util.ArrayList;
import java.util.List;
import com.lovoinfo.biz.UserService;
import com.lovoinfo.domain.User;
/**
*
* @author jackfrued
*
*/
public class UserServiceImpl implements UserService {
private static List userList = new ArrayList();
static {
userList.add(new User("admin", "123456"));
userList.add(new User("jack", "abcdefg"));
}
public UserServiceImpl() {
}
@Override
public boolean doLogin(User user) {
User temp = findUserByUsername(user.getUsername());
if(temp != null) {
return temp.getPassword().equals(user.getPassword());
}
return false;
}
@Override
public boolean doRegister(User user) {
User temp = findUserByUsername(user.getUsername());
if(temp == null) {
userList.add(user);
return true;
}
return false;
}
//
private User findUserByUsername(String username) {
for(User temp : userList) {
if(temp.getUsername().equals(username)) {
return temp;
}
}
return null;
}
}