java-design pattern (structural type)-"Combined mode"

Source: Internet
Author: User

1. Combination mode (Composite)

Definition: Combined mode is sometimes called part-the overall mode is convenient when dealing with problems like tree structure

Use scene: Combine multiple objects together to manipulate, often used to represent a tree structure, such as a binary tree, number, etc., file directory.

2. File Replication Examples

File class:

Interface to the various operations of the file public interface fileoperate {void copy ();}    File: Single document class file implements fileoperate{//file name private String filename=null;    Public File (String fileName) {this.filename=filename; } @Overridepublic void Copy () {//TODO auto-generated method StubSystem.out.println ("Copy" "+filename+" "File ...        ");} Public String GetFileName () {return fileName;} public void Setfilename (String fileName) {this.filename = FileName;}}    Folder: Contains all sub-files class Filefolder implements fileoperate{//folder name private String foldername=null;    All sub-Files private arraylist<file> child=new arraylist<file> ();    Public Filefolder (String folderName) {this.foldername=foldername;    }//Add a child file method public void Add (file file) {child.add (file);    } @Overridepublic void Copy () {//TODO auto-generated Method Stub System.out.println ("Current folder:" +foldername);    Iterator It=child.iterator ();  while (It.hasnext ()) {file f= (file) It.next ();    F.copy (); }}        Public String Getfoldername () {return folderName;} public void Setfoldername (String folderName) {this.foldername = FolderName;}}

Test:

public class Test {public static void main (string[] args) {//TODO auto-generated method stub        filefolder root=new File Folder ("My Videos");        File File1=new file ("Fatal Id.avi");        File File2=new file ("Two days a night. rmvb");        Add File        root.add (file1);        Root.add (file2);        Copy file        root.copy ();}}

Operation Result:

Current folder: My video copy "Fatal Id.avi" File ... Copy "Two days a night. rmvb" File ...

  

java-design pattern (structural type)-"Combined mode"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.