Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ClientA extends Client use TClientWeight
- class ClientB extends Client use TClientHeight
- class ClientC extends Client use TClientWeight, TClientHeight
- internal class Program
- {
- private static void Main(string[] args)
- {
- var a = new ClientA("Adam", 68);
- var b = new ClientB("Bob", 1.75);
- var c = new ClientC("Cheryl", 54.4, 1.65);
- Console.WriteLine("{0} is {1:0.0} lbs.", a.Name, a.WeightPounds());
- Console.WriteLine("{0} is {1:0.0} inches tall.", b.Name, b.HeightInches());
- Console.WriteLine("{0} is {1:0.0} lbs and {2:0.0} inches.", c.Name, c.WeightPounds(), c.HeightInches());
- Console.ReadLine();
- }
- }
- public class Client
- {
- public string Name { get; set; }
- public Client(string name)
- {
- Name = name;
- }
- }
- public interface IWeight
- {
- double Weight { get; set; }
- }
- public interface IHeight
- {
- double Height { get; set; }
- }
- public class ClientA : Client, IWeight
- {
- public double Weight { get; set; }
- public ClientA(string name, double weight) : base(name)
- {
- Weight = weight;
- }
- }
- public class ClientB : Client, IHeight
- {
- public double Height { get; set; }
- public ClientB(string name, double height) : base(name)
- {
- Height = height;
- }
- }
- public class ClientC : Client, IWeight, IHeight
- {
- public double Weight { get; set; }
- public double Height { get; set; }
- public ClientC(string name, double weight, double height) : base(name)
- {
- Weight = weight;
- Height = height;
- }
- }
- public static class ClientExt
- {
- public static double HeightInches(this IHeight client)
- {
- return client.Height * 39.3700787;
- }
- public static double WeightPounds(this IWeight client)
- {
- return client.Weight * 2.20462262;
- }
- }
- Adam is 149.9 lbs.
- Bob is 68.9 inches tall.
- Cheryl is 119.9 lbs and 65.0 inches.
- /// <summary>
- /// This is our generic base class.
- /// </summary>
- public abstract class Model
- {
- /// <summary>
- /// A generic IO operation on models.
- /// </summary>
- public void Write(string value)
- {
- // blah blah
- }
- }
- /// <summary>
- /// This is our trait class.
- /// </summary>
- public class Publish
- {
- private Model _owner;
- public Publish(Model owner)
- {
- this._owner = owner;
- }
- public void ChangeStatus(string status)
- {
- // write a new status to the model
- _owner.Write(status);
- }
- }
- /// <summary>
- /// This is our document class with the trait.
- /// </summary>
- public class Document : Model
- {
- /// <summary>
- /// The contained trait.
- /// </summary>
- private Publish _publish;
- /// <summary>
- /// Public read-only access.
- /// </summary>
- public Publish Publish
- {
- get { return _publish; }
- }
- /// <summary>
- /// Constructor creates the trait
- /// </summary>
- public Document()
- {
- this._publish = new Publish(this);
- }
- }
- Document doc = new Document();
- doc.Publish.ChangeStatus("review");
- /// <summary>
- /// This is our generic base class.
- /// </summary>
- public abstract class Model
- {
- /// <summary>
- /// A generic IO operation on models.
- /// </summary>
- public void Write(string value)
- {
- // blah blah
- }
- }
- /// <summary>
- /// This is our trait class.
- /// </summary>
- public class Publish traits Model
- {
- public void ChangeStatus(Model owner, string status)
- {
- // write a new status to the model
- owner.Write(status);
- }
- }
- /// <summary>
- /// This is our document class with the trait.
- /// </summary>
- public class Document : Model uses Publish
- {
- }
- Document doc = new Document();
- doc.ChangeStatus("review");
- public class Client {
- public double Weight { get; }
- public double Height { get; }
- }
- public interface TClientWeight {
- double Weight { get; }
- }
- public interface TClientHeight {
- double Height { get; }
- }
- public class ClientA: Client, TClientWeight { }
- public class ClientB: Client, TClientHeight { }
- public class ClientC: Client, TClientWeight, TClientHeight { }
- public static class TClientWeightMethods {
- public static bool IsHeavierThan(this TClientWeight client, double weight) {
- return client.Weight > weight;
- }
- // add more methods as you see fit
- }
- public static class TClientHeightMethods {
- public static bool IsTallerThan(this TClientHeight client, double height) {
- return client.Height > height;
- }
- // add more methods as you see fit
- }
- var c1 = new Class1();
- c1.IsHeavierThan(10); // OK
- c1.IsTallerThan(10); // compiler error
- public interface IDynamicObject {
- bool TryGetAttribute(string key, out object value);
- void SetAttribute(string key, object value);
- // void RemoveAttribute(string key)
- }
- public class DynamicObject: IDynamicObject {
- private readonly Dictionary<string, object> data = new Dictionary<string, object>(StringComparer.Ordinal);
- bool IDynamicObject.TryGetAttribute(string key, out object value) {
- return data.TryGet(key, out value);
- }
- void IDynamicObject.SetAttribute(string key, object value) {
- data[key] = value;
- }
- }
- public class Client: DynamicObject { /* implementation see above */ }
- public interface TClientWeight, IDynamicObject {
- double Weight { get; }
- }
- public class ClientA: Client, TClientWeight { }
- public static class TClientWeightMethods {
- public static bool HasWeightChanged(this TClientWeight client) {
- object oldWeight;
- bool result = client.TryGetAttribute("oldWeight", out oldWeight) && client.Weight.Equals(oldWeight);
- client.SetAttribute("oldWeight", client.Weight);
- return result;
- }
- // add more methods as you see fit
- }
- using System.Windows;
- public class Client : DependencyObject
- {
- public string Name { get; set; }
- public Client(string name)
- {
- Name = name;
- }
- //add to descendant to use
- //public double Weight
- //{
- // get { return (double)GetValue(WeightProperty); }
- // set { SetValue(WeightProperty, value); }
- //}
- public static readonly DependencyProperty WeightProperty =
- DependencyProperty.Register("Weight", typeof(double), typeof(Client), new PropertyMetadata());
- //add to descendant to use
- //public double Height
- //{
- // get { return (double)GetValue(HeightProperty); }
- // set { SetValue(HeightProperty, value); }
- //}
- public static readonly DependencyProperty HeightProperty =
- DependencyProperty.Register("Height", typeof(double), typeof(Client), new PropertyMetadata());
- }
- public interface IWeight
- {
- double Weight { get; set; }
- }
- public interface IHeight
- {
- double Height { get; set; }
- }
- public class ClientA : Client, IWeight
- {
- public double Weight
- {
- get { return (double)GetValue(WeightProperty); }
- set { SetValue(WeightProperty, value); }
- }
- public ClientA(string name, double weight)
- : base(name)
- {
- Weight = weight;
- }
- }
- public class ClientB : Client, IHeight
- {
- public double Height
- {
- get { return (double)GetValue(HeightProperty); }
- set { SetValue(HeightProperty, value); }
- }
- public ClientB(string name, double height)
- : base(name)
- {
- Height = height;
- }
- }
- public class ClientC : Client, IHeight, IWeight
- {
- public double Height
- {
- get { return (double)GetValue(HeightProperty); }
- set { SetValue(HeightProperty, value); }
- }
- public double Weight
- {
- get { return (double)GetValue(WeightProperty); }
- set { SetValue(WeightProperty, value); }
- }
- public ClientC(string name, double weight, double height)
- : base(name)
- {
- Weight = weight;
- Height = height;
- }
- }
- public static class ClientExt
- {
- public static double HeightInches(this IHeight client)
- {
- return client.Height * 39.3700787;
- }
- public static double WeightPounds(this IWeight client)
- {
- return client.Weight * 2.20462262;
- }
- }
- public class RSwitchable : Role
- {
- private bool on = false;
- public void TurnOn() { on = true; }
- public void TurnOff() { on = false; }
- public bool IsOn { get { return on; } }
- public bool IsOff { get { return !on; } }
- }
- public class RTunable : Role
- {
- public int Channel { get; private set; }
- public void Seek(int step) { Channel += step; }
- }
- public class Radio : Does<RSwitchable>, Does<RTunable> { }
- var radio = new Radio();
- radio.TurnOn();
- radio.Seek(42);
- radio.As<RSwitchable>().TurnOn();
- radio.As<RTunable>().Seek(42);
- interface TClientWeight {
- void foo {get; set;}
- }
- class Client {
- }
- class ClientA : Client, TClientWeight {
- void foo {get; set;}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement