桥接模式和策略模式的区别:
桥接模式使我们能够重新实施,根据我们当前的情况运行业务结构,另一方策略模式使我们能够实施各种业务策略并将它们封装起来并根据情况或一次使用它们。
两者的主要区别在于使用桥接模式我们可以改变我们的整体结构,但使用策略我们可以同时改变我们的业务策略。
根据我的理解,我在下面详细阐述了两个非常重要的设计模式。
请小心扔掉这个我想它会清除你对它们的理解。
桥接模式:
什么是桥接设计模式?
GoF 建议的桥模式的意义是将组件的实现与其抽象分离。
我们什么时候会使用桥接设计模式?
让我们想象一个组件已经实现并且根据您的业务需求运行良好的情况。突然,该组织改变了他们的商业战略。为此,您需要更改或重新实现组件。在这种情况下,您将做什么改变过去几年运行良好的前一个,或者您创建新组件。在这种情况下,桥模式很好地处理了这个场景。请参阅下面的示例以更好地理解。
// Main component
public interface Ibridge
{
void function1();
}
// Already Implemented that are currently being using
public class Bridge1 : Ibridge
{
public void function1()
{
Console.WriteLine("Implemented function from bridge 1");
}
}
//New Implementation as per Organisation needs
public class Bridge2 : Ibridge
{
public void function1()
{
Console.WriteLine("Implemented function from bridge2");
}
}
//Abstract Calling functionalities
public interface IAbstractBridge
{
void CallFunc1();
}
// implementation of calling implemented component at a time
public class AbstractBridge:IAbstractBridge
{
protected Ibridge _ibridge;
public Ibridge Ibridge
{
set { _ibridge = value; }
}
public void CallFunc1()
{
this._ibridge.function1();
}
}
class Program
{
static void Main(string[] args)
{
AbstractBridge abp = new AbstractBridge();
/*
here you see that now being using the previous implemented component.
but need change newly implemented component so here we need just changed
the implementation of component, please see below
*/
//Commented old one
abp.Ibridge = new Bridge1();
//using new one just change the "Bridge1" to "Bridge2"
abp.Ibridge = new Bridge2();
abp.CallFunc1();
}
}
策略设计模式:
什么是策略设计模式?
GoF 建议的策略模式的意义是定义一系列算法,封装每个算法,并使它们可互换。策略让算法独立于使用它的客户端而变化。
我们什么时候会使用策略设计模式?
假设一个购物中心的老板想要吸引顾客根据他们的不同场合提供不同的折扣优惠,并且任何时候老板都可以从折扣模式切换到正常模式,反之亦然,那么如何处理这种情况在这种情况下,策略模式处理了这种情况。让我们看下面的例子来更好地理解。
public interface ISellingStrategy
{
void selling();
}
public class BasicStrategy : ISellingStrategy
{
public void selling()
{
Console.WriteLine("Buy Three get 5% discount.");
}
}
public class ChrismasStrategy : ISellingStrategy
{
public void selling()
{
Console.WriteLine("Buy Three get one offer + extra 5% discount.");
}
}
public class HoliFestiveStrategy : ISellingStrategy
{
public void selling()
{
Console.WriteLine("Buy two get one offer + extra 5% discount.");
}
}
public class DurgapuljaStrategy : ISellingStrategy
{
public void selling()
{
Console.WriteLine("Buy one get one offer + extra 5% discount.");
}
}
public class Billing
{
private ISellingStrategy strategy;
public void SetStrategy(ISellingStrategy _strategy)
{
this.strategy = _strategy;
}
public void ApplyStrategy()
{
strategy.selling();
Console.WriteLine("Please wait offer is being applying...");
Console.WriteLine("Offer is now Applied and ready for billing..");
}
}
public class BillingFactory
{
public static Billing CreateBillingObject()
{
return new Billing();
}
}
class Program
{
static void Main(string[] args)
{
Billing billing = BillingFactory.CreateBillingObject();
billing.SetStrategy(new BasicStrategy());
billing.ApplyStrategy();
Console.ReadLine();
}
}