c # design patterns (bridging) - Extension Method

1. Order to create a distribution interface, and realize each order, the general practice is as follows:

public interface IDistribute<T>
    {
        void Distribute(T t);
    }
    public class OrderDistribute:IDistribute<Order>
    {
        public void Distribute(Oreder order)
        {
            //配送订单
        }
    }
    public class DistributeHelper
    {
        IDistribute<Order> distribute = DistributeFactory.Create();
        public void DistributeOrder(Order order)
        {
            distribute.Distribute(order);
        }
    }

 

Abstract interface IDistribute a delivery method, OrderDistribute implements this interface, for implementing the specified logical, abstract all applications rely portion, i.e. IDistribute.

 

2. Now, because of the demand for change, the need for VIP distribution capabilities, we will increase the general method of VIP order method in the interface:

 public interface IDistribute<T>
    {
        void Distribute(T t);
        void DistributeVIP(T t);
    }
    public class OrderDistribute:IDistribute<Order>
    {
        public void Distribute(Oreder order)
        {
            //配送订单
        }
        public void DIstributeVIP(Order vipOrder)
        {
            //配送VIP订单
        }
    }
    public class DistributeHelper
    {
        IDistribute<Order> distribute = DistributeFactory.Create();
        public void DistributeOrder(Order order)
        {
            distribute.Distribute(order);
        }
        public void DistributeVIPOrder(Order ordervip)
        {
            distribute.DistributeVIP(ordervip);
        }
    }

 

This scenario is relatively normal, no one would have predicted a few years what changes the interface, the interface must be carried out at a time when change is dependent on all interfaces also need to change the interface will appear pollution, leads to a range of issues, including integration, testing and so on.

 

3. Using c # extension methods allow for independent evolution implement logic:

    public  interface IDistribute <T> 
    { 
        void the Distribute (T T); // interface does 
    }
     public  class OrderDistribute: IDistribute <the Order> 
    { 
        public  void the Distribute (Oreder Order) // initial implementation constant 
        {
             // Delivery Order 
        }         
    } 
    public  static  class DistributeExtends 
    { 
        public  static  void DistributeVIP ( the this IDistribute <the Order> the distribute, the Order vipOrder) 
        { 
            // distribution vip
        }
    }
    public class DistributeHelper
    {
        IDistribute<Order> distribute = DistributeFactory.Create();
        public void DistributeOrder(Order order)
        {
            distribute.Distribute(order);
        }
        public void DistributeVIPOrder(Order ordervip)
        {
            distribute.DistributeVIP(ordervip);
        }
    }

 

Extension method is developed in another dimension, not to modify the interface.

 

Extension methods to achieve bridging and bridging inheritance is essentially different extension methods no longer dependent on the abstract interface, but rather on the signature, there is a natural adaptability, can freely unlimited expansion.

In C #, extension methods preferably used to design patterns of abstraction and separation, in addition to bridge mode, to other modes can also bring great benefits.

 

Quote ".NET Framework model"

 

Guess you like

Origin www.cnblogs.com/coder-fang/p/11236542.html