设计模式程序.docx_第1页
设计模式程序.docx_第2页
设计模式程序.docx_第3页
设计模式程序.docx_第4页
设计模式程序.docx_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

作业一(1):工厂方法模式AmdCPU.java:public class AmdCPU implements CPUpublic void play()System.out.println(AmdCPU生产中.);AmdCPUFactory.java:public class AmdCPUFactory implements CPUFactorypublic CPU productCPU()System.out.println(AmdCPU工厂生产AmdCPU。);return new AmdCPU();Client .java:public class Client public static void main(String args) tryCPU cpu;CPUFactory factory;factory=(CPUFactory)XMLUtil.getBean();cpu=ductCPU();cpu.play();catch(Exception e) e.printStackTrace();System.out.println(e.getMessage();CPU.java:public interface CPU public void play();CPUFactory.java:public interface CPUFactorypublic CPU productCPU();IntelCPU.java:public class IntelCPU implements CPUpublic void play()System.out.println(IntelCPU生产中.);IntelCPUFactory .java:public class IntelCPUFactory implements CPUFactory public CPU productCPU()System.out.println(IntelCPU工厂生产IntelCPU。);return new IntelCPU();XMLUtil.java:import java.io.*;import javax.xml.parsers.*;import org.w3c.dom.*;import java.lang.*;public class XMLUtil public static Object getBean()tryDocumentBuilderFactory dFactory=DocumentBuilderFactory.newInstance();DocumentBuilder builder=dFactory.newDocumentBuilder();Document doc;doc=builder.parse(new File(config.xml);NodeList nl=doc.getElementsByTagName(className);Node classNode=nl.item(0).getFirstChild();String cName=classNode.getNodeValue();Class c=Class.forName(exe1_1.+cName);Object obj=c.newInstance();return obj;catch(Exception e)e.printStackTrace();return null;作业一(2):抽象工厂模式AmdCPU.java:public class AmdCPU implements CPUpublic void play()System.out.println(AmdCPU生产中.);AmdFactory .java:public class AmdFactory implements EFactorypublic CPU productCPU()return new AmdCPU();public Memory productMemoryBank()return new AmdMemory();AmdMemory.java:public class AmdMemory implements Memorypublic void produce()System.out.println(Amd内存条生产中.);Client.java:public class Client public static void main(String args)tryCPU cpu;EFactory factory;Memory me;factory=(EFactory)XMLUtil.getBean();cpu=ductCPU();cpu.play();me=ductMemoryBank();duce();catch(Exception e)e.printStackTrace();CPU.java:public interface CPU public void play();EFactory.java:public interface EFactory public CPU productCPU();public Memory productMemoryBank();IntelCPU.java:public class IntelCPU implements CPUpublic void play()System.out.println(IntelCPU生产中.);IntelFactory.java:public class IntelFactory implements EFactorypublic CPU productCPU()return new IntelCPU();public Memory productMemoryBank()return new IntelMemory();IntelMemory.java:public class IntelMemory implements Memorypublic void produce()System.out.println(Intel内存条生产中.);Memory.java:public interface Memory public void produce();XMLUtil.java:import java.io.File;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.NodeList;public class XMLUtil public static Object getBean()tryDocumentBuilderFactory dFactory=DocumentBuilderFactory.newInstance();DocumentBuilder builder=dFactory.newDocumentBuilder();Document doc;doc=builder.parse(new File(config.xml);NodeList nl=doc.getElementsByTagName(className);Node classNode=nl.item(0).getFirstChild();String cName=classNode.getNodeValue();Class c=Class.forName(exe1_2.+cName);Object obj=c.newInstance();return obj;catch(Exception e)e.printStackTrace();return null;作业六:观察者模式Client.java:public class Client public static void main(String args)SeekJobCenter center=new SeekJobCenter();University ZhangLin=new University(center,a.txt);Master WangHao=new Master(center,b.txt);center.giveNewMess(三菱公司需要15个前端开发设计师。);center.notifyObservers();center.giveNewMess(宫崎骏公司需要2个动画设计师。);center.notifyObservers();center.giveNewMess(美的公司需要5个电工。);center.notifyObservers();center.giveNewMess(格力公司需要10个电工。);center.notifyObservers();Master.java:import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;public class Master implements ObserverSubject subject;File myFile;Master(Subject subject,String fileName)this.subject=subject;subject.addObserver(this);myFile=new File(fileName);public void hearTelephone(String heardMess)tryboolean boo=heardMess.contains(前端开发设计师)|heardMess.contains(软件);if(boo)RandomAccessFile out=new RandomAccessFile(myFile,rw);out.seek(out.length();byte b=heardMess.getBytes();out.write(b);System.out.println();System.out.print(我是一个研究生,);System.out.print(我向文件+myFile.getName()+写入如下内容:);System.out.print(heardMess);elseSystem.out.println();System.out.println(我是研究生,这次的信息中没有我需要的信息);catch(IOException exp)System.out.println(exp.toString();Observer.java:public interface Observer public void hearTelephone(String heardMess);SeekJobCenter.java:import java.util.ArrayList;public class SeekJobCenter implements SubjectString mess;boolean changed;ArrayList personList;SeekJobCenter()personList=new ArrayList();mess=;changed=false;public void addObserver(Observer o)if(!(personList.contains(o)personList.add(o);public void deleteObserver(Observer o)if(!(personList.contains(o)personList.remove(o);public void notifyObservers()if(changed)for(int i=0;ipersonList.size();i+)Observer observer=personList.get(i); observer.hearTelephone(mess);changed=false;public void giveNewMess(String str)if(str.equals(mess)changed=false;elsemess=str;changed=true;Subject.java:public interface Subject public void addObserver(Observer o);public void deleteObserver(Observer o);public void notifyObservers();University.java:import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;public class University implements ObserverSubject subject;File myFile;University(Subject subject,String fileName)this.subject=subject;subject.addObserver(this);myFile=new File(fileName);public void hearTelephone(String heardMess)tryRandomAccessFile out=new RandomAccessFile(myFile,rw);out.seek(out.length();byte b=heardMess.getBytes();out.write(b);System.out.println();System.out.print(我是一个大学生,);System.out.print(我向文件+myFile.getName()+写入如下内容:);System.out.print(heardMess);catch(IOException exp)System.out.println(exp.toString();作业七:策略模式BookCart.java:import java.util.ArrayList;class BookCart private ArrayList list=new ArrayList();public void addBookOrder(BookOrder order)list.add(order);public void deleteBookOrder(BookOrder order)list.remove(order);public double payBill()System.out.println(-阿毛书店,谢谢光临!-);double totalMoney=0;for(int i=0;ilist.size();i+) totalMoney+=list.get(i).getOrderMoney();System.out.println(您一共需要支付:+totalMoney+元。);return totalMoney;BookOrder.java:public class BookOrder private int bookID;private String type;private double price;private int amount;private Discount strategy;public BookOrder(int bookID,String type,double price,int amount)super();this.bookID=bookID;this.type=type;this.price=price;this.amount=amount;this.setDiscountStrategy();public int getBookID()return bookID;public void setBookID(int bookID)this.bookID=bookID;public String getType()return type;public void setType(String type)this.type=type;public double getPrice()return price;public void setPrice(double price)this.price=price;public int getAmount()return amount;public void setAmount(int amount)this.amount=amount;public void setDiscountStrategy()if(this.type.equals(coumputer)this.strategy=new ComputerBookDiscount();else if(this.type.equals(language)this.strategy=new LanguageBookDiscount();else if(this.type.equals(novel)this.strategy=new NovelBookDiscount();elsethis.strategy=new NullDiscount();public double getOrderMoney()double bill=pute(this.amount,this.price);System.out.println(书:+this.bookID+t+this.type+类t共:+this.amount+本,t单价为: +this.price+t原价为:+this.price*this.amount+t打折后为:+bill);return bill;Client .java:public class Client public static void main(String args)BookCart cart=new BookCart();BookOrder order1=new BookOrder(1001,ComputerBook,33.00,10);BookOrde

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论