Bibit Kelapa Gading Kuning Buah Tanaman Cepat Berbuah Bekasi Berkualitas

bibit kelapa gading kuning buah tanaman cepat berbuah Official Resmi bibit tanaman buah Pusat bibit hijau MOHON DIBACA SEBELUM MEMBELI : 1.Asal bibit : Langusung Dari Petani 2.Iklim Tumbuh Optimal …

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Managing Dependencies

Think about a busy application and the messages each object passes. At first, the traffic might seem overwhelming. However, if you take a step back and look at the bigger picture, you can start to see a pattern. Each message in this busy application is initiated by an object to invoke some bit of behavior. Each component has behavior that can be invoked. For any object to invoke some desired behavior, it either knows another object personally, inherits it, or knows another object who knows it. Well-designed objects have a single responsibility and collaborate with each other in order to accomplish complex tasks. In order to collaborate with other objects, they must know something about others. This knowledge of other objects creates a dependency.

An easy way to determine whether an object depends on another object is if when one object is forced to change as a result of another object changing. In the book “Practical Object Oriented Design in Ruby” by Sandi Metz, an object has a dependency when it knows:

When designing applications, your challenge is to manage dependencies so that each class has the fewest dependencies possible. A single class should not do a thing more than know just enough to do its job. Coupling objects together not only makes one change ripple through an entire application, but it also impossible to reuse tightly coupled objects.

Since I’m currently writing a Ruby tic-tac-toe application, I will use a generic game as an example (since it’s fresh in my brain). Below is a Game class with a method that contains an explicit reference to a class HumanPlayer:

The code below is another version of the Game class which expects to be initialized with an object that can respond to move:

In this version, the Game now uses the @player and as a result, doesn’t know or care that the object might either be an instance of class HumanPlayer or an instance of class ComputerPlayer. This is known as dependency injection.

The Game is now smarter because it knows less. It can now respond to both Player classes:

Another way to manage dependencies is removing the order of arguments that an object requires. Let’s just say for example that the Game class requires that a HumanPlayer needs to be a certain age in order to play. The Game and HumanPlayer classes may now look something like this (keep in mind that it’s specific for HumanPlayer. A ComputerPlayer doesn’t require age):

However, if the order is changed such that age is initialized as a String, then it won’t work:

Luckily (in Ruby, at least), there is a way to remove the argument-order dependency by using keywords. Take a look at the following example:

There is a slight change to the initialize method. The arguments now end in :, which denotes that they are keyword arguments. The HumanPlayer class can now be initialized in two ways:

The benefits of using keyword arguments as opposed to positional arguments are clear. Keyword arguments can be passed in any order. The HumanPlayer class will no longer have side-effects as a result of order. Woohoo!

Creating future-proof application starts with dependency management. It is the core of these types of applications. Dependency injection creates loosely-coupled objects that can be reused. Isolating your dependencies will allow objects throughout your application to quickly adapt to unexpected changes. Depending on explicit references as opposed to messages that objects can respond to increases the chance of changes crippling your application.

Add a comment

Related posts:

A system is software

A software is a process, a mechanic, a machine. The term logic means that we have been able to formalize in an objective way actions that, cumulatively, produce an emergence. The emergence of a…

Digitize a Logo for Machine Embroidery

If you have a business, you may want to consider printing your own logo or digitizing embroidery for merchandise. Printed logos are not as preferred as embroidered logos as the print may be damaged…

I Got Fingered By My Female Friend While Our Spouses Where Downstairs At The Party

I had never had sexual fantasies about another woman as a straight married lady. Until she came around. She was stunning, masculine, powerful, and enticing. I met her through my husband. We quickly…