code_junkie

Design of inheritance for Validate interfaces

Nov 11th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. interface Validatable {
  2. public void validate() throws ValidateException;
  3. }
  4. interface Object1Validatable {
  5. public void validate(Object1Converse converse) throws ValidateException;
  6. }
  7. class Object1 implements Object1Validatable {
  8. ...
  9. public void validate() throws ValidateException {
  10. throw new UnsupportedOperationException();
  11. }
  12. }
  13. class Object2 implements Validatable {
  14. ...
  15. public void validate() throws ValidateException {
  16. ...
  17. }
  18. }
  19.  
  20. interface Object1Validatable {
  21. public void validate(Object1Converse converse) throws ValidateException;
  22. }
  23. class Object1 implements Object1Validatable {
  24. ...
  25. public void validate(Object1Converse converse) throws ValidateException {
  26. ...
  27. }
  28. }
  29. interface Object2Validatable {
  30. public void validate() throws ValidateException;
  31. }
  32. class Object2 implements Object2Validatable {
  33. ...
  34. public void validate() throws ValidateException {
  35. ...
  36. }
  37. }
  38.  
  39. interface Validatable {
  40. void validate(Validator v);
  41. }
  42.  
  43. class Object1 implements Validatable{
  44. void validate(Validator v){
  45. v.foo
  46. v.bar
  47. }
  48. }
  49. class Object1Converse implements Validator{
  50. //....
  51. }
  52. class Object2 implements Validatable{
  53. void validate(Validator v){
  54. //do whatever you need and ingore validator ?
  55. }
  56. }
  57.  
  58. public class MyClass {
  59. //Properties and methods here
  60. }
  61.  
  62. public class MyClassValidator : IValidator<MyClass> {
  63. IList<IValidatorError> IValidator.Validate(MyClass obj) {
  64. //Perform some checks here
  65. }
  66. }
  67.  
  68. //...
  69.  
  70. public void RegisterValidators() {
  71. Validators.Add<MyClassValidator>();
  72. }
  73.  
  74. //...
  75.  
  76. public void PerformSomeLogic() {
  77. var myobj = new MyClass { };
  78. //Set some properties, call some methods, etc.
  79. var v = Validators.Get<MyClass>();
  80. if(v.GetErrors(myobj).Count() > 0)
  81. throw new Exception();
  82. SaveToDatabase(myobj);
  83. }
  84.  
  85. interface Validateable
  86. {
  87. }
  88.  
  89. interface EmptyValidateable inherits Validateable //Or is it implements?
  90. {
  91. void validate() throws ValidateException;
  92. }
  93.  
  94. interface Objectvalidateable inherits Validateable
  95. {
  96. void validate(Object validateObj);
  97. }
Add Comment
Please, Sign In to add comment