Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.53 KB | None | 0 0
  1. class ClientA extends Client use TClientWeight
  2. class ClientB extends Client use TClientHeight
  3. class ClientC extends Client use TClientWeight, TClientHeight
  4.  
  5. internal class Program
  6. {
  7. private static void Main(string[] args)
  8. {
  9. var a = new ClientA("Adam", 68);
  10. var b = new ClientB("Bob", 1.75);
  11. var c = new ClientC("Cheryl", 54.4, 1.65);
  12.  
  13. Console.WriteLine("{0} is {1:0.0} lbs.", a.Name, a.WeightPounds());
  14. Console.WriteLine("{0} is {1:0.0} inches tall.", b.Name, b.HeightInches());
  15. Console.WriteLine("{0} is {1:0.0} lbs and {2:0.0} inches.", c.Name, c.WeightPounds(), c.HeightInches());
  16. Console.ReadLine();
  17. }
  18. }
  19.  
  20. public class Client
  21. {
  22. public string Name { get; set; }
  23.  
  24. public Client(string name)
  25. {
  26. Name = name;
  27. }
  28. }
  29.  
  30. public interface IWeight
  31. {
  32. double Weight { get; set; }
  33. }
  34.  
  35. public interface IHeight
  36. {
  37. double Height { get; set; }
  38. }
  39.  
  40. public class ClientA : Client, IWeight
  41. {
  42. public double Weight { get; set; }
  43. public ClientA(string name, double weight) : base(name)
  44. {
  45. Weight = weight;
  46. }
  47. }
  48.  
  49. public class ClientB : Client, IHeight
  50. {
  51. public double Height { get; set; }
  52. public ClientB(string name, double height) : base(name)
  53. {
  54. Height = height;
  55. }
  56. }
  57.  
  58. public class ClientC : Client, IWeight, IHeight
  59. {
  60. public double Weight { get; set; }
  61. public double Height { get; set; }
  62. public ClientC(string name, double weight, double height) : base(name)
  63. {
  64. Weight = weight;
  65. Height = height;
  66. }
  67. }
  68.  
  69. public static class ClientExt
  70. {
  71. public static double HeightInches(this IHeight client)
  72. {
  73. return client.Height * 39.3700787;
  74. }
  75.  
  76. public static double WeightPounds(this IWeight client)
  77. {
  78. return client.Weight * 2.20462262;
  79. }
  80. }
  81.  
  82. Adam is 149.9 lbs.
  83. Bob is 68.9 inches tall.
  84. Cheryl is 119.9 lbs and 65.0 inches.
  85.  
  86. /// <summary>
  87. /// This is our generic base class.
  88. /// </summary>
  89. public abstract class Model
  90. {
  91. /// <summary>
  92. /// A generic IO operation on models.
  93. /// </summary>
  94. public void Write(string value)
  95. {
  96. // blah blah
  97. }
  98. }
  99.  
  100. /// <summary>
  101. /// This is our trait class.
  102. /// </summary>
  103. public class Publish
  104. {
  105. private Model _owner;
  106.  
  107. public Publish(Model owner)
  108. {
  109. this._owner = owner;
  110. }
  111.  
  112. public void ChangeStatus(string status)
  113. {
  114. // write a new status to the model
  115. _owner.Write(status);
  116. }
  117. }
  118.  
  119. /// <summary>
  120. /// This is our document class with the trait.
  121. /// </summary>
  122. public class Document : Model
  123. {
  124. /// <summary>
  125. /// The contained trait.
  126. /// </summary>
  127. private Publish _publish;
  128.  
  129. /// <summary>
  130. /// Public read-only access.
  131. /// </summary>
  132. public Publish Publish
  133. {
  134. get { return _publish; }
  135. }
  136.  
  137. /// <summary>
  138. /// Constructor creates the trait
  139. /// </summary>
  140. public Document()
  141. {
  142. this._publish = new Publish(this);
  143. }
  144. }
  145.  
  146. Document doc = new Document();
  147. doc.Publish.ChangeStatus("review");
  148.  
  149. /// <summary>
  150. /// This is our generic base class.
  151. /// </summary>
  152. public abstract class Model
  153. {
  154. /// <summary>
  155. /// A generic IO operation on models.
  156. /// </summary>
  157. public void Write(string value)
  158. {
  159. // blah blah
  160. }
  161. }
  162.  
  163. /// <summary>
  164. /// This is our trait class.
  165. /// </summary>
  166. public class Publish traits Model
  167. {
  168. public void ChangeStatus(Model owner, string status)
  169. {
  170. // write a new status to the model
  171. owner.Write(status);
  172. }
  173. }
  174.  
  175. /// <summary>
  176. /// This is our document class with the trait.
  177. /// </summary>
  178. public class Document : Model uses Publish
  179. {
  180. }
  181.  
  182. Document doc = new Document();
  183. doc.ChangeStatus("review");
  184.  
  185. public class Client {
  186. public double Weight { get; }
  187.  
  188. public double Height { get; }
  189. }
  190.  
  191. public interface TClientWeight {
  192. double Weight { get; }
  193. }
  194.  
  195. public interface TClientHeight {
  196. double Height { get; }
  197. }
  198.  
  199. public class ClientA: Client, TClientWeight { }
  200.  
  201. public class ClientB: Client, TClientHeight { }
  202.  
  203. public class ClientC: Client, TClientWeight, TClientHeight { }
  204.  
  205. public static class TClientWeightMethods {
  206. public static bool IsHeavierThan(this TClientWeight client, double weight) {
  207. return client.Weight > weight;
  208. }
  209. // add more methods as you see fit
  210. }
  211.  
  212. public static class TClientHeightMethods {
  213. public static bool IsTallerThan(this TClientHeight client, double height) {
  214. return client.Height > height;
  215. }
  216. // add more methods as you see fit
  217. }
  218.  
  219. var c1 = new Class1();
  220. c1.IsHeavierThan(10); // OK
  221. c1.IsTallerThan(10); // compiler error
  222.  
  223. public interface IDynamicObject {
  224. bool TryGetAttribute(string key, out object value);
  225. void SetAttribute(string key, object value);
  226. // void RemoveAttribute(string key)
  227. }
  228.  
  229. public class DynamicObject: IDynamicObject {
  230. private readonly Dictionary<string, object> data = new Dictionary<string, object>(StringComparer.Ordinal);
  231.  
  232. bool IDynamicObject.TryGetAttribute(string key, out object value) {
  233. return data.TryGet(key, out value);
  234. }
  235.  
  236. void IDynamicObject.SetAttribute(string key, object value) {
  237. data[key] = value;
  238. }
  239. }
  240.  
  241. public class Client: DynamicObject { /* implementation see above */ }
  242.  
  243. public interface TClientWeight, IDynamicObject {
  244. double Weight { get; }
  245. }
  246.  
  247. public class ClientA: Client, TClientWeight { }
  248.  
  249. public static class TClientWeightMethods {
  250. public static bool HasWeightChanged(this TClientWeight client) {
  251. object oldWeight;
  252. bool result = client.TryGetAttribute("oldWeight", out oldWeight) && client.Weight.Equals(oldWeight);
  253. client.SetAttribute("oldWeight", client.Weight);
  254. return result;
  255. }
  256. // add more methods as you see fit
  257. }
  258.  
  259. using System.Windows;
  260.  
  261. public class Client : DependencyObject
  262. {
  263. public string Name { get; set; }
  264.  
  265. public Client(string name)
  266. {
  267. Name = name;
  268. }
  269.  
  270. //add to descendant to use
  271. //public double Weight
  272. //{
  273. // get { return (double)GetValue(WeightProperty); }
  274. // set { SetValue(WeightProperty, value); }
  275. //}
  276.  
  277. public static readonly DependencyProperty WeightProperty =
  278. DependencyProperty.Register("Weight", typeof(double), typeof(Client), new PropertyMetadata());
  279.  
  280.  
  281. //add to descendant to use
  282. //public double Height
  283. //{
  284. // get { return (double)GetValue(HeightProperty); }
  285. // set { SetValue(HeightProperty, value); }
  286. //}
  287.  
  288. public static readonly DependencyProperty HeightProperty =
  289. DependencyProperty.Register("Height", typeof(double), typeof(Client), new PropertyMetadata());
  290. }
  291.  
  292. public interface IWeight
  293. {
  294. double Weight { get; set; }
  295. }
  296.  
  297. public interface IHeight
  298. {
  299. double Height { get; set; }
  300. }
  301.  
  302. public class ClientA : Client, IWeight
  303. {
  304. public double Weight
  305. {
  306. get { return (double)GetValue(WeightProperty); }
  307. set { SetValue(WeightProperty, value); }
  308. }
  309.  
  310. public ClientA(string name, double weight)
  311. : base(name)
  312. {
  313. Weight = weight;
  314. }
  315. }
  316.  
  317. public class ClientB : Client, IHeight
  318. {
  319. public double Height
  320. {
  321. get { return (double)GetValue(HeightProperty); }
  322. set { SetValue(HeightProperty, value); }
  323. }
  324.  
  325. public ClientB(string name, double height)
  326. : base(name)
  327. {
  328. Height = height;
  329. }
  330. }
  331.  
  332. public class ClientC : Client, IHeight, IWeight
  333. {
  334. public double Height
  335. {
  336. get { return (double)GetValue(HeightProperty); }
  337. set { SetValue(HeightProperty, value); }
  338. }
  339.  
  340. public double Weight
  341. {
  342. get { return (double)GetValue(WeightProperty); }
  343. set { SetValue(WeightProperty, value); }
  344. }
  345.  
  346. public ClientC(string name, double weight, double height)
  347. : base(name)
  348. {
  349. Weight = weight;
  350. Height = height;
  351. }
  352.  
  353. }
  354.  
  355. public static class ClientExt
  356. {
  357. public static double HeightInches(this IHeight client)
  358. {
  359. return client.Height * 39.3700787;
  360. }
  361.  
  362. public static double WeightPounds(this IWeight client)
  363. {
  364. return client.Weight * 2.20462262;
  365. }
  366. }
  367.  
  368. public class RSwitchable : Role
  369. {
  370. private bool on = false;
  371. public void TurnOn() { on = true; }
  372. public void TurnOff() { on = false; }
  373. public bool IsOn { get { return on; } }
  374. public bool IsOff { get { return !on; } }
  375. }
  376.  
  377. public class RTunable : Role
  378. {
  379. public int Channel { get; private set; }
  380. public void Seek(int step) { Channel += step; }
  381. }
  382.  
  383. public class Radio : Does<RSwitchable>, Does<RTunable> { }
  384.  
  385. var radio = new Radio();
  386. radio.TurnOn();
  387. radio.Seek(42);
  388.  
  389. radio.As<RSwitchable>().TurnOn();
  390. radio.As<RTunable>().Seek(42);
  391.  
  392. interface TClientWeight {
  393. void foo {get; set;}
  394. }
  395.  
  396. class Client {
  397.  
  398. }
  399.  
  400. class ClientA : Client, TClientWeight {
  401. void foo {get; set;}
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement