RESTful implementation of factory method design pattern
up vote
1
down vote
favorite
How would you design a RESTful API for a class that uses the factory method pattern to create objects?
Let's say you have class Passport:
public class Passport {
final String firstName;
final String lastName;
final Date birthDate;
State state;
private Passport(State state, String firstName, String lastName, Date birthDate) {
this.state = state;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
}
public static Passport createPreliminary(String firstName, String lastName, Date birthDate) {
return new Passport(PRELIMINARY, firstName, lastName, birthDate);
}
public static Passport createRegular(String firstName, String lastName, Date birthDate) {
return new Passport(REGULAR, firstName, lastName, birthDate);
}
public void invalidate() {
state = INVALID;
}
}
public enum State {
PRELIMINARY,
REGULAR,
EXPIRED,
INVALID
}
A Passport instance can be created in two different states. After creation only the state property can be changed in a restricted way via state transition methods like invalidate().
What would be the RESTful way to create Passport resources? A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state? Or two different URLs for creating passport resources, one for "regular" and one for "preliminary" passports?
rest design-patterns
add a comment |
up vote
1
down vote
favorite
How would you design a RESTful API for a class that uses the factory method pattern to create objects?
Let's say you have class Passport:
public class Passport {
final String firstName;
final String lastName;
final Date birthDate;
State state;
private Passport(State state, String firstName, String lastName, Date birthDate) {
this.state = state;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
}
public static Passport createPreliminary(String firstName, String lastName, Date birthDate) {
return new Passport(PRELIMINARY, firstName, lastName, birthDate);
}
public static Passport createRegular(String firstName, String lastName, Date birthDate) {
return new Passport(REGULAR, firstName, lastName, birthDate);
}
public void invalidate() {
state = INVALID;
}
}
public enum State {
PRELIMINARY,
REGULAR,
EXPIRED,
INVALID
}
A Passport instance can be created in two different states. After creation only the state property can be changed in a restricted way via state transition methods like invalidate().
What would be the RESTful way to create Passport resources? A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state? Or two different URLs for creating passport resources, one for "regular" and one for "preliminary" passports?
rest design-patterns
How would you achive your needs on the Web? You'd ask the server for a resource that teaches you how a request has to be build. The server would respond with a form that offers a client a skeletton what fields and what types are necessary and also provides a convenient submit button to actually perform the request. Whether you create aREGULAR
or aPRELIMINARY
passport will be determined by probably a dropdown choice. The same can be applied for any REST application as well. Either reuse HTML forms or specify your own general-purpose media-types in that particular case
– Roman Vottner
Nov 19 at 18:17
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
How would you design a RESTful API for a class that uses the factory method pattern to create objects?
Let's say you have class Passport:
public class Passport {
final String firstName;
final String lastName;
final Date birthDate;
State state;
private Passport(State state, String firstName, String lastName, Date birthDate) {
this.state = state;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
}
public static Passport createPreliminary(String firstName, String lastName, Date birthDate) {
return new Passport(PRELIMINARY, firstName, lastName, birthDate);
}
public static Passport createRegular(String firstName, String lastName, Date birthDate) {
return new Passport(REGULAR, firstName, lastName, birthDate);
}
public void invalidate() {
state = INVALID;
}
}
public enum State {
PRELIMINARY,
REGULAR,
EXPIRED,
INVALID
}
A Passport instance can be created in two different states. After creation only the state property can be changed in a restricted way via state transition methods like invalidate().
What would be the RESTful way to create Passport resources? A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state? Or two different URLs for creating passport resources, one for "regular" and one for "preliminary" passports?
rest design-patterns
How would you design a RESTful API for a class that uses the factory method pattern to create objects?
Let's say you have class Passport:
public class Passport {
final String firstName;
final String lastName;
final Date birthDate;
State state;
private Passport(State state, String firstName, String lastName, Date birthDate) {
this.state = state;
this.firstName = firstName;
this.lastName = lastName;
this.birthDate = birthDate;
}
public static Passport createPreliminary(String firstName, String lastName, Date birthDate) {
return new Passport(PRELIMINARY, firstName, lastName, birthDate);
}
public static Passport createRegular(String firstName, String lastName, Date birthDate) {
return new Passport(REGULAR, firstName, lastName, birthDate);
}
public void invalidate() {
state = INVALID;
}
}
public enum State {
PRELIMINARY,
REGULAR,
EXPIRED,
INVALID
}
A Passport instance can be created in two different states. After creation only the state property can be changed in a restricted way via state transition methods like invalidate().
What would be the RESTful way to create Passport resources? A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state? Or two different URLs for creating passport resources, one for "regular" and one for "preliminary" passports?
rest design-patterns
rest design-patterns
asked Nov 19 at 9:50
Christof Schablinski
83
83
How would you achive your needs on the Web? You'd ask the server for a resource that teaches you how a request has to be build. The server would respond with a form that offers a client a skeletton what fields and what types are necessary and also provides a convenient submit button to actually perform the request. Whether you create aREGULAR
or aPRELIMINARY
passport will be determined by probably a dropdown choice. The same can be applied for any REST application as well. Either reuse HTML forms or specify your own general-purpose media-types in that particular case
– Roman Vottner
Nov 19 at 18:17
add a comment |
How would you achive your needs on the Web? You'd ask the server for a resource that teaches you how a request has to be build. The server would respond with a form that offers a client a skeletton what fields and what types are necessary and also provides a convenient submit button to actually perform the request. Whether you create aREGULAR
or aPRELIMINARY
passport will be determined by probably a dropdown choice. The same can be applied for any REST application as well. Either reuse HTML forms or specify your own general-purpose media-types in that particular case
– Roman Vottner
Nov 19 at 18:17
How would you achive your needs on the Web? You'd ask the server for a resource that teaches you how a request has to be build. The server would respond with a form that offers a client a skeletton what fields and what types are necessary and also provides a convenient submit button to actually perform the request. Whether you create a
REGULAR
or a PRELIMINARY
passport will be determined by probably a dropdown choice. The same can be applied for any REST application as well. Either reuse HTML forms or specify your own general-purpose media-types in that particular case– Roman Vottner
Nov 19 at 18:17
How would you achive your needs on the Web? You'd ask the server for a resource that teaches you how a request has to be build. The server would respond with a form that offers a client a skeletton what fields and what types are necessary and also provides a convenient submit button to actually perform the request. Whether you create a
REGULAR
or a PRELIMINARY
passport will be determined by probably a dropdown choice. The same can be applied for any REST application as well. Either reuse HTML forms or specify your own general-purpose media-types in that particular case– Roman Vottner
Nov 19 at 18:17
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state?
This is the option I'd go for. Having one endpoint for adding to the passports resource is the simplest and the easiest to understand.
If you used the other option, every time you added/changed an item to the "State" enum, you'd have to also change your resource structure. It just introduces unnecessary complexity and causes your API design to be less flexible.
Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to.
+1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
– Eric Stein
Nov 19 at 14:20
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state?
This is the option I'd go for. Having one endpoint for adding to the passports resource is the simplest and the easiest to understand.
If you used the other option, every time you added/changed an item to the "State" enum, you'd have to also change your resource structure. It just introduces unnecessary complexity and causes your API design to be less flexible.
Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to.
+1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
– Eric Stein
Nov 19 at 14:20
add a comment |
up vote
1
down vote
accepted
A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state?
This is the option I'd go for. Having one endpoint for adding to the passports resource is the simplest and the easiest to understand.
If you used the other option, every time you added/changed an item to the "State" enum, you'd have to also change your resource structure. It just introduces unnecessary complexity and causes your API design to be less flexible.
Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to.
+1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
– Eric Stein
Nov 19 at 14:20
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state?
This is the option I'd go for. Having one endpoint for adding to the passports resource is the simplest and the easiest to understand.
If you used the other option, every time you added/changed an item to the "State" enum, you'd have to also change your resource structure. It just introduces unnecessary complexity and causes your API design to be less flexible.
Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to.
A POST to /passports including the state property and check on the server-side that the state is either REGULAR or PRELIMINARY and return a BAD REQUEST response if it is an illegal state?
This is the option I'd go for. Having one endpoint for adding to the passports resource is the simplest and the easiest to understand.
If you used the other option, every time you added/changed an item to the "State" enum, you'd have to also change your resource structure. It just introduces unnecessary complexity and causes your API design to be less flexible.
Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to.
edited Nov 19 at 12:34
answered Nov 19 at 12:15
J Marlow
1055
1055
+1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
– Eric Stein
Nov 19 at 14:20
add a comment |
+1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
– Eric Stein
Nov 19 at 14:20
+1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
– Eric Stein
Nov 19 at 14:20
+1 for "Your API should be an abstraction of your internal business logic. Don't make it complex and layered if you don't have to."
– Eric Stein
Nov 19 at 14:20
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53372025%2frestful-implementation-of-factory-method-design-pattern%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
How would you achive your needs on the Web? You'd ask the server for a resource that teaches you how a request has to be build. The server would respond with a form that offers a client a skeletton what fields and what types are necessary and also provides a convenient submit button to actually perform the request. Whether you create a
REGULAR
or aPRELIMINARY
passport will be determined by probably a dropdown choice. The same can be applied for any REST application as well. Either reuse HTML forms or specify your own general-purpose media-types in that particular case– Roman Vottner
Nov 19 at 18:17