工厂模式 - 程序实现(java)
09年5月CSDN一网友提出如下问题:
设计一个用于管理银行客户的类BankCustomer: 仅描述客户的几个重要方面: 帐号、身份证号、姓名、联系方式、密码、账户余额。 所有的成员变量均用private访问控制,因此每一个成员变量就要有相应的存取器 (getter和setter,即获取和设置其值的相应的成员方法。需要setter还是getter,还是两者都要,视情况而定) 成员方法: 开户(开户时必须要有身份证号),系统自动生成帐号,帐号使用系统时间(格式:"yyyyMMddHHmmss"14位),初始密码为“666666”。 注意开户和构造方法之间的关系。 存钱、取钱、显示账户信息、修改密码(密码最短要六位) 怎样在main中使用这个类,自行安排,要表现出你设计的类的各个方面,并在main中用英语加以注释
根据此,使用工厂模式设计如下若干类:
------------factory部分---------------------------xiaobin-------------
Customer: 抽象类(factory祖先类)
BankCustomer:继承类(factory类)
〉〉〉〉〉〉〉〉〉〉扩展部分
ContactWay:联系方式(factory引用类)
IM :实时消息(ContactWay引用类)
------------product部分---------------------------xiaobin-------------
Bank: 接口(product接口)
Account: 实现类(concrete product类)
类图如下:(使用Enterprise Architect绘制)
使用EA生成的代码如下:
Customer.java
package bankCustomer; /** * @author xiaobin * @version 1.0 * @created 23-5-2009 15:19:06 */ public abstract class Customer { private Bank bank; private double myMoney; public Bank m_Bank; public Customer(){ } public void finalize() throws Throwable { } public abstract Bank createAccount(); public void myAccount(){ } public void mySaveMoney(){ } public void myTakeMoney(){ } }
BankCustomer.java
package bankCustomer; /** * @author xiaobin * @version 1.0 * @created 23-5-2009 15:30:13 */ public class BankCustomer extends Customer { private ContactWay contact; private String name; private String personID; public Account m_Account; public ContactWay m_ContactWay; public BankCustomer(){ } public void finalize() throws Throwable { super.finalize(); } public Bank createAccount(){ return null; } private ContactWay getContact(){ return null; } private String getName(){ return ""; } private String getPersonID(){ return ""; } /** * * @param contactWay */ private void setContact(ContactWay contactWay){ } /** * * @param myName */ private void setName(String myName){ } /** * * @param myPersonID */ private void setPersonID(String myPersonID){ } }
ContactWay.java
package bankCustomer; /** * @author xiaobin * @version 1.0 * @created 23-5-2009 15:30:29 */ class ContactWay { private String email; private IM imContact; private String mobilePhone; private String phone; public IM m_IM; public ContactWay(){ } public void finalize() throws Throwable { } private String getEmail(){ return ""; } private IM getImContact(){ return null; } private String getMobilePhone(){ return ""; } private String getPhone(){ return ""; } /** * * @param myEmail */ private void setEmail(String myEmail){ } /** * * @param im */ private void setImContact(IM im){ } /** * * @param mobilePhone */ private void setMobilePhone(String mobilePhone){ } /** * * @param myPhone */ private void setPhone(String myPhone){ } }
IM.java
package bankCustomer; /** * @author xiaobin * @version 1.0 * @created 23-5-2009 15:30:39 */ class IM { private String MSN; private String QQ; public IM(){ } public void finalize() throws Throwable { } private String getMSN(){ return ""; } private String getQQ(){ return ""; } /** * * @param msn */ private void setMSN(String msn){ } /** * * @param qq */ private void setQQ(String qq){ } }
Bank.java
package bankCustomer; /** * @author xiaobin * @version 1.0 * @created 23-5-2009 15:19:16 */ public interface Bank { /** * * @param money */ public boolean saveMoney(double money); /** * * @param money */ public boolean takeMoney(double money); }
Account.java
package bankCustomer; /** * @author xiaobin * @version 1.0 * @created 07-7-2013 20:21:35 */ public class Account implements Bank { private String accountID; private double accountSurplus; private Date date; public Account(){ } public void finalize() throws Throwable { } /** * * @param val1 * @param val2 */ private double addAlgorithm(double val1, double val2){ return 0; } /** * * @param now */ private String createMyAccount(Date now){ return ""; } private String getAccountID(){ return ""; } /** * * @param oldPWD * @param curPWD */ private boolean modifyPWD(String oldPWD, String curPWD){ return false; } /** * * @param money */ public synchronized boolean saveMoney(double money){ return false; } private void setAccountID(){ } /** * * @param val1 * @param val2 */ private double subAlgorithm(double val1, double val2){ return 0; } /** * * @param money */ public synchronized boolean takeMoney(double money){ return false; } }