-Поиск по дневнику

Поиск сообщений в rss_thedaily_wtf

 -Подписка по e-mail

 

 -Постоянные читатели

 -Статистика

Статистика LiveInternet.ru: показано количество хитов и посетителей
Создан: 06.04.2008
Записей:
Комментариев:
Написано: 0


CodeSOD: Patterned After Success

Четверг, 23 Июля 2015 г. 13:30 + в цитатник

Design patterns are more than just useless interview questions that waste everyones time and annoy developers. Theyre also a set of approaches to solving common software problems, while at the same time, being a great way to introduce new problems, but enough about Spring.

For those of us that really want global variables back in our object oriented languages, the Singleton pattern is our go-to approach. Since its the easiest design pattern to understand and implement, those new to design patterns tend to throw it in everywhere, whether or not it fits.

Andres once worked with a developer who not only was new and enthusiastic about design patterns, but didnt actually understand how to implement the Singleton pattern. Which is why Andres was getting null reference exceptions when trying to get an instance from this singleton.

   public class SingletonSmtpClient
    {
        private static SmtpClient _smtpClient;

        public SingletonSmtpClient(string host, string user, string password, bool ssl)
        {
            if (_smtpClient == null)
            {
                _smtpClient = new SmtpClient();
                _smtpClient.Credentials = new System.Net.NetworkCredential(user, password);
                _smtpClient.EnableSsl = ssl;
                _smtpClient.Host = host;
            }
        }

        public static SmtpClient getInstance()
        {
                return _smtpClient;
        }
    }



[Advertisement] Use NuGet or npm? Check out ProGet, the easy-to-use package repository that lets you host and manage your own personal or enterprise-wide NuGet feeds and npm repositories. It's got an impressively-featured free edition, too!

http://thedailywtf.com/articles/patterned-after-success

Метки:  

 

Добавить комментарий:
Текст комментария: смайлики

Проверка орфографии: (найти ошибки)

Прикрепить картинку:

 Переводить URL в ссылку
 Подписаться на комментарии
 Подписать картинку