官术网_书友最值得收藏!

Bad or broken designs

Quite often, the structure or design and patterns used in implementing an application can result in bad code, most especially when object-oriented programming principles or design patterns are wrongly used. A common anti-pattern is spaghetti coding. It is common among developers with little grasp of object-orientation and this involves creating a code base with unclear structures, little or no reusability, and no relationships between objects and components. This leads to applications that are difficult to maintain and extend.

There is a common practice among inexperienced developers, which is the unnecessary or inappropriate use of design patterns in solving application complexity. The design patterns when used incorrectly can give a code base bad structure and design. The use of design patterns should simplify complexity and create readable and maintainable solutions to software problems. When a pattern is causing a readability issue and overtly adding complexity to a program, it is worth reconsidering whether to use the pattern at all, as the pattern is being misused.

For example, a singleton pattern is used to create a single instance to a resource. The design of a singleton class should have a private constructor with no arguments, a static variable with reference to the single instance of the resource, and a managed public means of referencing the static variable. A singleton pattern can simplify the access to a single-shared resource but can also cause a lot of problems when not implemented with thread safety in mind. Two or more threads can access the if (smtpGateway==null) line at the same time, which can create multiple instances of the resource if the line is evaluated to true, as with the implementation shown in the following code:

public class SMTPGateway
{
private static SMTPGateway smtpGateway=null;

private SMTPGateway()
{
}

public static SMTPGateway SMTPGatewayObject
{
get
{
if (smtpGateway==null)
{
smtpGateway = new SMTPGateway();
}
return smtpGateway;
}
}
}
主站蜘蛛池模板: 邮箱| 铜陵市| 石狮市| 磐安县| 西丰县| 沛县| 新余市| 山东省| 肥城市| 包头市| 宣化县| 西吉县| 铜川市| 翁牛特旗| 新竹县| 班玛县| 贡嘎县| 柘荣县| 民和| 嵊州市| 兴仁县| 高淳县| 乌拉特后旗| 方山县| 深水埗区| 永仁县| 武隆县| 临安市| 临夏市| 怀远县| 海晏县| 合作市| 连平县| 朝阳市| 张家口市| 和龙市| 万载县| 棋牌| 黔江区| 米林县| 全南县|