Guest User

Untitled

a guest
Jun 18th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. namespace ConsoleApplication4983
  2. {
  3. public class MyClass
  4. {
  5. static void Main()
  6. {
  7. var c = new MyClass();
  8. c.DoSomething(new Sequential());
  9. c.DoSomething(new Random());
  10. }
  11.  
  12. public void DoSomething(ProcessingMethod method)
  13. {
  14. if (method is Sequential)
  15. {
  16. // do something sequential
  17. }
  18. else if (method is Random)
  19. {
  20. // do something random
  21. }
  22. }
  23. }
  24.  
  25. public class ProcessingMethod {}
  26. public class Sequential : ProcessingMethod {}
  27. public class Random : ProcessingMethod {}
  28. }
  29.  
  30. using System;
  31.  
  32. namespace ConsoleApplication4983
  33. {
  34. public class MyClass
  35. {
  36. static void Main()
  37. {
  38. var c = new MyClass();
  39. c.DoSomething(new Sequential());
  40. c.DoSomething(new Random());
  41. }
  42.  
  43. public void DoSomething(ProcessingMethod method)
  44. {
  45. method.Foo();
  46. }
  47. }
  48.  
  49. public class ProcessingMethod
  50. {
  51. public virtual void Foo() { }
  52. }
  53. public class Sequential : ProcessingMethod
  54. {
  55. public override void Foo() { }
  56. }
  57. public class Random : ProcessingMethod
  58. {
  59. public override void Foo() { }
  60. }
  61. }
  62.  
  63. public interface IProcessingMethod
  64. {
  65. void Process();
  66. }
  67.  
  68. public class SequentialProcess : IProcessingMethod
  69. {
  70. public void Process( IProcessable obj )
  71. {
  72. do something sequentially with the obj
  73. }
  74. }
  75.  
  76. public class ParallelProcess : IProcessingMethod
  77. {
  78. public void Process( IProcessable obj )
  79. {
  80. do something in parallel with the obj
  81. }
  82. }
  83.  
  84. public interface IProcessable
  85. {
  86. void Process( IProcessingMethod method );
  87. }
  88.  
  89. public class MyClass : IProcessable
  90. {
  91. public void Process( IProcessingMethod method )
  92. {
  93. method.Process( this );
  94. }
  95. }
  96.  
  97. ...
  98.  
  99. var obj = new MyClass();
  100. obj.Process( new SequentialProcess() );
  101.  
  102. public void DoSomething(ProcessingMethod method) {
  103. method.DoSomething();
  104. }
  105.  
  106. public void DoSomething(ProcessingMethod method)
  107. {
  108. method.Process();
  109. }
  110.  
  111. public abstract class ProcessingMethod
  112. {
  113. public abstract void Process();
  114. }
  115.  
  116. public class Sequental : ProcessingMethod
  117. {
  118. public override void Process()
  119. {
  120. // do something sequential
  121. }
  122. }
  123.  
  124. public class Random : ProcessingMethod
  125. {
  126. public override void Process()
  127. {
  128. // do something random
  129. }
  130. }
  131.  
  132. public class Parallel : ProcessingMethod{}
  133.  
  134. namespace ConsoleApplication4983
  135. {
  136. public class MyClass
  137. {
  138. static void Main()
  139. {
  140. var c = new MyClass();
  141. c.DoSomething(ProcessingMethod.Sequential);
  142. c.DoSomething(ProcessingMethod.Random);
  143. }
  144.  
  145. public void DoSomething(ProcessingMethod method)
  146. {
  147. if (method == ProcessingMethod.Sequential)
  148. {
  149. // do something sequential
  150. }
  151. else if (method == ProcessingMethod.Random)
  152. {
  153. // do something random
  154. }
  155. }
  156. }
  157.  
  158. public enum ProcessingMethod
  159. {
  160. Sequential,
  161. Random
  162. }
  163. }
Add Comment
Please, Sign In to add comment