Skip to content

Commit

Permalink
add composite in java in design-patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailDeriabin committed Sep 19, 2024
1 parent 96dda72 commit fa7e9e4
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.patterns;

import org.patterns.patterns.IRunner;
import org.patterns.patterns.decorator.*;
import org.patterns.patterns.composite.*;

public class App {
public static void main(String[] args) {
Expand Down
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'");
}
}
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);
}
}
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);
}
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();
}
}

0 comments on commit fa7e9e4

Please sign in to comment.