In: Computer Science
/* Encapsulated family of Algorithms
* Interface and its implementations
*/
public interface IBrakeBehavior {
public void brake();
}
public class BrakeWithABS implements IBrakeBehavior {
public void brake() {
System.out.println("Brake with ABS applied");
}
}
public class Brake implements IBrakeBehavior {
public void brake() {
System.out.println("Simple Brake applied");
}
}
/* Client that can use the algorithms above interchangeably
*/
public abstract class Car {
private IBrakeBehavior brakeBehavior;
public Car(IBrakeBehavior brakeBehavior) {
this.brakeBehavior = brakeBehavior;
}
public void applyBrake() {
brakeBehavior.brake();
}
public void setBrakeBehavior(IBrakeBehavior brakeType) {
this.brakeBehavior = brakeType;
}
}
/* Client 1 uses one algorithm (Brake) in the constructor
*/
public class Sedan extends Car {
public Sedan() {
super(new Brake());
}
}
/* Client 2 uses another algorithm (BrakeWithABS) in the
constructor */
public class SUV extends Car {
public SUV() {
super(new BrakeWithABS());
}
}
/* Using the Car example */
public class CarExample {
public static void main(final String[] arguments) {
Car sedanCar = new Sedan();
sedanCar.applyBrake(); // This will invoke class "Brake"
Car suvCar = new SUV();
suvCar.applyBrake(); // This will invoke class "BrakeWithABS"
// set brake behavior dynamically
suvCar.setBrakeBehavior( new Brake() );
suvCar.applyBrake(); // This will invoke class "Brake"
}
}
UML Diagram if provided below . It is explained thoroughly. if you need any further clarification, please ask in comments.
########################################################################
EXPLANATION
A UML class diagram represents the relationship among different classes, and describes the attributes and operations of a class. Although we have 6 classses in this program but we have taken 5 classes for UML class digaram. Because generally we dont consider the class, which has only main function and no other attributes or function. As class CarExample does not have any attributes and functions other than main(), so we are not considering it for UML.
Symbols used:
For Access Specifiers:
We have used + for public and - for private members.
For relationships:
1. Dotted Arrow:--- This is a relationship between Interface and and the classes that implements it
2. Solid Arraow --- This is a realtionship where one class extends other class, hence it is a Inheritance relationship between two classes
3. Arrow with Diamond--- This is a aggregation or composition relationshipe. In other words it is a Has-A relationship, when one class has as its attribute the object of some other class. If the relationship is very strong as in our case then it is called composition. What it means is That a car and brakes can not exist independently. They are in a strong relationship.
Now we have a interface IBrakeBehaviour with one function. this is implemnted by two classes Brake and BrakeWithABS . Bot these classes have only the inherited function. Then we have a class Car which has has-A relationship with IBrakeBehaviour. it has a private attribute . This attribute is an object of type IBrakeBehaviour. Then we have three methods which are public. One is constructor and other two areapplyBrake() and setBrakeBehaviour(). Then there is a class SUV which extends The class Car. it ha sonly one public constructor.