Skip to main content
added 553 characters in body
Source Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190

Finding features

To find a feature I would create an extension like this one:

static class FeatureExtensions
{ 
    public static T Find<T>(this Feature feature) where T : Feature
    {
        var result = default(T);
        feature.ForEach(f =>
        {
            if (f.GetType() == typeof(T)) 
            {
                result = (T)f;
                return;
            }
        });
        return result;
    }
}

Example:

var injectionA = feature.Find<InjectionA>();
var injectionB = feature.Find<InjectionB>();
...do the comparison

You can virtually add anything you want.


Finding features

To find a feature I would create an extension like this one:

static class FeatureExtensions
{ 
    public static T Find<T>(this Feature feature) where T : Feature
    {
        var result = default(T);
        feature.ForEach(f =>
        {
            if (f.GetType() == typeof(T)) 
            {
                result = (T)f;
                return;
            }
        });
        return result;
    }
}

Example:

var injectionA = feature.Find<InjectionA>();
var injectionB = feature.Find<InjectionB>();
...do the comparison

You can virtually add anything you want.

added 685 characters in body
Source Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190

Decorator

You can also build different cars with a decorator pattern instead of an inheritance.

You leave the Car untouched:

abstract class Car
{
    protected Car(string make) { ... }
    public string Make { get; }
    public Feature Feature { get; set; }
}

but then instead of more abstract classes you wrap a Focus as a Combi

class Focus : Car
{
    public Focus() : base("Ford") {}
}

class Combi : Car 
{
    private readonly _car;
    public Combi(Car car) { _car = car; }
}

Example:

var focus = new Focus();
var focusCombi = new Combi(focus);

Decorator

You can also build different cars with a decorator pattern instead of an inheritance.

You leave the Car untouched:

abstract class Car
{
    protected Car(string make) { ... }
    public string Make { get; }
    public Feature Feature { get; set; }
}

but then instead of more abstract classes you wrap a Focus as a Combi

class Focus : Car
{
    public Focus() : base("Ford") {}
}

class Combi : Car 
{
    private readonly _car;
    public Combi(Car car) { _car = car; }
}

Example:

var focus = new Focus();
var focusCombi = new Combi(focus);
added 1645 characters in body
Source Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190

Feature composition

First we need an abstract class for other features and a helper Empty property. ForEach will us allow to get all features.

abstract class Feature
{
    public static Feature Empty => new CompositeFeature();

    public virtual void ForEach(Action<Feature> feature) { feature(this); }    
}

Next we need some contrete features:

class PowerWindows : Feature { }

class InjectionA : Feature { }

To build a feature chain we need a composite feature that will store the chain and it will also enumerate it:

class CompositeFeature : Feature
{
    private readonly Feature[] _features;

    public CompositeFeature(params Feature[] feautures)
    {
        _features = feautures;
    }
    
    public override void ForEach(Action<Feature> feature) 
    { 
        foreach (var f in _features)
        {
            f.ForEach(feature);
        }        
    }
}

With an extension like this we can easily build the chain:

static class FeatureComposition
{
    public static Feature Add<TFeature>(this Feature feature) where TFeature : Feature, new()
    {
        return new CompositeFeature(feature, new TFeature());
    }
}

Usage:

var feature = Feature.Empty
    .Add<PowerWindows>()
    .Add<InjectionA>();

feature.ForEach(x => /* do somehting with x */ );

It's a one big feature that is composed of many other features.

I've added the ForEach so that we can get them all and do something later with each of them.

One of my favourite patterns ;-)


Feature composition

First we need an abstract class for other features and a helper Empty property. ForEach will us allow to get all features.

abstract class Feature
{
    public static Feature Empty => new CompositeFeature();

    public virtual void ForEach(Action<Feature> feature) { feature(this); }    
}

Next we need some contrete features:

class PowerWindows : Feature { }

class InjectionA : Feature { }

To build a feature chain we need a composite feature that will store the chain and it will also enumerate it:

class CompositeFeature : Feature
{
    private readonly Feature[] _features;

    public CompositeFeature(params Feature[] feautures)
    {
        _features = feautures;
    }
    
    public override void ForEach(Action<Feature> feature) 
    { 
        foreach (var f in _features)
        {
            f.ForEach(feature);
        }        
    }
}

With an extension like this we can easily build the chain:

static class FeatureComposition
{
    public static Feature Add<TFeature>(this Feature feature) where TFeature : Feature, new()
    {
        return new CompositeFeature(feature, new TFeature());
    }
}

Usage:

var feature = Feature.Empty
    .Add<PowerWindows>()
    .Add<InjectionA>();

feature.ForEach(x => /* do somehting with x */ );

It's a one big feature that is composed of many other features.

I've added the ForEach so that we can get them all and do something later with each of them.

One of my favourite patterns ;-)

added 8 characters in body
Source Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190
Loading
added 560 characters in body
Source Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190
Loading
added 560 characters in body
Source Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190
Loading
Source Link
t3chb0t
  • 44.3k
  • 9
  • 84
  • 190
Loading