-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add composite in java in design-patterns
- Loading branch information
1 parent
96dda72
commit fa7e9e4
Showing
5 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
design-patterns-2/java/app/src/main/java/org/patterns/patterns/composite/BinaryFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.patterns.patterns.composite; | ||
|
||
/** | ||
* Leaf node in composite pattern. | ||
* Leaf has the most basic operations | ||
*/ | ||
public class BinaryFile extends File{ | ||
|
||
public BinaryFile(String name) { | ||
super(name); | ||
} | ||
|
||
@Override | ||
public void ls() { | ||
System.out.println(this.getIntendString() + "-" + this.getName()); | ||
} | ||
|
||
@Override | ||
public void addFile(File file) { | ||
throw new UnsupportedOperationException("Unimplemented method 'addFile'"); | ||
} | ||
|
||
@Override | ||
public File[] getFiles() { | ||
throw new UnsupportedOperationException("Unimplemented method 'getFiles'"); | ||
} | ||
|
||
@Override | ||
public boolean removeFile(File file) { | ||
throw new UnsupportedOperationException("Unimplemented method 'removeFile'"); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
design-patterns-2/java/app/src/main/java/org/patterns/patterns/composite/Directory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.patterns.patterns.composite; | ||
|
||
import java.util.ArrayList; | ||
|
||
/** | ||
* Composite in the composite pattern | ||
* That means it contains leaves or other composite objects inside of it (files list) | ||
*/ | ||
public class Directory extends File{ | ||
|
||
public Directory(String name) { | ||
super(name); | ||
files = new ArrayList<File>(); | ||
} | ||
|
||
private ArrayList<File> files; | ||
|
||
@Override | ||
public void ls() { | ||
System.out.println(this.getIntendString() + "/" + this.getName()); | ||
for (File file : files) { | ||
file.addIntend(this.getIntend() + 2); | ||
file.ls(); | ||
} | ||
} | ||
|
||
@Override | ||
public void addFile(File file) { | ||
this.files.add(file); | ||
} | ||
|
||
@Override | ||
public File[] getFiles() { | ||
return files.toArray(new File[files.size()]); | ||
} | ||
|
||
@Override | ||
public boolean removeFile(File file) { | ||
return files.remove(file); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
design-patterns-2/java/app/src/main/java/org/patterns/patterns/composite/File.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package org.patterns.patterns.composite; | ||
|
||
/** | ||
* The component base class for composite pattern | ||
* defines operations applicable both leaf & composite | ||
*/ | ||
public abstract class File { | ||
|
||
private String name; | ||
private int intend; | ||
|
||
public File(String name) { | ||
this.name = name; | ||
this.intend = 0; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
protected String getIntendString(){ | ||
String intendStr = ""; | ||
|
||
for (int i = 0; i < intend; i++) { | ||
intendStr += " "; | ||
} | ||
return intendStr; | ||
} | ||
protected void addIntend(int amount){ | ||
this.intend += amount; | ||
} | ||
protected int getIntend(){ | ||
return this.intend; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object object){ | ||
if(object == this) | ||
return true; | ||
|
||
if(object == null) | ||
return false; | ||
|
||
if(object.getClass() != this.getClass()) | ||
return false; | ||
|
||
File parsedObject = (File) object; | ||
|
||
return parsedObject.getName() == this.getName(); | ||
} | ||
|
||
public abstract void ls(); | ||
|
||
public abstract void addFile(File file); | ||
|
||
public abstract File[] getFiles(); | ||
|
||
public abstract boolean removeFile(File file); | ||
} |
28 changes: 28 additions & 0 deletions
28
design-patterns-2/java/app/src/main/java/org/patterns/patterns/composite/Runner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.patterns.patterns.composite; | ||
|
||
import org.patterns.patterns.IRunner; | ||
|
||
public class Runner implements IRunner{ | ||
@Override | ||
public void run(int exampleNum) { | ||
Directory myDir1 = new Directory("myDir1"); | ||
|
||
File myFile1 = new BinaryFile("myFile1"); | ||
File myFile2 = new BinaryFile("myFile2"); | ||
File myFile3 = new BinaryFile("myFile3"); | ||
|
||
myDir1.addFile(myFile1); | ||
myDir1.addFile(myFile2); | ||
myDir1.addFile(myFile3); | ||
|
||
Directory myDir2 = new Directory("myDir2"); | ||
File myFile4 = new BinaryFile("myFile4"); | ||
File myFile5 = new BinaryFile("myFile5"); | ||
myDir2.addFile(myFile4); | ||
myDir2.addFile(myFile5); | ||
|
||
myDir1.addFile(myDir2); | ||
|
||
myDir1.ls(); | ||
} | ||
} |