zoukankan      html  css  js  c++  java
  • 策略模式、简单工厂结合

    需求:使用加密狗进行软件保护,可能会使用不同的加密狗。

    分析:加密狗

    设计:使用策略模式,但由于策略模式需要使用人员了解哪个组件提供了软件保护服务,于是结合工厂模式对对象进行创建。由于可以使用的加密狗不会很多,所以采用了简单工厂模式。

      这样,结构大体如下:工厂、功能抽象、功能实现、功能环境。

    编码:

    功能抽象:

    public abstract class AbstractKeyWriter
    {
      internal abstract void write(string key);
    }

    功能实现:

    internal class AWriter:AbstractKeyWriter
    {
      internal override void write(string key)
      {
        throw new NotImplementedException();
      }
    }

    internal class BWriter:AbstractKeyWriter
    {
      internal override void write(string key)
      {
        throw new NotImplementedException();
      }
    }

    功能环境:

    internal class KeyWriterContext
    {
      private AbstractKeyWriter writer;
      public KeyWriterContext() { }
      public KeyWriterContext(AbstractKeyWriter writer)
      {
        this.writer = writer;
      }
      public void setKeyWriter(AbstractKeyWriter writer)
      {
        this.writer = writer;
      }
      public void write(string key)
      {
        this.writer.write(key);
      }
    }

    工厂:

    public class KeyWriterFactory
    {
      public static AbstractKeyWriter getAWriter()
      {
        return new AWriter();
      }


      private static AbstractKeyWriter getBWriter()
      {
        return new BWriter();
      }
    }

    用户不能直接通过具体实现类进行调用,而只能通过环境类。

    .net里没用包的概念,不能方便实现访问限制,没有只能名称空间级的访问限制,太不方便了,鄙视一下。

  • 相关阅读:
    poj 1860 Currency Exchange(最短路径的应用)
    poj 2965 The Pilots Brothers' refrigerator
    zoj 1827 the game of 31 (有限制的博弈论)
    poj 3295 Tautology (构造法)
    poj 1753 Flip Game(枚举)
    poj 2109 (贪心)
    poj 1328(贪心)
    Qt 对单个控件美化
    Qt 4基础
    Bash Shell
  • 原文地址:https://www.cnblogs.com/javaleon/p/3740553.html
Copyright © 2011-2022 走看看