ARTS Week 2 - Dependency Injection

Algorithm:

Problem: Unique Email Addresses
Answer:

public class Solution {
    public int NumUniqueEmails(string[] emails) {
        HashSet uniqueEmails = new HashSet();
        
        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 uniqueEmails = new HashSet();
        
        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

你可能感兴趣的:(ARTS Week 2 - Dependency Injection)