ARTS Week 2 - Dependency Injection

Algorithm:

Problem: Unique Email Addresses
Answer:

public class Solution {
    public int NumUniqueEmails(string[] emails) {
        HashSet<string> uniqueEmails = new HashSet<string>();
        
        foreach(string email in emails) {
            string prefix = email.Substring(0, email.IndexOf('@'));
            while (prefix.IndexOf('.') != -1) {
                var dotIndex = prefix.IndexOf('.');
                prefix = prefix.Substring(0, dotIndex) + prefix.Substring(dotIndex + 1);
            }
            while (prefix.IndexOf('+') != -1) {
                var addIndex = prefix.IndexOf('+');
                prefix = prefix.Substring(0, addIndex);
            }
            uniqueEmails.Add(prefix + email.Substring(email.IndexOf('@')));
        }
        
        return uniqueEmails.Count;
    }
}

Simplify:

public class Solution {
    public int NumUniqueEmails(string[] emails) {
        HashSet<string> uniqueEmails = new HashSet<string>();
        
        foreach(string email in emails) {
            var emailParts = email.Split('@');
            uniqueEmails.Add(emailParts[0].Split('+')[0].Replace(".", "") + emailParts[1]);
        }
        
        return uniqueEmails.Count;
    }
}

Review:

Article: Inversion of Control Containers and the Dependency Injection pattern
Notes:

There are three main styles of dependency injection. The names I'm using for them are Constructor Injection, Setter Injection, and Interface Injection.

Injection isn't the only way to break this dependency, another is to use a service locator.

A really good article about dependency injection and service locator. Well describe three type of dependency injection.

Article: The Log: What every software engineer should know about real-time data's unifying abstraction
It is a little hard for me to understand this article. I will continue the article in the future.

Tip:

Something about dependency injection.
What is dependency injection.

Dependency injection means giving an object its instance variables.

Why we use it?

It's handy for isolating classes during testing.

More details in Share and Ref.

Share:

Inversion of Control Containers and the Dependency Injection pattern
An article about dependency injection.

Follow Up:

  1. Complete the article
  2. Paxos Algorithm

Ref:

[1] https://stackoverflow.com/questions/130794/what-is-dependency-injection
[2] https://www.jamesshore.com/Blog/Dependency-Injection-Demystified.html
[3] https://stackoverflow.com/questions/130794/what-is-dependency-injection
[4] https://martinfowler.com/articles/injection.html

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容