Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
Initial commit
  • Loading branch information
Omar El Gabry committed Jul 25, 2015
1 parent aa9aa87 commit 6ec3f36
Show file tree
Hide file tree
Showing 24 changed files with 567 additions and 0 deletions.
Binary file added diagrams/composite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/prototype.abstract.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added diagrams/prototype.interface.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/composite/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package composite;

public class Circle implements Shape{

@Override
public void draw() {
System.out.println("Circle");
}

}
88 changes: 88 additions & 0 deletions src/composite/ClassDiagram.ucls
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.8" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
associations="true" dependencies="false" nesting-relationships="true">
<class id="1" language="java" name="composite.Circle" project="JavaDesignPatterns"
file="/JavaDesignPatterns/src/composite/Circle.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="238" y="262"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<interface id="2" language="java" name="composite.Shape" project="JavaDesignPatterns"
file="/JavaDesignPatterns/src/composite/Shape.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="353" y="116"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<class id="3" language="java" name="composite.Line" project="JavaDesignPatterns"
file="/JavaDesignPatterns/src/composite/Line.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="365" y="262"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="4" language="java" name="composite.Main" project="JavaDesignPatterns"
file="/JavaDesignPatterns/src/composite/Main.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="97" y="128"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="5" language="java" name="composite.Picture" project="JavaDesignPatterns"
file="/JavaDesignPatterns/src/composite/Picture.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="639" y="146"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="6" language="java" name="composite.Rectangle" project="JavaDesignPatterns"
file="/JavaDesignPatterns/src/composite/Rectangle.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="500" y="263"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<association id="7">
<end type="SOURCE" refId="5" navigable="false">
<attribute id="8" name="shapes"/>
<multiplicity id="9" minimum="0" maximum="2147483647"/>
</end>
<end type="TARGET" refId="2" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<realization id="10">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="2"/>
</realization>
<realization id="11">
<end type="SOURCE" refId="1"/>
<end type="TARGET" refId="2"/>
</realization>
<realization id="12">
<end type="SOURCE" refId="5"/>
<end type="TARGET" refId="2"/>
</realization>
<realization id="13">
<end type="SOURCE" refId="6"/>
<end type="TARGET" refId="2"/>
</realization>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>
9 changes: 9 additions & 0 deletions src/composite/Line.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package composite;

public class Line implements Shape{

@Override
public void draw() {
System.out.println("Line");
}
}
26 changes: 26 additions & 0 deletions src/composite/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package composite;

public class Main {

public static void main(String[] args) {

// start drawing zoo picture
Picture zoo = new Picture("Zoo");

// add shapes(circle, rectangle, line, picture) to picture
zoo.add(new Circle());
zoo.add(new Rectangle());
zoo.add(new Line());

Picture lion = new Picture("Lion");

lion.add(new Circle());
lion.add(new Rectangle());

zoo.add(lion);

// draw zoo picture
zoo.draw();
}

}
32 changes: 32 additions & 0 deletions src/composite/Picture.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package composite;

import java.util.List;
import java.util.ArrayList;

public class Picture implements Shape{

private List<Shape> shapes = new ArrayList<Shape>();
private String name;

public Picture(String _name){
name = _name;
}

@Override
public void draw() {
System.out.println("Start Drawing Picture: " + name);
for(Shape shape: shapes){
shape.draw();
}
System.out.println("Finished Drawing Picture: " + name);
}

public void add(Shape shape) {
shapes.add(shape);
}

public void remove(Shape shape) {
shapes.remove(shape);
}

}
10 changes: 10 additions & 0 deletions src/composite/Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package composite;

public class Rectangle implements Shape{

@Override
public void draw() {
System.out.println("Rectangle");
}

}
6 changes: 6 additions & 0 deletions src/composite/Shape.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package composite;

public interface Shape {

public void draw();
}
9 changes: 9 additions & 0 deletions src/prototype/abstr/Album.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package prototype.abstr;

public class Album extends Product{

@Override
public String toString() {
return "Album";
}
}
9 changes: 9 additions & 0 deletions src/prototype/abstr/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package prototype.abstr;

public class Book extends Product{

@Override
public String toString() {
return "Book";
}
}
84 changes: 84 additions & 0 deletions src/prototype/abstr/ClassDiagram.ucls
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.8" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
associations="true" dependencies="false" nesting-relationships="true">
<class id="1" language="java" name="prototype.abstr.Product" project="JavaDesignPatterns"
file="/JavaDesignPatterns/src/prototype/abstr/Product.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="525" y="144"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="2" language="java" name="prototype.abstr.Book" project="JavaDesignPatterns"
file="/JavaDesignPatterns/src/prototype/abstr/Book.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="382" y="283"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="3" language="java" name="prototype.abstr.Album" project="JavaDesignPatterns"
file="/JavaDesignPatterns/src/prototype/abstr/Album.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="524" y="283"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="4" language="java" name="prototype.abstr.Main" project="JavaDesignPatterns"
file="/JavaDesignPatterns/src/prototype/abstr/Main.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="91" y="133"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="5" language="java" name="prototype.abstr.ProductFactory" project="JavaDesignPatterns"
file="/JavaDesignPatterns/src/prototype/abstr/ProductFactory.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="260" y="143"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="6" language="java" name="prototype.abstr.Movie" project="JavaDesignPatterns"
file="/JavaDesignPatterns/src/prototype/abstr/Movie.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="666" y="283"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<generalization id="7">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="1"/>
</generalization>
<association id="8">
<end type="SOURCE" refId="5" navigable="false">
<attribute id="9" name="products"/>
<multiplicity id="10" minimum="0" maximum="2147483647"/>
</end>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<generalization id="11">
<end type="SOURCE" refId="6"/>
<end type="TARGET" refId="1"/>
</generalization>
<generalization id="12">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="1"/>
</generalization>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>
24 changes: 24 additions & 0 deletions src/prototype/abstr/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package prototype.abstr;

public class Main {

public static void main(String[] args) {

// load objects
ProductFactory.loadCache();

// clone existing album object
Album album = (Album) ProductFactory.getProduct("Album");
System.out.println(album);

// clone existing book object
Book book = (Book) ProductFactory.getProduct("Book");
System.out.println(book);

// clone existing movie object
Movie movie = (Movie) ProductFactory.getProduct("Movie");
System.out.println(movie);

}

}
9 changes: 9 additions & 0 deletions src/prototype/abstr/Movie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package prototype.abstr;

public class Movie extends Product{

@Override
public String toString() {
return "Movie";
}
}
13 changes: 13 additions & 0 deletions src/prototype/abstr/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package prototype.abstr;

public abstract class Product implements Cloneable{

@Override
public Product clone() {
try {
return (Product) super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}
}
25 changes: 25 additions & 0 deletions src/prototype/abstr/ProductFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package prototype.abstr;

import java.util.HashMap;
import java.util.Map;

public class ProductFactory {

private static Map<String, Product> products = new HashMap<String, Product>();

public static Product getProduct(String product) {
Product cachedProduct = products.get(product);
return (Product) cachedProduct.clone();
}

public static void loadCache() {

Book book = new Book();
Album album = new Album();
Movie movie = new Movie();

products.put(book.toString(), book);
products.put(album.toString(), album);
products.put(movie.toString(), movie);
}
}
19 changes: 19 additions & 0 deletions src/prototype/inter/Album.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package prototype.inter;

public class Album implements Product{

@Override
public Album clone() {
try {
return (Album) super.clone();
} catch (CloneNotSupportedException e) {
return null;
}
}

@Override
public String toString() {
return "Album";
}

}
Loading

0 comments on commit 6ec3f36

Please sign in to comment.