diff --git a/src/main/java/org/wahlzeit/model/AbstractCoordinate.java b/src/main/java/org/wahlzeit/model/AbstractCoordinate.java index 480b3a1ac..c199e33fd 100644 --- a/src/main/java/org/wahlzeit/model/AbstractCoordinate.java +++ b/src/main/java/org/wahlzeit/model/AbstractCoordinate.java @@ -5,6 +5,8 @@ package org.wahlzeit.model; +import org.wahlzeit.utils.DesignPattern; + public abstract class AbstractCoordinate implements Coordinate { /** @@ -95,6 +97,10 @@ private boolean isWithinDistance(Coordinate c, double epsilon) { * @param c Coordinate Object to convert * @return SphericCoordinate representation of c Coordinate */ + @DesignPattern( + name = "Flyweight", + participants = { "Client" } + ) protected static SphericCoordinate fromCartesian(CartesianCoordinate c) { if (c == null) { throw new IllegalArgumentException("Can't convert null coordinate"); @@ -122,6 +128,10 @@ protected static SphericCoordinate fromCartesian(CartesianCoordinate c) { * @param c Coordinate Object to convert * @return CartesianCoordinate representation of c Coordinate */ + @DesignPattern( + name = "Flyweight", + participants = { "Client" } + ) protected static CartesianCoordinate fromSpheric(SphericCoordinate c) { if (c == null) { throw new IllegalArgumentException("Can't convert null coordinate"); diff --git a/src/main/java/org/wahlzeit/model/CartesianCoordinate.java b/src/main/java/org/wahlzeit/model/CartesianCoordinate.java index 2a7cbcc5d..f89e00d28 100644 --- a/src/main/java/org/wahlzeit/model/CartesianCoordinate.java +++ b/src/main/java/org/wahlzeit/model/CartesianCoordinate.java @@ -6,12 +6,18 @@ package org.wahlzeit.model; +import org.wahlzeit.utils.DesignPattern; + import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /* * Local plain class to store 3D Cartesian Coordinate values. */ +@DesignPattern( + name = "Flyweight", + participants = { "Flyweight" } +) public class CartesianCoordinate extends AbstractCoordinate { private final double x, y, z; @@ -67,7 +73,7 @@ public double getZ() { return z; } - private static Map coordinateInstances = new ConcurrentHashMap<>(); + /** * Part of the Value Type implementation (week8) * @param x @@ -75,21 +81,26 @@ public double getZ() { * @param z * @param location a location object to link the new coordinate to or null * @return a new cartesianCoordinate with coordinate @param x y z and location attribute values, - * or the stored object if one with the same attribute values already has been created. + * or the stored object in @value coordinateInstances if one with the same attribute values already has been saved. */ + private static Map coordinateInstances = new ConcurrentHashMap<>(); + @DesignPattern( + name = "Flyweight", + participants = { "FlyweightFactory" } + ) public static CartesianCoordinate getCoordinate(double x, double y, double z, Location location) { if (!assertNotNaN(x) || !assertNotNaN(y) || !assertNotNaN(z)) { throw new IllegalArgumentException("getCoordinate must receive exactly 3 non NaN doubles"); } // look up if object exists already - Integer coordianteHashCode = calcHashCode(x, y, z, location); + Integer coordinateHashCode = calcHashCode(x, y, z, location); - if (coordinateInstances.containsKey(coordianteHashCode)) { - return coordinateInstances.get(coordianteHashCode); + if (coordinateInstances.containsKey(coordinateHashCode)) { + return coordinateInstances.get(coordinateHashCode); } - // if immutable shared Coordiante object has not been created yet, create a new one and store it + // if immutable shared Coordinate object has not been created yet, create a new one and store it CartesianCoordinate coordinate; if (location == null) { @@ -98,7 +109,7 @@ public static CartesianCoordinate getCoordinate(double x, double y, double z, Lo coordinate = new CartesianCoordinate(x, y, z, location); } - coordinateInstances.put(coordianteHashCode, coordinate); + coordinateInstances.put(coordinateHashCode, coordinate); return coordinate; } diff --git a/src/main/java/org/wahlzeit/model/CubePhoto.java b/src/main/java/org/wahlzeit/model/CubePhoto.java index 95306776d..3624be51d 100644 --- a/src/main/java/org/wahlzeit/model/CubePhoto.java +++ b/src/main/java/org/wahlzeit/model/CubePhoto.java @@ -5,8 +5,14 @@ package org.wahlzeit.model; +import org.wahlzeit.utils.DesignPattern; + import java.sql.*; +@DesignPattern( + name = "AbstractFactory", + participants = { "Product" } +) public class CubePhoto extends Photo { public CubePhoto() { diff --git a/src/main/java/org/wahlzeit/model/CubePhotoFactory.java b/src/main/java/org/wahlzeit/model/CubePhotoFactory.java index c62226caf..8c1855374 100644 --- a/src/main/java/org/wahlzeit/model/CubePhotoFactory.java +++ b/src/main/java/org/wahlzeit/model/CubePhotoFactory.java @@ -6,9 +6,14 @@ package org.wahlzeit.model; import org.wahlzeit.services.SysLog; +import org.wahlzeit.utils.DesignPattern; import java.sql.*; +@DesignPattern( + name = "AbstractFactory", + participants = { "Factory" } +) public class CubePhotoFactory extends PhotoFactory { private static CubePhotoFactory instance = null; diff --git a/src/main/java/org/wahlzeit/model/CubePhotoManager.java b/src/main/java/org/wahlzeit/model/CubePhotoManager.java index acac9eb88..74309efdd 100644 --- a/src/main/java/org/wahlzeit/model/CubePhotoManager.java +++ b/src/main/java/org/wahlzeit/model/CubePhotoManager.java @@ -5,8 +5,14 @@ package org.wahlzeit.model; +import org.wahlzeit.utils.DesignPattern; + import java.sql.*; +@DesignPattern( + name = "Singleton", + participants = { "Singleton" } +) public class CubePhotoManager extends PhotoManager { protected CubePhoto createObject(ResultSet rset) throws SQLException { diff --git a/src/main/java/org/wahlzeit/model/Photo.java b/src/main/java/org/wahlzeit/model/Photo.java index 62cec621d..13bd91a8a 100644 --- a/src/main/java/org/wahlzeit/model/Photo.java +++ b/src/main/java/org/wahlzeit/model/Photo.java @@ -14,6 +14,10 @@ /** * A photo represents a user-provided (uploaded) photo. */ +@DesignPattern ( + name = "AbstractFactory", + participants = { "Product" } +) public class Photo extends DataObject { /** @@ -130,10 +134,11 @@ public Photo(ResultSet rset) throws SQLException { public String getIdAsString() { return String.valueOf(id.asInt()); } - - /** - * - */ + + @DesignPattern( + name = "Flyweight", + participants = { "Client" } + ) public void readFrom(ResultSet rset) throws SQLException { id = PhotoId.getIdFromInt(rset.getInt("id")); diff --git a/src/main/java/org/wahlzeit/model/PhotoCaseManager.java b/src/main/java/org/wahlzeit/model/PhotoCaseManager.java index bfaa1f858..a8dda26c5 100644 --- a/src/main/java/org/wahlzeit/model/PhotoCaseManager.java +++ b/src/main/java/org/wahlzeit/model/PhotoCaseManager.java @@ -9,10 +9,15 @@ import java.util.*; import org.wahlzeit.services.*; +import org.wahlzeit.utils.DesignPattern; /** * The photo case manager provides access to and manages persistent photo cases. */ +@DesignPattern( + name = "Singleton", + participants = { "Singleton" } +) public class PhotoCaseManager extends ObjectManager { /** diff --git a/src/main/java/org/wahlzeit/model/PhotoFactory.java b/src/main/java/org/wahlzeit/model/PhotoFactory.java index 4975f75b6..2abad0f8f 100644 --- a/src/main/java/org/wahlzeit/model/PhotoFactory.java +++ b/src/main/java/org/wahlzeit/model/PhotoFactory.java @@ -8,10 +8,15 @@ import java.sql.*; import org.wahlzeit.services.*; +import org.wahlzeit.utils.DesignPattern; /** - * An Abstract Factory for creating photos and related objects. + * Implementation of an Abstract Factory for creating photos and related objects. */ +@DesignPattern ( + name = "AbstractFactory", + participants = { "Factory" } +) public class PhotoFactory { /** diff --git a/src/main/java/org/wahlzeit/model/PhotoManager.java b/src/main/java/org/wahlzeit/model/PhotoManager.java index 61563b349..0ceaf215a 100644 --- a/src/main/java/org/wahlzeit/model/PhotoManager.java +++ b/src/main/java/org/wahlzeit/model/PhotoManager.java @@ -11,10 +11,15 @@ import org.wahlzeit.main.*; import org.wahlzeit.services.*; +import org.wahlzeit.utils.DesignPattern; /** * A photo manager provides access to and manages photos. */ +@DesignPattern( + name = "Singleton", + participants = { "Singleton" } +) public class PhotoManager extends ObjectManager { /** diff --git a/src/main/java/org/wahlzeit/model/SphericCoordinate.java b/src/main/java/org/wahlzeit/model/SphericCoordinate.java index e4e628290..e29f1feea 100644 --- a/src/main/java/org/wahlzeit/model/SphericCoordinate.java +++ b/src/main/java/org/wahlzeit/model/SphericCoordinate.java @@ -5,9 +5,15 @@ package org.wahlzeit.model; +import org.wahlzeit.utils.DesignPattern; + import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +@DesignPattern( + name = "Flyweight", + participants = { "Flyweight" } +) public class SphericCoordinate extends AbstractCoordinate { private final double phi, theta, radius; @@ -63,7 +69,7 @@ public double getRadius() { return radius; } - private static Map coordinateInstances = new ConcurrentHashMap<>(); + /** * Part of the Value Type implementation (week8) * @param phi @@ -71,21 +77,26 @@ public double getRadius() { * @param radius * @param location a location object to link the new coordinate to or null * @return a new sphericCoordinate with coordinate @param radius, theta, phi and location attribute values, - * or the stored object if one with the same attribute values already has been created. + * or the stored object in @value coordinateInstances if one with the same attribute values already has been saved. */ + private static Map coordinateInstances = new ConcurrentHashMap<>(); + @DesignPattern( + name = "Flyweight", + participants = { "FlyweightFactory" } + ) public static SphericCoordinate getCoordinate(double radius, double theta, double phi, Location location) { if (!assertNotNaN(phi) || !assertNotNaN(theta) || !assertNotNaN(radius)) { throw new IllegalArgumentException("getCoordinate must receive exactly 3 non NaN doubles"); } // look up if object exists already - Integer coordianteHashCode = calcHashCode(phi, theta, radius, location); + Integer coordinateHashCode = calcHashCode(phi, theta, radius, location); - if (coordinateInstances.containsKey(coordianteHashCode)) { - return coordinateInstances.get(coordianteHashCode); + if (coordinateInstances.containsKey(coordinateHashCode)) { + return coordinateInstances.get(coordinateHashCode); } - // if immutable shared Coordiante object has not been created yet, create a new one and store it + // if immutable shared Coordinate object has not been created yet, create a new one and store it SphericCoordinate coordinate; if (location == null) { @@ -94,7 +105,7 @@ public static SphericCoordinate getCoordinate(double radius, double theta, doubl coordinate = new SphericCoordinate(phi, theta, radius, location); } - coordinateInstances.put(coordianteHashCode, coordinate); + coordinateInstances.put(coordinateHashCode, coordinate); return coordinate; } diff --git a/src/main/java/org/wahlzeit/model/UserManager.java b/src/main/java/org/wahlzeit/model/UserManager.java index 11b28c3f3..24f4fd7cd 100644 --- a/src/main/java/org/wahlzeit/model/UserManager.java +++ b/src/main/java/org/wahlzeit/model/UserManager.java @@ -10,10 +10,15 @@ import org.wahlzeit.services.*; import org.wahlzeit.services.mailing.*; +import org.wahlzeit.utils.DesignPattern; /** * The UserManager provides access to and manages Users (including Moderators and Administrators). */ +@DesignPattern( + name = "Singleton", + participants = { "Singleton" } +) public class UserManager extends ObjectManager { /** diff --git a/src/main/java/org/wahlzeit/services/mailing/EmailService.java b/src/main/java/org/wahlzeit/services/mailing/EmailService.java index d74005103..413687b55 100644 --- a/src/main/java/org/wahlzeit/services/mailing/EmailService.java +++ b/src/main/java/org/wahlzeit/services/mailing/EmailService.java @@ -6,11 +6,16 @@ package org.wahlzeit.services.mailing; import org.wahlzeit.services.EmailAddress; +import org.wahlzeit.utils.DesignPattern; /** * An EmailServer can send emails. Various implementations exist. * */ +@DesignPattern( + name = "Decorator", + participants = { "Component" } +) public interface EmailService { /** diff --git a/src/main/java/org/wahlzeit/services/mailing/EmailServiceManager.java b/src/main/java/org/wahlzeit/services/mailing/EmailServiceManager.java index 364008493..c1944db4b 100644 --- a/src/main/java/org/wahlzeit/services/mailing/EmailServiceManager.java +++ b/src/main/java/org/wahlzeit/services/mailing/EmailServiceManager.java @@ -6,10 +6,15 @@ package org.wahlzeit.services.mailing; import org.wahlzeit.main.ServiceMain; +import org.wahlzeit.utils.DesignPattern; /** * */ +@DesignPattern( + name = "Singleton", + participants = { "Singleton" } +) public class EmailServiceManager { /** diff --git a/src/main/java/org/wahlzeit/services/mailing/LoggingEmailService.java b/src/main/java/org/wahlzeit/services/mailing/LoggingEmailService.java index ac4d752fa..a13612e7c 100644 --- a/src/main/java/org/wahlzeit/services/mailing/LoggingEmailService.java +++ b/src/main/java/org/wahlzeit/services/mailing/LoggingEmailService.java @@ -7,12 +7,17 @@ import org.wahlzeit.services.EmailAddress; import org.wahlzeit.services.SysLog; +import org.wahlzeit.utils.DesignPattern; /** * A logging mailing service logs email send attempts before sending emails. * This is a decorator pattern application. * */ +@DesignPattern( + name = "Decorator", + participants = { "Decorator" } +) public class LoggingEmailService implements EmailService { /** diff --git a/src/main/java/org/wahlzeit/utils/DesignPattern.java b/src/main/java/org/wahlzeit/utils/DesignPattern.java new file mode 100644 index 000000000..0eee27f48 --- /dev/null +++ b/src/main/java/org/wahlzeit/utils/DesignPattern.java @@ -0,0 +1,6 @@ +package org.wahlzeit.utils; + +public @interface DesignPattern { + String name(); + String[] participants(); +}