Last active
August 29, 2015 14:06
-
-
Save marinhoarthur/4cec49663de7ecc49e46 to your computer and use it in GitHub Desktop.
Decorator pattern written in Java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.car.decorator; | |
interface Car | |
{ | |
int getHp(); | |
float getPrice(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.car.decorator; | |
public class Collector extends MotorDecorator | |
{ | |
Collector(Car c) | |
{ | |
super(c); | |
} | |
@Override | |
public int getHp() | |
{ | |
return c.getHp() + 20; | |
} | |
@Override | |
public float getPrice() | |
{ | |
return c.getPrice() + 100; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.car.decorator; | |
abstract public class Decorator implements Car | |
{ | |
Car c; | |
Decorator(Car c) | |
{ | |
this.c=c; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.car.decorator; | |
public class Factory | |
{ | |
public static void main(String[] s) | |
{ | |
// Customizing at instantiation | |
Car c = new SeatMassager(new Collector(new Turbo(new Turbo(new Turbo(new Prius()))))); | |
Car cc = new SeatMassager(new LeaderSeat(new Turbo(new Turbo(new Turbo(new Prius()))))); | |
Car ccc = new Turbo(new Prius()); | |
Car cccc = new LeaderSeat(new Prius()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.car.decorator; | |
public class InteriorDecorator extends Decorator | |
{ | |
InteriorDecorator(Car c) | |
{ | |
super(c); | |
} | |
@Override | |
public int getHp() | |
{ | |
return c.getHp(); | |
} | |
@Override | |
public float getPrice() | |
{ | |
return c.getPrice(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.car.decorator; | |
public class LeaderSeat extends InteriorDecorator | |
{ | |
LeaderSeat(Car c) | |
{ | |
super(c); | |
} | |
@Override | |
public float getPrice() | |
{ | |
return c.getPrice() + 2000; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.car.decorator; | |
public class MotorDecorator extends Decorator | |
{ | |
MotorDecorator(Car c) | |
{ | |
super(c); | |
} | |
@Override | |
public int getHp() | |
{ | |
return c.getHp(); | |
} | |
@Override | |
public float getPrice() | |
{ | |
return c.getPrice(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.car.decorator; | |
public class Prius implements Car | |
{ | |
int hp = 104; | |
float price = 10000; | |
@Override | |
public int getHp() | |
{ | |
return hp; | |
} | |
@Override | |
public float getPrice() | |
{ | |
return price; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.car.decorator; | |
public class SeatMassager extends InteriorDecorator | |
{ | |
SeatMassager(Car c) | |
{ | |
super(c); | |
} | |
@Override | |
public float getPrice() | |
{ | |
return c.getPrice() + 5000; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.car.decorator; | |
public class Turbo extends MotorDecorator | |
{ | |
Turbo(Car c) | |
{ | |
super(c); | |
} | |
@Override | |
public int getHp() | |
{ | |
return c.getHp() + 50; | |
} | |
@Override | |
public float getPrice() | |
{ | |
return c.getPrice() + 500; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment