Why is “dependency injection” ok, but not “the opposite of preserve whole object (pass required parameters only)”?

According to Why should I use dependency injection?, “dependency injection” has some advantages, for example:

“Non dependency injection” version:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Client{
private Logger logger;
public Client(){
this.logger=new Logger();
}
}
</code>
<code>public class Client{ private Logger logger; public Client(){ this.logger=new Logger(); } } </code>
public class Client{
    private Logger logger;
    public Client(){
        this.logger=new Logger();
    }
}

“Dependency Injection” version:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public Interface ILogger{
}
public class Client{
private ILogger logger;
public Client(ILogger logger){
this.logger=logger;
}
}
</code>
<code>public Interface ILogger{ } public class Client{ private ILogger logger; public Client(ILogger logger){ this.logger=logger; } } </code>
public Interface ILogger{
}
public class Client{
    private ILogger logger;
    public Client(ILogger logger){
        this.logger=logger;
    }
}

Dependency Injection version is better because:

  1. Client doesn’t need to always depend on Logger, which can use other types of ILogger (eg:PrinterLogger) instead of bind to specific implementation

  2. Easier to test : Dependency Injection version don’t need to new a Logger object first to test Client, or we may replace Logger to other ILogger such as PrinterLogger, more likely a unit test instead of integration test

  3. Looser coupling : Removing Logger class doesn’t require me to modify Client to recompile

  4. More reuseable Client : I don’t need to create another Client or another constructor when I need another Logger, eg: I don’t need to add : public Client(){ this.logger=new PrinterLogger(); } when I want to use PrinterLogger

And when there are some codes “pass required parameters only”, for example:

“Pass required parameters only” version:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class showInfo(String name, int age){
}
this.showInfo(student.getName(),student.getAge());
</code>
<code>public class showInfo(String name, int age){ } this.showInfo(student.getName(),student.getAge()); </code>
public class showInfo(String name, int age){
}

this.showInfo(student.getName(),student.getAge());

it would usually be considered as code smell and want to be refactored as “preserve whole object”:

“Preserve whole object” version:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public void showInfo(Student student){
}
this.showInfo(student);
</code>
<code>public void showInfo(Student student){ } this.showInfo(student); </code>
public void showInfo(Student student){
}

this.showInfo(student);

However, I think change “Preserve whole object version” to “pass required parameters only” seems go the similar direction of “dependency injection” and have similar advantages:

  1. showInfo() doesn’t need to depend on Student, which can use other types of objects that also contain name and age (eg: Teacher) instead of binding specific implementation

  2. Easer to test : Pass required parameters only don’t need to new a Student object first to test showInfo(),or we may replace Student with other objects that also has name and age such as Teacher at the calling client side, more likely a unit test instead of integration test

  3. Looser coupling : Removing Student class doesn’t require me to modify showInfo() to recompile

  4. More reuseable showInfo() : I don’t need to create another showInfo() method when I need another object (eg: I don’t need to add public void showInfo(Teacher teacher){} when I want to show info of Teacher that also contains name and age)

Also I think I can apply the advantages of “Preserve whole object” to say “Non dependency injection” is better:

  1. “Non dependency injection” has less parameters in the Client constructor, which fits the motivation of “Preserve whole object” that shorter parameter list is better

  2. In “Non dependency injection”, adding new components don’t need to modify the parameter list of constructors,eg:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Client{
private Logger logger;
private Timer timer;
public Client(){
this.logger=new Logger();
this.timer=new Timer();
}
}
</code>
<code>public class Client{ private Logger logger; private Timer timer; public Client(){ this.logger=new Logger(); this.timer=new Timer(); } } </code>
public class Client{
    private Logger logger;
    private Timer timer;
    public Client(){
        this.logger=new Logger();
        this.timer=new Timer();
    }
}

but the “Dependency injection” version needs to add a new parameter:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class Client{
private ILogger logger;
private Timer timer;
public Client(ILogger logger, ITimer timer){
this.logger=logger;
this.timer=timer;
}
}
</code>
<code>public class Client{ private ILogger logger; private Timer timer; public Client(ILogger logger, ITimer timer){ this.logger=logger; this.timer=timer; } } </code>
public class Client{
    private ILogger logger;
    private Timer timer;
    public Client(ILogger logger, ITimer timer){
        this.logger=logger;
        this.timer=timer;
    }
}
  1. “Non dependency injection” types less words when calling constructors:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Client c1=new Client();
Client c2=new Client();
</code>
<code>Client c1=new Client(); Client c2=new Client(); </code>
Client c1=new Client();
Client c2=new Client();

instead of

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Client c1=new Client(new Logger());
Client c2=new Client(new Logger());
</code>
<code>Client c1=new Client(new Logger()); Client c2=new Client(new Logger()); </code>
Client c1=new Client(new Logger());
Client c2=new Client(new Logger());

, as if in showInfo() repeating getters:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>showInfo(student);
</code>
<code>showInfo(student); </code>
showInfo(student);

instead of

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>showInfo(student.getName(),student.getAge());
</code>
<code>showInfo(student.getName(),student.getAge()); </code>
showInfo(student.getName(),student.getAge());

But I guess you would not say “Dependency injection” is code smell, and should be refactored to “Non dependency injection” version, right?

But I believe the fact is, “Pass required parameters only” is always considered a code smell waiting to be refactored as “Preserve whole object”, instead of some code smell that waiting to be refactored to “Pass required parameters only”.

So my question is, why is “Dependency Injection” ok, but not “Pass required parameters only” even if I think they are having similar goals and going to similar directions?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật