-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit
- Loading branch information
Omar El Gabry
committed
Jul 25, 2015
1 parent
aa9aa87
commit 6ec3f36
Showing
24 changed files
with
567 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,10 @@ | ||
package composite; | ||
|
||
public class Circle implements Shape{ | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Circle"); | ||
} | ||
|
||
} |
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,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> |
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,9 @@ | ||
package composite; | ||
|
||
public class Line implements Shape{ | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Line"); | ||
} | ||
} |
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,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(); | ||
} | ||
|
||
} |
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 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); | ||
} | ||
|
||
} |
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,10 @@ | ||
package composite; | ||
|
||
public class Rectangle implements Shape{ | ||
|
||
@Override | ||
public void draw() { | ||
System.out.println("Rectangle"); | ||
} | ||
|
||
} |
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,6 @@ | ||
package composite; | ||
|
||
public interface Shape { | ||
|
||
public void draw(); | ||
} |
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,9 @@ | ||
package prototype.abstr; | ||
|
||
public class Album extends Product{ | ||
|
||
@Override | ||
public String toString() { | ||
return "Album"; | ||
} | ||
} |
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,9 @@ | ||
package prototype.abstr; | ||
|
||
public class Book extends Product{ | ||
|
||
@Override | ||
public String toString() { | ||
return "Book"; | ||
} | ||
} |
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,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> |
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,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); | ||
|
||
} | ||
|
||
} |
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,9 @@ | ||
package prototype.abstr; | ||
|
||
public class Movie extends Product{ | ||
|
||
@Override | ||
public String toString() { | ||
return "Movie"; | ||
} | ||
} |
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,13 @@ | ||
package prototype.abstr; | ||
|
||
public abstract class Product implements Cloneable{ | ||
|
||
@Override | ||
public Product clone() { | ||
try { | ||
return (Product) super.clone(); | ||
} catch (CloneNotSupportedException e) { | ||
return null; | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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"; | ||
} | ||
|
||
} |
Oops, something went wrong.