Singleton Design Pattern

Singleton define as a class of which only a single instance can exist by http://www.dofactory.com/net/singleton-design-pattern.

This pattern has no logical diagram because it is not as complicated.

You can see in the sample below there is only a simple class, Singleton. This class defines an Instance operation that lets clients access its unique instance. Instance is a class operation and responsible for creating and maintaining its own unique instance.

The theory of this is simple. The data members of the Singleton class must be static and the member functions of the Singleton class must also be static.

 

using System;

 

namespace ConAppSingleton

{

    class MainApp

    {

        static void Main()

        {

            // Constructor is protected — cannot use new

            Singleton s1 = Singleton.Instance();

            Singleton s2 = Singleton.Instance();

 

            // Test for same instance

            if (s1 == s2)

            {

                Console.WriteLine(“Objects are the same instance”);

            }

 

            // Wait for user

            Console.ReadKey();

        }

    }

 

    class Singleton

    {

        private static Singleton _instance;

 

        // Constructor is ‘protected’

        protected Singleton()

        {

        }

 

        public static Singleton Instance()

        {

            // Uses lazy initialization.

            // Note: this is not thread safe.

            if (_instance == null)

            {

                _instance = new Singleton();

            }

 

            return _instance;

        }

    }

}

You can also download a single solution from http://skydrive.live.com. The sample file name is ConAppSingleton.zip. My MSN ID is chanmmn@hotmail.com.

Posted in .Net | Tagged , | Leave a comment

HTTP Error 503. The service is unavailable

This is a re-blog, I found the existing blog is incomplete. I added additional step to resolve the HTTP Error 503 for SharePoint 2013.

You might bump into the error message “HTTP Error 503. The service is unavailable” when you are having SharePoint 2010 or SharePoint 2013.

clip_image001

Make sure you start the necessary Application Pools in IIS.

clip_image003

Do check the account use for the Application Pool. Check on the Advance Setting. Then select the Identity.

clip_image005

Click on Set then re-enter the User name: and Password.

sharepoint50313

Posted in .Net | Tagged , , | Leave a comment

Convert Abstract class to Interface in C#

This short article showing you how to convert the Abstract class to Interface in C#.

If in your abstract class you have totally has no implement on any Method like the simple Proxy Design Pattern below then you should convert your Abstract class to become Interface.

using System;

 

namespace DoFactory.GangOfFour.Proxy.Structural

{

    class MainApp

    {

        static void Main()

        {

            // Create proxy and request a service

            Proxy proxy = new Proxy();

            proxy.Request();

 

            // Wait for user

            Console.ReadKey();

        }

    }

 

    abstract class Subject

    {

        public abstract void Request();

    }

 

    class RealSubject : Subject

    {

        public override void Request()

        {

            Console.WriteLine(“Called RealSubject.Request()”);

        }

    }

 

    class Proxy : Subject

    {

        private RealSubject _realSubject;

 

        public override void Request()

        {

            // Use ‘lazy initialization’

            if (_realSubject == null)

            {

                _realSubject = new RealSubject();

            }

 

            _realSubject.Request();

        }

    }

}

Below is the code that convert from Abstract class to Interface.

using System;

 

namespace ConAppProxy

{

    class MainApp

    {

        static void Main()

        {

            // Create proxy and request a service

            Proxy proxy = new Proxy();

            proxy.Request();

 

            // Wait for user

            Console.ReadKey();

        }

    }

 

    interface Subject

    {

        //public abstract void Request();

        void Request();

    }

 

    class RealSubject : Subject

    {

        //public override void Request()

        public void Request()

        {

            Console.WriteLine(“Called RealSubject.Request()”);

        }

    }

 

    class Proxy : Subject

    {

        private RealSubject _realSubject;

 

        public void Request()

        {

            // Use ‘lazy initialization’

            if (_realSubject == null)

            {

                _realSubject = new RealSubject();

            }

 

            _realSubject.Request();

        }

    }

}

Posted in .Net | Tagged , | Leave a comment

Cannot access to Local Area Network (LAN) connection

If you cannot access to LAN connection, there are few possibilities.

 network

1. You IP is not in the same segment.

2. Your DNS server did not set correctly.

3. You Network Interface Card is spoiled.

4. Driver is not correct.

5. System board is spoiled.

If all these are not the reason there is still a last one I found.

6. Go to shutdown all you Hyper-V VM then restart your machine.

Posted in Community, Computers and Internet, Operating System | Tagged , , | Leave a comment

Proxy Design Pattern C#

This short article shows you how to build Proxy Design Pattern using C#.

The design pattern define as an object representing another object by http://www.dofactory.com/net/proxy-design-pattern.

 

The logical model for Proxy Design Pattern as following.

clip_image001

The key class in this pattern is the Proxy class.

The first class to create is the Subject Interface class. This class defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected.

interface Subject

{

    void Request();

}

 

Inherit the Subject class to create Proxy class. This class maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.

The Proxy class provides an interface identical to Subject’s so that a proxy can be substituted for the real subject and controls access to the real subject and may be responsible for creating and deleting it.

The other responsibilites depend on the kind of proxy:

·         remote proxies are responsible for encoding a request and its arguments and for sending the encoded request to the real subject in a different address space.

·         virtual proxies may cache additional information about the real subject so that they can postpone accessing it. For example, the ImageProxy from the Motivation caches the real images’s extent.

·         protection proxies check that the caller has the access permissions required to perform a request.

 

class Proxy : Subject

{

    private RealSubject _realSubject;

 

    public void Request()

    {

        // Use ‘lazy initialization’

        if (_realSubject == null)

        {

            _realSubject = new RealSubject();

        }

 

        _realSubject.Request();

    }

}

Lastly create the RealSubject class to define the real object that the proxy represents.

class RealSubject : Subject

{

    //public override void Request()

    public void Request()

    {

        Console.WriteLine(“Called RealSubject.Request()”);

    }

}

 

The main method as below:

static void Main()

{

    // Create proxy and request a service

    Proxy proxy = new Proxy();

    proxy.Request();

 

    // Wait for user

    Console.ReadKey();

}

 

You can also download a single solution from http://skydrive.live.com. The sample file name is ConAppProxy.zip. My MSN ID is chanmmn@hotmail.com.

Posted in .Net | Tagged , , | Leave a comment

Read and write to properties file in Java

This short article shows you how to read and write value to properties file in Java.

 

1. Import java.util.Properties

 

import java.util.Properties;

 

2. The code snippet to write to properties file as below.

try (OutputStream out = new FileOutputStream(“example.properties”)) {

                     Properties properties = new Properties();

                     properties.setProperty(“name”, “mingman”);

3. The snippet to read to properties file as below.

try (InputStream in = new FileInputStream(“example.properties”)) {

                     Properties prop = new Properties();

                     prop.load(in);

                     System.out.println(“####Properties.getProperty usage####”);

                     System.out.println(prop.getProperty(“name”));

                     System.out.println();

 

                     System.out.println(“####Properties.stringPropertyNames usage####”);

                     for (String property : prop.stringPropertyNames()) {

                           String value = prop.getProperty(property);

                           System.out.println(property + “=” + value);

                     }

This code is extracted from http://examples.javacodegeeks.com/core-java/util/properties/properties-file-java-example/.

You can also download a single solution from http://skydrive.live.com. The sample file name is JavaPropertiesExample.zip. My MSN ID is chanmmn@hotmail.com.

Posted in Java, Oracle | Tagged | Leave a comment

Import Eclipse project into NetBeans

This short article shows you how to import Eclipse project into NetBeans.

1. File -> Import Project -> Eclipse Project

 netbean

2. If you import a single project, choose Import project ignoring project dependencies.

 nbimport

I tested the project running fine after import.

Posted in Java, Oracle | Tagged | Leave a comment