Composite Pattern
複合模式-我們在真實世界可以將東西的數量簡單分為複數和單數,在程式同樣道理,我們設計時可以將元件(Component)或是物件(Object)是分為複數和單數。設計模式是將單數個體和負數的個體可以同時用一個介面(Interface)做規格,來完成加入元件、取得元件和刪除元件的動作。
架構圖:
例子:
我們做檔案的上傳程式,它可以做單筆或是多筆上傳,
FileCommonComponent.java
//Componentpublic abstract class FileCommonComponent {
// define attribute private String fileName ;
private long fileSize ;
//constractor public FileCommonComponent()
{ }
public FileCommonComponent(String filename, long filesize)
{
this.fileName = filename ;
this.fileSize = filesize ;
}
// abstract method public abstract void getFileComponentSize() ;
}
Filecomponent .java
//component
public class Filecomponent extends FileCommonComponent{
public class Filecomponent extends FileCommonComponent{
private String fileName ;
private long fileSize ;
public Filecomponent(String filename , long filesize) {
// TODO Auto-generated constructor stub
super(filename, filesize) ;
this.fileName = filename ;
this.fileSize = filesize ;
}
///method
public void getFileComponentSize()
{
StringBuffer strbuffer = new StringBuffer() ;
strbuffer.append("檔案名稱"+this.fileName+",檔案大小:"+this.fileSize);
System.out.println(strbuffer.toString()) ;
}
}
public void getFileComponentSize()
{
StringBuffer strbuffer = new StringBuffer() ;
strbuffer.append("檔案名稱"+this.fileName+",檔案大小:"+this.fileSize);
System.out.println(strbuffer.toString()) ;
}
}
FileComposites.java
public class FileComposites extends FileCommonComponent{
Vector fileVect = new Vector() ;
Filecomponent component ;
public FileComposites()
{
super() ;
}
//method public void addFileComponent(FileCommonComponent component)
{
fileVect.add(component) ;
}
public Filecomponent getFileComponentSize(int location)
{
Filecomponent component = (Filecomponent)fileVect.elementAt(location) ;
return component ;
}
public void getFileComponentSize()
{
Enumeration<FileCommonComponent> e = this.fileVect.elements() ;
while(e.hasMoreElements())
{
FileCommonComponent f = e.nextElement() ;
f.getFileComponentSize();
}
Filecomponent component ;
public FileComposites()
{
super() ;
}
//method public void addFileComponent(FileCommonComponent component)
{
fileVect.add(component) ;
}
public Filecomponent getFileComponentSize(int location)
{
Filecomponent component = (Filecomponent)fileVect.elementAt(location) ;
return component ;
}
public void getFileComponentSize()
{
Enumeration<FileCommonComponent> e = this.fileVect.elements() ;
while(e.hasMoreElements())
{
FileCommonComponent f = e.nextElement() ;
f.getFileComponentSize();
}
}
}
}
TestComposite.java
public class TestComposite {
public static void main(String[] args)
{
FileCommonComponent component = new Filecomponent("test.txt",(long) 1000.5f);
FileCommonComponent component2 = new Filecomponent("test2.txt",(long) 1000.5f);
FileComposites composites = new FileComposites() ;
composites.addFileComponent(component);
composites.addFileComponent(component2);
composites.getFileComponentSize() ;
}
}
{
FileCommonComponent component = new Filecomponent("test.txt",(long) 1000.5f);
FileCommonComponent component2 = new Filecomponent("test2.txt",(long) 1000.5f);
FileComposites composites = new FileComposites() ;
composites.addFileComponent(component);
composites.addFileComponent(component2);
composites.getFileComponentSize() ;
}
}
留言
張貼留言