четверг, 20 октября 2011 г.

Паттерн Декоратор (Decorator). Пример кода


public interface Car {
    public int getPrice();
    public String getDescription();
}

public class SimpleCar implements Car {

    @Override
    public int getPrice() {
        return 100000;
    }

    @Override
    public String getDescription() {
        return "Simple auto";
    }
}

public abstract class CarDecorator implements Car {
    
    protected final Car car;
    
    public CarDecorator(Car car) {
        this.car = car;
    }

    @Override
    public int getPrice() {
        return car.getPrice();
    }

    @Override
    public String getDescription() {
        return car.getDescription();
    }
}

public class Conditioner extends CarDecorator {
    public Conditioner(Car car) {
        super(car);
    }
    
    public int getPrice() {
        return car.getPrice() + 2000;
    }
    
    public String getDescription() {
        return car.getDescription() + " with conditioner";
    }
}

public class LeatherInterior extends CarDecorator {
    public LeatherInterior(Car car) {
        super(car);
    }
    
    public int getPrice() {
        return car.getPrice() + 3000;
    }
    
    public String getDescription() {
        return car.getDescription() + " with leather interior";
    }
}

public class Client {
    public static void main(String[] args) {
        Car car = new SimpleCar();
        System.out.println("Price: " + car.getPrice() 
            + ", Description: " + car.getDescription());
        
        car = new Conditioner(car);
        System.out.println("Price: " + car.getPrice() 
            + ", Description: " + car.getDescription());
        
        car = new LeatherInterior(car);
        System.out.println("Price: " + car.getPrice() 
            + ", Description: " + car.getDescription());
    }
}
/**
 * Output:
 * Price: 100000, Description: Simple auto
 * Price: 102000, Description: Simple auto with conditioner
 * Price: 105000, Description: Simple auto with conditioner with leather interior
 */


вторник, 11 октября 2011 г.

Паттерн Компоновщик (Composite). Пример кода


/**
 * Component
 */
public abstract class TextPart {
    
    protected List value = new ArrayList();
    
    public String getValue() {
        StringBuilder sb = new StringBuilder();
        for (TextPart textPart : value) {
            sb.append(textPart.getValue());
        }
        return sb.toString();
    }
    
    public void append(TextPart... textPart) {
        for (TextPart part : textPart) {
            value.add(part);
        }
    }
}

/**
 * Composite
 */
public class Sentence extends TextPart {
    public Sentence(TextPart... value) {
        for (TextPart textPart : value) {
            this.value.add(textPart);
        }
    }
}

/**
 * Composite
 */
public class Paragraph extends Sentence {

    public Paragraph(TextPart... value) {
        super(value);
    }

    @Override
    public String getValue() {
        return "    " + super.getValue() + "\n\n";
    }

}


/**
 * Leaf
 */
public class Symbol extends TextPart {
    private char value;
    
    public Symbol(char value) {
        this.value = value;
    }

    @Override
    public String getValue() {
        return String.valueOf(value);
    }

    @Override
    public void append(TextPart... textPart) {
        throw new UnsupportedOperationException();
    }
}


public class Client {
    public static void main(String[] args) {
        Sentence sentence1  = new Sentence(
            new Symbol('H'),
            new Symbol('e'),
            new Symbol('l'),
            new Symbol('l'),
            new Symbol('o')
        );
        System.out.println(sentence1.getValue());
        
        Sentence sentence2  = new Sentence(
            new Symbol('w'),
            new Symbol('o'),
            new Symbol('r'),
            new Symbol('l'),
            new Symbol('d'),
            new Symbol('!')
        );
        
        Sentence sentence3 = new Sentence(sentence1, new Symbol(' '), sentence2);
        System.out.println(sentence3.getValue());
        
        Paragraph paragraph = new Paragraph(sentence3, new Symbol(' '), sentence3);
        System.out.println(paragraph.getValue());
    }
}

/**
 * Output:
 * Hello
 * Hello world!
 *     Hello world! Hello world!
 */


среда, 5 октября 2011 г.

Паттерн Мост (Bridge). Пример кода


public abstract class Animal {
    AnimalImp animalImp;
    public Animal(AnimalImp animalImp) {
        this.animalImp = animalImp;
    }
    public void say() {
        animalImp.say();
    }
}

public class HappyAnimal extends Animal {
    public HappyAnimal(AnimalImp animalImp) {
        super(animalImp);
    }
}

public interface AnimalImp {
    public void say();
}

public class Cat implements AnimalImp {
    public void say() {
        System.out.println("Miaou! I'm a happy cat!");
    }    
}

public class Pig implements AnimalImp {
    public void say() {
        System.out.println("Groin-groin! I'm a happy pig!");
    }    
}

public class Rooster implements AnimalImp {
    public void say() {
        System.out.println("Cocorico! I'm a happy rooster!");
    }    
}

import java.util.ArrayList;
import java.util.List;

public class Client {
    public static void main(String[] args) {
        List happyAnimals = new ArrayList();
        happyAnimals.add(new HappyAnimal(new Cat()));
        happyAnimals.add(new HappyAnimal(new Pig()));
        happyAnimals.add(new HappyAnimal(new Rooster()));
        
        for (Animal animal: happyAnimals) {
            animal.say();
        }
    }
}
/**
 * Output:
 * Miaou! I'm a happy cat!
 * Groin-groin! I'm a happy pig!
 * Cocorico! I'm a happy rooster!
 */