下載本文檔
版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、interface Log/ 抽象工廠public void writelog();class FileLog implements Logpublic void writelog() / TODO Auto-generated method stubSystem.out.println(" 文件日志記錄 ");class DatabaseLog implements Logpublic void writelog() / TODO Auto-generated method stubSystem.out.println(" 數(shù)據(jù)庫日志記錄 "); in
2、terface LogFactorypublic Log creatLog();class FileLogFactory implements LogFactorypublic Log creatLog() / TODO Auto-generated method stub return new FileLog();class DatabaseFactory implements LogFactorypublic Log creatLog() / TODO Auto-generated method stub return new DatabaseLog();public class Clie
3、nt public static void main( String args) LogFactory factory;Log log;factory=new DatabaseFactory(); log=factory.creatLog(); log.writelog();/建造者模式public class Actor / 復(fù)合場(chǎng)品private String type;private String sex;private String face;private String costume;private String hairstyle;public String getType()
4、return type;public void setType(String type) this.type = type;public String getSex() return sex;public void setSex(String sex) this.sex = sex;public String getFace() return face;public void setFace(String face) this.face = face;public String getCostume() return costume;public void setCostume(String
5、costume) this.costume = costume;public String getHairstyle() return hairstyle;public void setHairstyle(String hairstyle) this.hairstyle = hairstyle;abstract class ActorBulider/ 抽象的建造者protected Actor actor=new Actor(); public abstract void bulidType(); public abstract void bulidSex(); public abstract
6、 void bulidFace(); public abstract void BulidCostume(); public abstract void bulidHairstype(); public Actor creatActor() return actor;class HeroBuilder extends ActorBuliderpublic void BulidCostume() / TODO Auto-generated method stub actor.setCostume(盔甲);public void bulidFace() / TODO Auto-generated
7、method stub actor.setFace("英俊");public void bulidHairstype() / TODO Auto-generated method stub actor.setHairstyle(" 飄逸 ");public void bulidSex() / TODO Auto-generated method stubactor.setSex'"男"public void bulidType / TODO Auto-generated method stub actor.setType英雄;
8、class AngelBuilder extends ActorBuliderpublic void BulidCostume / TODO Auto-generated method stub actor.setCostume 白裙"public void bulidFace / TODO Auto-generated method stub actor.setFace"漂 亮"public void bulidHairstype / TODO Auto-generated method stub actor.setHairstyle" 披肩長發(fā) &q
9、uot;public void bulidSex / TODO Auto-generated method stub actor.setSex"女"public void bulidType / TODO Auto-generated method stub actor.setType"天使"class GhostBuilder extends ActorBuliderpublic void BulidCostume() / TODO Auto-generated method stub actor.setCostume( 黑衣);public void
10、 bulidFace() / TODO Auto-generated method stub actor.setFace('丑陋");public void bulidHairstype() / TODO Auto-generated method stub actor.setHairstyle(" 光頭 ");public void bulidSex() / TODO Auto-generated method stub actor.setSex('妖");public void bulidType() / TODO Auto-gene
11、rated method stub actor.setType(魔鬼");class ActorController / 角色控制:指揮者public Actor construct (ActorBulider ab)Actor actor;ab.BulidCostume(); ab.bulidFace(); ab.bulidHairstype(); ab.bulidSex();ab.bulidType(); actor=ab.creatActor(); return actor;public class Client public static void main( String
12、args) ActorController ac=new ActorController(); ActorBulider ab;ab=new GhostBuilder();Actor angel;angel=ac.construct(ab);System.out.println(angel.getType()+ "System.out.println(" 性別System.out.println(" 面容System.out.println(" 服飾 System.out.println(" 發(fā)型的外觀: ")"+angel
13、.getSex();"+angel.getFace(); "+angel.getCostume(); "+angel.getHairstyle();/原型模式class WeekLog implements Cloneable public String getName() return name;public void setName(String name) = name;public String getData() return data;public void setData(String data) this.data = data
14、;public String getContent() return content;public void setContent(String content) this.content = content;private String name;private String data;private String content;public Object clone()Object obj=null;tryobj=super.clone(); return obj; catch(CloneNotSupportedException e) System.out.println("
15、 不能復(fù)制 "); return null;public class Client public static void main( String args)WeekLog log_previous=new WeekLog(); log_previous.setName("zhangsan"); log_previous.setData("2022-12"); log_previous.setContent("busy");System.out.println("*week*");System.out.p
16、rintln(log_previous.getContent();System.out.println(log_previous.getName();System.out.println(log_previous.getData();WeekLog now; now=(WeekLog)log_previous.clone();now.setData("2022-13");System.out.println("*week*");System.out.println(now.getContent();System.out.println(now.getNa
17、me();System.out.println(now.getData();/適配器模式package hehe;import java.awt.BorderLayout;import java.awt.Container;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JDesktopPane;import javax.swing.JFrame;import javax.swing.JInternalFram
18、e;import javax.swing.event.InternalFrameAdapter;import javax.swing.event.InternalFrameEvent;/單class SubFrame extends JInternalFrameprivate static SubFrame frame;private SubFrame()super("子窗體 ,true,true,true,false);this.setLocation(20, 20);this.setSize(200, 200);this.addInternalFrameListener(new
19、MyIFListener(); this.setVisible(true);public static SubFrame getFrame()if(frame=null) frame=new SubFrame();return frame;class MyIFListener extends InternalFrameAdapterpublic void internalFrameClosing(InternalFrameEvent e) if(frame!=null) frame=null;public class Client extends JFrameprivate JButton b
20、utton;private JDesktopPane desktopPane;private SubFrame iFrame=null;public Client()super("主窗體);Container c=this.getContentPane();c.setLayout(new BorderLayout(); button=new JButton(" 單機(jī)內(nèi)部的窗體 "); button.addActionListener(new BtListener(); c.add(button,BorderLayout.SOUTH); desktopPane=ne
21、w JDesktopPane(); c.add(desktopPane); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLocationRelativeTo(null); this.setSize(400,400); this.show();class BtListener implements ActionListenerOverride public void actionPerformed(ActionEvent e) / TODO Auto-generated method stub if(iFrame!=n
22、ull)desktopPane.remove(iFrame);iFrame=SubFrame.getFrame(); desktopPane.add(iFrame);public static void main(String arg)new Client();interface DataOperationpublic int sort (int array);public int search(int array,int key);/快排適配器 class Quicksortpublic int quickSort(int array)sort(array, 0, array.length-
23、1); return array;public void sort(int a,int p,int r)int q=0;if(p<r)q=partition(a ,p,r); sort(a, p, q-1); sort(a, q+1, r);private int partition(int a, int p, int r) / TODO Auto-generated method stubint x=ar;int j=p-1;for(int i=p;i<=r-1;i+)if(ai<x)j+; swap(a,j,i);swap(a,j+1,r);return j+1;publ
24、ic void swap(int a,int i,int j)int t=ai;ai=aj;aj=t;/二分法查找適配器class BinarySearchpublic int binarySearch(int array,int key)int low=0;int high=array.length-1;while(low<=high)int mid=(low+high)/2;int midVal=arraymid;if(midVal<key)low=mid+1;else if(mid>key)high=mid-1;else return 1;return -1;class
25、 OperationAdapter implements DataOperationprivate Quicksort quicksort;private BinarySearch binarySearch;public OperationAdapter(Quicksort quicksort,BinarySearch binarySearch) this.quicksort=quicksort;this.binarySearch=binarySearch;public int search(int array, int key) / TODO Auto-generated method st
26、ubreturn binarySearch.binarySearch(array, key);public int sort(int array) / TODO Auto-generated method stubreturn quicksort.quickSort(array);public class Client public static void main(String args) DataOperation dataOperation;Quicksort quicksort=new Quicksort();BinarySearch binarySearch=new BinarySe
27、arch(); dataOperation=new OperationAdapter(quicksort, binarySearch); int array=13,24,15,36,26,17,68,78;int result;int value;System.out.println("the result is:");result=dataOperation.sort(array);for(int i=0;i<result.length;i+)System.out.print(resulti+",");System.out.println();
28、System.out.println("search result is:"); value=dataOperation.search(array, 24); if(value!=-1)System.out.println("24 be find");else System.out.println("24 is not find");/橋連接模式abstract class OprationSystemVersionprotected VideoFile vf;public void setVf(VideoFile vf) this.
29、vf = vf;public abstract void paly( String fileName);interface VideoFilepublic void decode(String osType,String fileName);class MPEGFile implements VideoFilepublic void decode(String osType, String fileName) / TODO Auto-generated method stubSystem.out.println(" 格式為 MPEG 的視屏 "+fileName+"
30、; 在 "+osType+" 中播放 "); class RMVBFile implements VideoFilepublic void decode(String osType, String fileName) / TODO Auto-generated method stubSystem.out.println(" 格式為 RMVB 的視屏 "+fileName+" 在 "+osType+" 中播放 "); class AVIFile implements VideoFilepublic void
31、 decode(String osType, String fileName) / TODO Auto-generated method stubSystem.out.println(" 格式為 AVI 的視屏 "+fileName+" 在 "+osType+" 中播放 "); class WindowsVersion extends OprationSystemVersionOverridepublic void paly(String fileName) / TODO Auto-generated method stubvf.de
32、code("windows", fileName);class LinuxVersion extends OprationSystemVersionOverridepublic void paly(String fileName) / TODO Auto-generated method stubvf.decode("LINUx", fileName); class UnixVersion extends OprationSystemVersionOverridepublic void paly(String fileName) / TODO Auto-
33、generated method stub vf.decode("Unix", fileName);public class Client public static void main(String args) VideoFile videoFile;OprationSystemVersion oprationSystemVersion; videoFile=new A VIFile();oprationSystemVersion=new WindowsVersion(); oprationSystemVersion.setVf(videoFile); oprationS
34、ystemVersion.paly(" 讓子彈飛 ");import java.util.*;/抽象文件類abstract class AbstractFilepublic abstract void add(AbstractFile element);public abstract void remove (AbstractFile element); public abstract void display();/容器類class Folder extends AbstractFileprivate ArrayList fileList=new ArrayList();
35、private String fileName;public Folder(String fileName)this.fileName=fileName;Overridepublic void add(AbstractFile element) / TODO Auto-generated method stub fileList.add(element);Overridepublic void display() / TODO Auto-generated method stubSystem.out.println(" 文件夾 "+fileName+" 包含如下資
36、料 "); for(Object obj:fileList)(AbstractFile)obj).display();Overridepublic void remove(AbstractFile element) / TODO Auto-generated method stub fileList.remove(element);/葉子構(gòu)件class ImageFile extends AbstractFileprivate String fileName;public ImageFile(String fileName) this.fileName=fileName;Overri
37、depublic void add(AbstractFile element) / TODO Auto-generated method stubSystem.out.println(" 對(duì)不起不支持該方法 ");Overridepublic void display() / TODO Auto-generated method stub System.out.println(" 瀏覽圖片文件 "+fileName);Overridepublic void remove(AbstractFile element) / TODO Auto-generate
38、d method stub System.out.println(" 對(duì)不起不支持該方法 ");class TextFile extends AbstractFileprivate String fileName;public TextFile(String fileName)this.fileName=fileName;Overridepublic void add(AbstractFile element) / TODO Auto-generated method stub System.out.println(" 對(duì)不起不支持該方法 ");Over
39、ridepublic void display() / TODO Auto-generated method stubSystem.out.println(" 瀏覽文本文件 "+fileName); Overridepublic void remove(AbstractFile element) / TODO Auto-generated method stub System.out.println(" 對(duì)不起不支持該方法 ");public class Client public static void main(String args) Abstra
40、ctFile file1,file2,folder1,folder2,folder3;file1=new ImageFile("girl.jpg"); file2=new TextFile("java design"); file1.add(file1);file1.display();folder1=new Folder("new Imag");folder1.add(file1);folder2=new Folder("personer");folder2.add(file2);folder1.display(
41、);folder3=new Folder("zi liao ku");folder3.add(folder1);folder3.add(folder2);folder3.display();abstract class Windowpublic abstract void display();class SimpleWindow extends Window Override public void display() / TODO Auto-generated method stub System.out.println(" 顯示窗體 ");/窗體裝飾
42、類 : 抽象裝飾類 class WindowDecorator extends Window private Window window;public WindowDecorator(Window window) this.window=window;Overridepublic void display() / TODO Auto-generated method stub window.display();/滾動(dòng)條窗體裝飾類:具體抽象類 class ScrollbarDecorator extends WindowDecoratorpublic ScrollbarDecorator(Win
43、dow window) super(window);/ TODO Auto-generated constructor stubpublic void display() this.setScrollbar(); super.display();public void setScrollbar() / TODO Auto-generated method stub System.out.println("add Scrollbar");class TransparentDecorator extends WindowDecorator public TransparentD
44、ecorator(Window window) super(window);/ TODO Auto-generated constructor stubpublic void display() this.setTransparent(); super.display();private void setTransparent() / TODO Auto-generated method stub System.out.println("set window transparent"); public class Clientpublic static void main(
45、String args)/ TODO 自動(dòng)生成方法存根Window windows,windowsb,windowT; windows=new SimpleWindow(); windowsb=new ScrollbarDecorator(windows); windowT=new TransparentDecorator(windowsb); windowT.display();import java.beans.DesignMode; import java.io.FileInputStream;import java.io.FileNotFoundException; import ja
46、va.io.FileOutputStream;import java.io.IOException;/加密外觀類class EncryptFacade private FileReader reader; private CipherMachine clpher; private FileWriter writer;public EncryptFacade() reader = new FileReader(); writer = new FileWriter(); clpher = new CipherMachine();public void fileEncrypt(String file
47、NameSrc, String fileNameDes) String plainStr = reader.read(fileNameSrc);String encryptStr = clpher.encrypt(plainStr); writer.write(encryptStr, fileNameDes);/文件讀取類:子系統(tǒng)類class FileReader public String read(String FileNameSrc) System.out.println(" read filesrc");StringBuffer sBuffer = new Stri
48、ngBuffer();try FileInputStream inFS = new FileInputStream(FileNameSrc); int data;while (data = inFS.read() != -1) sBuffer = sBuffer.append(char) data); inFS.close(); catch (FileNotFoundException e) System.out.println("file not find");/ TODO: handle exception catch (IOException e) System.ou
49、t.println(" file farr");/ TODO: handle exceptionreturn sBuffer.toString();/文件保存class FileWriterpublic void write(String encryptSrc,String fileNameDes)System.out.println("file save");try FileOutputStream outFS=new FileOutputStream(fileNameDes);outFS.write(encryptSrc.getBytes(); ou
50、tFS.close(); catch (FileNotFoundException e) / TODO: handle exception System.out.println("file not find");catch (IOException e) / TODO: handle exceptionSystem.out.println(" file error");/文件加密class CipherMachinepublic String encrypt(String plainText)System.out.println("file i
51、s encrypting");String eString=""for(int i=0; i<plainText.length();i+)/String cString=String.valueOf(plainText.charAt(i)%7);String cString=String.valueOf(plainText); eString+=cString;return eString;public class Client public static void main(String args) / TODO 自動(dòng)生成方法存根EncryptFacade
52、 eFacade=new EncryptFacade(); eFacade.fileEncrypt("facade/src.txt", "facade/des.txt");/坐標(biāo)類:外部狀態(tài)類import java.util.Hashtable;/享元模式class Coordinatesprivate int x;private int y;public Coordinates(int x, int y) this.x = x;this.y = y;public int getX() return x; public void setX(int x)
53、this.x = x;public int getY() return y; public void setY(int y) this.y = y;/抽象的享元類abstract class IgoChessmanpublic abstract String getColor();public void locate(Coordinates coordinates)System.out.println(" 棋 子 的 顏 色 "+this.getColor()+" 棋 子 的 位 置 "+coordinates.getX()+","+
54、coordinates.getY();class BlackIgoChessman extends IgoChessmanOverridepublic String getColor() / TODO Auto-generated method stub return "black"class WhiteIgoChessman extends IgoChessmanOverridepublic String getColor() / TODO Auto-generated method stub return "white"/享元工廠類 class Ig
55、oChessmanFactoryprivate static IgoChessmanFactory instance=new IgoChessmanFactory(); private static Hashtable hashtable;private IgoChessmanFactory() hashtable=new Hashtable(); IgoChessman black,white; black=new BlackIgoChessman(); white=new WhiteIgoChessman(); hashtable.put("b", black); ha
56、shtable.put("w", white);public static IgoChessmanFactory getInstance()return instance;public static IgoChessman getIgoChessman(String color)return (IgoChessman)hashtable.get(color);public class Client public static void main(String args) / TODO 自動(dòng)生成方法存根IgoChessman black1,black2,black3,white1,white2; IgoChessmanFactory factory; factory=IgoChessmanFactory.getInstance(); black1=factory.getIgoChessman("b"); black2=factory.getIgoChessman
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 《翡翠培訓(xùn)資料》課件
- 《證券買賣技巧教案》課件
- 《證券基金銷售培訓(xùn)》課件
- 單位管理制度集粹匯編員工管理篇
- 單位管理制度分享大全【人力資源管理篇】
- 《社區(qū)工作實(shí)務(wù)》課件
- 單位管理制度范例選集【人力資源管理篇】十篇
- 單位管理制度范例合集職工管理十篇
- 單位管理制度呈現(xiàn)合集【人事管理】十篇
- 寒假自習(xí)課 25春初中地理八年級(jí)下冊(cè)人教版教學(xué)課件 第八章 第二節(jié) 干旱的寶地-塔里木盆地 第2課時(shí) 油氣資源的開發(fā)
- 2023年鞍山市海城市教育局畢業(yè)生招聘筆試真題
- 北京2025年首都醫(yī)科大學(xué)附屬北京友誼醫(yī)院招聘140人歷年參考題庫(頻考版)含答案解析
- 遼寧省撫順縣2024-2025學(xué)年九年級(jí)上學(xué)期期末物理試卷(含答案)
- 2024-2025學(xué)年安徽省合肥市巢湖市三年級(jí)數(shù)學(xué)第一學(xué)期期末達(dá)標(biāo)測(cè)試試題含解析
- 2023-2024人教版上學(xué)期小學(xué)英語三年級(jí)上冊(cè)期末試卷
- 冬季施工階段安全事故案例分析及對(duì)策
- 造船廠全套作業(yè)指導(dǎo)書
- 施工現(xiàn)場(chǎng)消防安全操作規(guī)程
- A4標(biāo)簽打印模板
- (完整版)工程項(xiàng)目管理組織機(jī)構(gòu)
- 工程質(zhì)量檢測(cè)內(nèi)容包括哪些?
評(píng)論
0/150
提交評(píng)論