Skip to content

Commit

Permalink
Added comments for design patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
leonopulos committed Jan 10, 2022
1 parent a26f6d6 commit ae4f1c8
Show file tree
Hide file tree
Showing 15 changed files with 114 additions and 19 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/wahlzeit/model/AbstractCoordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package org.wahlzeit.model;

import org.wahlzeit.utils.DesignPattern;

public abstract class AbstractCoordinate implements Coordinate {

/**
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down
25 changes: 18 additions & 7 deletions src/main/java/org/wahlzeit/model/CartesianCoordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -67,29 +73,34 @@ public double getZ() {
return z;
}

private static Map<Integer, CartesianCoordinate> coordinateInstances = new ConcurrentHashMap<>();

/**
* Part of the Value Type implementation (week8)
* @param x
* @param y
* @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<Integer, CartesianCoordinate> 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) {
Expand All @@ -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;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/wahlzeit/model/CubePhoto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/wahlzeit/model/CubePhotoFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/wahlzeit/model/CubePhotoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/org/wahlzeit/model/Photo.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
/**
* A photo represents a user-provided (uploaded) photo.
*/
@DesignPattern (
name = "AbstractFactory",
participants = { "Product" }
)
public class Photo extends DataObject {

/**
Expand Down Expand Up @@ -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"));

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/wahlzeit/model/PhotoCaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/wahlzeit/model/PhotoFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/wahlzeit/model/PhotoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
25 changes: 18 additions & 7 deletions src/main/java/org/wahlzeit/model/SphericCoordinate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -63,29 +69,34 @@ public double getRadius() {
return radius;
}

private static Map<Integer, SphericCoordinate> coordinateInstances = new ConcurrentHashMap<>();

/**
* Part of the Value Type implementation (week8)
* @param phi
* @param theta
* @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<Integer, SphericCoordinate> 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) {
Expand All @@ -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;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/wahlzeit/model/UserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/wahlzeit/services/mailing/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/wahlzeit/utils/DesignPattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.wahlzeit.utils;

public @interface DesignPattern {
String name();
String[] participants();
}

0 comments on commit ae4f1c8

Please sign in to comment.