Program to interfaces not to implementations
The next problem facing Skep is how to implement the new idea that has been fantastic. With the new system, thought you get a new advantage: you can now change the behavior of every soldier to run through some simple setter methods. Also in this way the Soldier classes should not know the implementation details of their behavior.
Skep plan to use an interface for each behavior (shoot and heal) and different implementations for different subtypes of conduct in order to remove the chains of slavery arising from the implementation of a method rather than another. It should be noted that the word interface is used incorrectly when you consider only as a java interface to interface here is meant as a supertype, even such an abstract class with abstract methods which do an override.
The scheme comes to design is the following:
What is the difference between planning and programming to interfaces to implementation?
Program is to make the code to implement static and extremely difficult to maintain, as new changes involve the creation and change of large parts of the existing code.
A programming implementation in our case might be this:
Marine Marine m = new ();
m.shoot ();
In this case, declare the variable m with static Marine, and then concrete implementation of a Soldier, forcing us to use the actual code implementation, or to the sea.
Programmare ad interfacce/supertipi invece significa sfruttare il polimorfismo, ad esempio in questo modo:
Soldier soldier = new Marine();
soldier.shoot();
In questo caso dipende dall'implementazione che diamo a shoot nella sottoclasse Marine: sappiamo che è un soldato ma possiamo usarne il riferimento in maniera polimorfica. Ciò ci permette anche l'iferenza del tipo a runtime in questo modo:
s = getSoldier();
s.shoot();
qui addirittura non ci interessiamo di che tipo sia il soldato, ci interessiamo solo che risponda al comando spara.
Un altro vantaggio di questa implementazione è che altri oggetti possono riusare il comportamento dei nostri soldati. Supponiamo che Skep voglia inserire nel gioco dei mezzi corazzati, e che un tipo di mezzo corazzato faccia gli stessi danni e spari allo stesso modo di un soldato con il bazooka. E' sufficiente richiamare nell'oggetto che rappresenterà il nuovo mezzo corazzato il comportamento definito precedentemente per il soldato e il gioco sarà fatto. Inoltre è facile aggiungere nuovi comportamenti senza modificare di una virgola i comportamenti definiti precedentemente.
L'idea generale è quindi questa: un Soldier delega il suo comportamento di attacco o cura invece di utilizzare metodi definiti nella classe Soldier o nelle sottoclassi.
Let's see how
Skep decides to really integrate the project.
1. Data fields and methods for the conduct
First, for each Soldier is necessary to add two data fields, one for a shoot and to heal. The nature of these data fields will be the type of interface or ShootBehavior HealBehavior, so you can instantiate in a sucessivamente using polymorphic subtypes defined. Also in Class Soldier you must define two methods, one for the shot and one for care, they will replace the old shoot and heal. The resulting matrix is \u200b\u200bas follows:
2. Implementing performShoot
public class Soldier {
ShootBehavior shootBehavior;
performShoot public void () {
shootBehavior.shoot ();}
}
Ext shootBehavior.shoot () embodies the idea of \u200b\u200bdelegating to other management behavior of the soldier. In this way, the soldier class will never be touched to change the implementation you shoot short, we do not care here as gunfire, only that shots. Rigurda exact same speech as the behavior of care
3. Implementation of the subtypes
Now Skep must implement the subtypes of a soldier, and do it this way:
Marine extends public class Soldier {public
Marine () {
shootBehavior ShootWithGun = new ();
healBehavior NoHeal = new ();}
public void display
{/ / Prints the marine
}}
While making an assignment of this type and shootBehavior healBehavior might seem erroneous in light of the programming interfaces, in reality, the static type of the variable is an interface and then you can, thanks to polymorphism, easily change the type runtime.
4. Implement behavior
It 's finally time to implement the behavior of sodati. Here I will show the implementation of behavior only shootBehavior, heal is very simple. First define the interface ShootBehavior
public interface ShootBehavior
{public void shoot ();}
Now we define the classes that implement this interface
public class ShootWithGun
{public void shoot () {
/ / firing the gun
}} public class
ShootWithBazooka
{public void shoot () {
/ / firing the bazooka
}}
In theory, our simulation a soldier would be ready, but Skep acora decides that it should be made more dynamic, in anticipation of future developments.
5. Controlled from the dynamic behavior
Skep says it is a pity that created everything and then dynamically assign the behavior at compilation time. He decides to enter a new method in the class Soldier
public void setShootBehavior (ShootBehavior sb) {
this . ShootBehavior = sb;}
In this way we can call these methods every time I want to change the behavior of the soldier. For example, suppose that a soldier is able to collect bazooka found on the ground from dead enemies. It would be so easy to change the capacity. At this point it would be logical to create a class model for every soldier and dynamically assign the various behaviors.
Skep has new ideas! In the next post we will see that this design has also taught another rule to our nice friend ...
0 comments:
Post a Comment