fork download
  1. using System;
  2. using System.IO;
  3.  
  4. public class Program
  5. {
  6. static void Main(string[] args)
  7. {
  8. MemoryStream ms = new MemoryStream();
  9. ms.WriteByte(100);
  10. ms.WriteByte(100);
  11. ms.WriteByte(0);
  12. ms.WriteByte(200);
  13. ms.WriteByte(200);
  14. ms.WriteByte(1);
  15. ms.WriteByte(50);
  16.  
  17. ms.Seek(0, SeekOrigin.Begin);
  18. Console.WriteLine(Factory.FromStream(ms));
  19. Console.WriteLine(Factory.FromStream(ms));
  20. }
  21. }
  22.  
  23. public struct Point
  24. {
  25. public int X;
  26. public int Y;
  27.  
  28. public override string ToString()
  29. {
  30. return string.Format("<Point(X={0}, Y={1})>", X, Y);
  31. }
  32. }
  33.  
  34. public class Shape
  35. {
  36. protected Point origin;
  37.  
  38. public Shape(int x, int y)
  39. {
  40. origin.X = x;
  41. origin.Y = y;
  42. }
  43.  
  44. public override string ToString()
  45. {
  46. return string.Format("<Shape(origin={0})>", origin);
  47. }
  48. }
  49.  
  50. public class Circle : Shape
  51. {
  52. private double r;
  53.  
  54. public Circle(int x, int y , int r) : base(x, y)
  55. {
  56. this.r = r;
  57. }
  58.  
  59. public override string ToString()
  60. {
  61. return string.Format("<Circle(origin={0}, r={1})>", origin, r);
  62. }
  63. }
  64.  
  65. public class Factory
  66. {
  67. public static Shape FromStream(Stream stream)
  68. {
  69. int x = stream.ReadByte();
  70. int y = stream.ReadByte();
  71. switch (stream.ReadByte())
  72. {
  73. case 0:
  74. return new Shape(x, y);
  75. case 1:
  76. return new Circle(x, y, stream.ReadByte());
  77. default:
  78. throw new Exception();
  79. }
  80. }
  81. }
  82.  
Success #stdin #stdout 0.01s 29672KB
stdin
Standard input is empty
stdout
<Shape(origin=<Point(X=100, Y=100)>)>
<Circle(origin=<Point(X=200, Y=200)>, r=50)>