Exemplo n.º 1
0
    static void Main(string[] args)
    {
        Console.WriteLine("Proxy Pattern\n");

        ISubject proxyA = new ProxyA();
        Console.WriteLine(proxyA.Request());

        ProtectedProxy subject = new ProtectedProxy();
        Console.WriteLine(subject.Request());
        Console.WriteLine(subject.Authenticate("abc"));
        Console.WriteLine(subject.Authenticate("abc123"));
        Console.WriteLine(subject.Request());

        Console.ReadLine();

        /*
         * Output:
         *
         * Proxy Pattern
         *
         * Subject inactive
         * Subject activated
         * Proxy A: Call - Subject request response
         *
         * Protected Proxy: Must authenticated first
         * Protected Proxy: No access
         * Protected Proxy: Authenticated
         * Protected Proxy: Call - Subject request response
         *
         */
    }
Exemplo n.º 2
0
        public static ProxyB CreateProxyBWithSelfReference(ClassB bObject)
        {
            var b = new ProxyB(bObject.Name)
            {
                A       = bObject.A,
                B       = bObject.B,
                ListOfA = bObject.ListOfA,
                ListOfB = bObject.ListOfB
            };

            b.BProxy         = b;
            b.ListOfBProxies = new List <ProxyB>
            {
                b,
                new ProxyB(bObject.Name)
            };

            var a = new ProxyA(bObject.Name);

            b.AProxy         = a;
            b.ListOfAProxies = new List <ProxyA>
            {
                a,
                new ProxyA(bObject.Name)
            };

            a.AProxy         = a;
            a.ListOfAProxies = new List <ProxyA>
            {
                a,
                new ProxyA(bObject.Name)
            };
            a.BProxy         = b;
            a.ListOfBProxies = b.ListOfBProxies;

            return(b);
        }
Exemplo n.º 3
0
        public static ProxyA CreateProxyWithSelfReference(ClassA aObject)
        {
            var a = new ProxyA(aObject.Name)
            {
                A       = aObject.A,
                B       = aObject.B,
                ListOfA = aObject.ListOfA,
                ListOfB = aObject.ListOfB
            };

            a.AProxy         = a;
            a.ListOfAProxies = new List <ProxyA>
            {
                a,
                new ProxyA(aObject.Name)
            };

            var b = new ProxyB(aObject.Name);

            a.BProxy         = b;
            a.ListOfBProxies = new List <ProxyB>
            {
                b,
                new ProxyB(aObject.Name)
            };

            b.BProxy         = b;
            b.ListOfBProxies = new List <ProxyB>
            {
                b,
                new ProxyB(aObject.Name)
            };
            b.AProxy         = a;
            b.ListOfAProxies = a.ListOfAProxies;

            return(a);
        }
Exemplo n.º 4
0
 public override int GetHashCode()
 {
     return(ProxyA.GetHashCode() + ProxyB.GetHashCode());
 }
    public void Run()
    {
        Failed = false;
        try {
            // Older versions of SWIG used IntPtr instead of HandleRef to hold the underlying
            // C++ pointer, so this test would (usually) fail as the garbage collector would
            // sometimes collect the Number class while it was being used in unmanaged code
            for (int i = 0; i < 5000; i++) // run test for a few seconds
            {
                {
                    Obj    obj    = new Obj();
                    Number n      = new Number(i);
                    Number triple = obj.triple(n);
                    if (triple.Value != i * 3)
                    {
                        throw new ApplicationException("triple failed: " + triple.Value);
                    }
                }
                {
                    Obj    obj    = new Obj();
                    Number n      = new Number(i);
                    Number times6 = obj.times6(n);
                    if (times6.Value != i * 6)
                    {
                        throw new ApplicationException("times6 failed: " + times6.Value);
                    }
                }
                {
                    Obj    obj    = new Obj();
                    Number n      = new Number(i);
                    Number times9 = obj.times9(n);
                    if (times9.Value != i * 9)
                    {
                        throw new ApplicationException("times9 failed: " + times9.Value);
                    }
                }
                {
                    Number n         = new Number(i);
                    Number quadruple = csharp_typemaps.quadruple(n);
                    if (quadruple.Value != i * 4)
                    {
                        throw new ApplicationException("quadruple failed: " + quadruple.Value);
                    }
                }
                {
                    Number n      = new Number(i);
                    Number times8 = csharp_typemaps.times8(n);
                    if (times8.Value != i * 8)
                    {
                        throw new ApplicationException("times8 failed: " + times8.Value);
                    }
                }
                {
                    Number n       = new Number(i);
                    Number times12 = csharp_typemaps.times12(n);
                    if (times12.Value != i * 12)
                    {
                        throw new ApplicationException("times12 failed: " + times12.Value);
                    }
                }
            }
        } catch (Exception e) {
            Console.Error.WriteLine("Test failed (thread " + threadId + "): " + e.Message);
            Failed = true;
        }

        // $imfuncname substitution
        ProxyA pa = new ProxyA();

        if (pa.imfuncname_test() != 123)
        {
            throw new ApplicationException("imfuncname_test is not 123");
        }
        if (ProxyA.imfuncname_static_test() != 234)
        {
            throw new ApplicationException("imfuncname_test is not 234");
        }
        if (csharp_typemaps.imfuncname_global_test() != 345)
        {
            throw new ApplicationException("imfuncname_test is not 345");
        }

        pa.variab = 1000;
        if (pa.variab != 1000 + 111 + 222)
        {
            throw new ApplicationException("pa.variab is not 1333");
        }
        csharp_typemaps.global_variab = 1000;
        if (csharp_typemaps.global_variab != 1000 + 333 + 444)
        {
            throw new ApplicationException("csharp_typemaps.variab is not 1777");
        }
    }