Are there any strong reasons to use DI for sub-classes that are known to be tightly coupled to their parent containers?

I have Product classes for Products A through Z. And each Product class has its own set of product-specific subclasses, which I’d say are tightly coupled to them, like so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>//specific product class inherits from generic product
class ProductA extends Product
{
function __construct()
{
parent::__construct();
//product-specific subclasses that are tightly coupled to the product
//note that they are not DI-ed, although they *could be*
$this->weight = new WeightA();
$this->spec = new SpecA();
}
}
class Product
{
public $weight;
public $spec;
function __construct()
{
//these are generic versions in case someone calls a generic product class
$this->weight = new Weight();
$this->spec = new Spec();
}
}
//example instantiation
$p = new ProductA();
$p->weight->a_specific = 4;
</code>
<code>//specific product class inherits from generic product class ProductA extends Product { function __construct() { parent::__construct(); //product-specific subclasses that are tightly coupled to the product //note that they are not DI-ed, although they *could be* $this->weight = new WeightA(); $this->spec = new SpecA(); } } class Product { public $weight; public $spec; function __construct() { //these are generic versions in case someone calls a generic product class $this->weight = new Weight(); $this->spec = new Spec(); } } //example instantiation $p = new ProductA(); $p->weight->a_specific = 4; </code>
//specific product class inherits from generic product
class ProductA extends Product
{
    function __construct()
    {
        parent::__construct();

        //product-specific subclasses that are tightly coupled to the product
        //note that they are not DI-ed, although they *could be*
        $this->weight = new WeightA();
        $this->spec = new SpecA();
    }
}

class Product
{
    public $weight;
    public $spec;

    function __construct()
    {
        //these are generic versions in case someone calls a generic product class   
        $this->weight = new Weight();
        $this->spec = new Spec();
    }    
}

//example instantiation
$p = new ProductA();
$p->weight->a_specific = 4;

Question:

I spent a bit of time reflecting on whether I wanted to pass my Weight and Spec subclasses using Dependency-Injection techniques, like so (adjusting my classes respectively):

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$p = new ProductA(new WeightA(), new SpecA());
</code>
<code>$p = new ProductA(new WeightA(), new SpecA()); </code>
$p = new ProductA(new WeightA(), new SpecA());

But so far I have found no good reason to do so. In fact, doing so may increase the chance of error where I can by accident do something like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>$p = new ProductA(new WeightB(), new SpecC()); //note the disparate classes
</code>
<code>$p = new ProductA(new WeightB(), new SpecC()); //note the disparate classes </code>
$p = new ProductA(new WeightB(), new SpecC()); //note the disparate classes

Question:

  • Are there any good reason to DI tightly-coupled subclasses into generic class containers anyway?
  • is there a common name for this issue (pattern) that I am dealing with?
  • are there other (better) patterns available to use in my instance?

Background:

I am refactoring a large ball-of-mud application and it is getting some shape, but one of the patterns I have identified is that there are product-specific classes that contain a lot of product-specific sub-classes, such as in the above example.

Possible Middle-Ground:

Note: alternatively if I really want to DI, I can use DI inside product-specific subclasses, injecting subclasses into the parent container (opposed to injecting it from the outside into the product-specific classes themselves like I have done above), like so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class ProductA extends Product
{
function __construct()
{
parent::__construct(new WeightA(), new SpecA());
}
}
class Product
{
function __construct($weight, $spec)
{
$this->weight = $weight;
$this->spec = $spec;
}
}
</code>
<code>class ProductA extends Product { function __construct() { parent::__construct(new WeightA(), new SpecA()); } } class Product { function __construct($weight, $spec) { $this->weight = $weight; $this->spec = $spec; } } </code>
class ProductA extends Product
{
    function __construct()
    {
        parent::__construct(new WeightA(), new SpecA());
    }
}

class Product
{
    function __construct($weight, $spec)
    {
        $this->weight = $weight;
        $this->spec = $spec;
    }    
}

I wonder if that will be a better option overall. One problem with it is that ProductA() has no parameters but Product($weight, $spec) will have two parameters, which is an inconsistency.

Why is the tight coupling an issue? If I create a class containing two integer fields or properties, it is tightly coupled to the implementation of integers. Is this a problem? Only if you need to change the integer to a different type, selecting that type at run time.

The premise behind the question (tight coupling is a problem) seems flawed. We can always add more DI and more abstractions. But does it add anything from the user’s viewpoint? Does it improve readability and reduce complexity? If you are adding things that are not needed, the answer is always no.

1

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