- C# and .NET Core Test Driven Development
- Ayobami Adewole
- 318字
- 2021-06-25 22:00:26
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;
}
}
}
- HTML5+CSS3王者歸來
- Microsoft Exchange Server PowerShell Cookbook(Third Edition)
- Apache Hive Essentials
- 數(shù)據(jù)結(jié)構(gòu)(C語言)
- The Data Visualization Workshop
- Swift細(xì)致入門與最佳實踐
- SQL Server與JSP動態(tài)網(wǎng)站開發(fā)
- Android驅(qū)動開發(fā)權(quán)威指南
- 微課學(xué)人工智能Python編程
- 區(qū)塊鏈架構(gòu)之美:從比特幣、以太坊、超級賬本看區(qū)塊鏈架構(gòu)設(shè)計
- Mastering Gephi Network Visualization
- 現(xiàn)代CPU性能分析與優(yōu)化
- Instant GLEW
- C語言程序設(shè)計教程
- Elasticsearch搜索引擎構(gòu)建入門與實戰(zhàn)