装饰模式
一、小菜扮靓第一版
class Person{
private String name;
public Person(String name) {
this.name=name;
}
public void WearTShirts(){
System.out.print("大T恤");
}
public void WearBigTrouser() {
System.out.print("垮裤");
}
public void WearSneakers() {
System.out.print("破球鞋");
}
public void WearSuit() {
System.out.print("西装");
}
public void WearTie() {
System.out.print("领带");
}
public void WearLeatherShoes() {
System.out.print("皮鞋");
}
public void Show() {
System.out.printf("装扮的"+name);
}
}
public class Main{
public static void main(String[] args){
Person xc=new Person("小菜");
System.out.printf("\n第一种装扮:");
xc.WearTShirts();
xc.WearBigTrouser();
xc.WearSneakers();
xc.Show();
System.out.printf("\n第二种装扮:");
xc.WearSuit();
xc.WearTie();
xc.WearLeatherShoes();
xc.Show();
}
}
增加装扮,需要修改Person类,而这样就违反了开放封闭原则。
二、小菜扮靓第二版
class Person{
private String name;
public Person(String name) {
this.name=name;
}
public void Show() {
System.out.printf("装扮的"+name);
}
}
abstract class Finery{
public abstract void Show();
}
class TShirts extends Finery{
public void Show() {
System.out.print("大T恤");}
}
class BigTrouser extends Finery{
public void Show() {
System.out.print("垮裤");}
}
class Sneakers extends Finery {
public void Show() {
System.out.print("破球鞋");}
}
class Suit extends Finery {
public void Show() {
System.out.print("西装");}
}
class Tie extends Finery {
public void Show() {
System.out.print("领带");}
}
class LeatherShoes extends Finery {
public void Show() {
System.out.print("皮鞋");}
}
public class Main{
public static void main(String[] args){
Person xc=new Person("小菜");
System.out.printf("\n第一种装扮:");
Finery dtx=new TShirts();
Finery kk=new BigTrouser();
Finery pqx=new Sneakers();
dtx.Show();
kk.Show();
pqx.Show();
xc.Show();
System.out.printf("\n第二种装扮:");
Finery xz=new Suit();
Finery ld=new Tie();
Finery px=new LeatherShoes();
xz.Show();
ld.Show();
px.Show();
xc.Show();
}
}
三、装饰模式
Component是定义一个对象接口,可以给这些对象动态地添加职责。ConcreteComponent是定义了一个具体的对象,也可以给这个对象添加一些职责。Decorater,装饰抽象类,继承了Component,从外来类扩展Component类的功能,但对Component来说,是无需知道Decorator的存在的。至于ConcreteDecorator就是具体的装饰对象,起到给Component添加职责的功能。
abstract class Component{
public abstract void Operation();
}
class ConcreteComponent extends Component{
@Override
public void Operation() {
// TODO Auto-generated method stub
System.out.println("具体对象操作");
}
}
abstract class Decorator extends Component{
protected Component component;
public void SetComponent(Component component) {
this.component=component;
}
public void Operation() {
if(component!=null) {//执行的是成员component的Operation()
component.Operation();
}
}
}
class ConcreteDecoratorA extends Decorator{
private String addedState;//区别于A的独有方法
public void Operation() {
super.Operation();
addedState="New State";
System.out.println("具体装饰对象A的操作");
}
}
class ConcreteDecoratorB extends Decorator{
private void addedBehavior() {//区别于A的独有方法
System.out.println("AddedBehavior Operation");
}
public void Operation() {
super.Operation();
addedBehavior();
System.out.println("具体装饰对象B的操作");
}
}
public class Main{
public static void main(String[] args){
ConcreteComponent c=new ConcreteComponent();
ConcreteDecoratorA d1=new ConcreteDecoratorA();
ConcreteDecoratorB d2=new ConcreteDecoratorB();
d1.SetComponent(c);
d2.SetComponent(d1);
d2.Operation();
}
}
装饰的方法是:首先用ConcreteCompotent实例化对象c,然后用ConcreteDecoratorA的实例化对象d1来包装c,再用ConcreteDecoratorB的对象d2包装d1,最终执行d2的Operation()。
四、小菜扮靓第三版
package operation;
abstract class Component{
public abstract void Show();
}
class Person extends Component{
public Person() {}
private String name;
public Person(String name) {
this.name=name;
}
@Override
public void Show() {
// TODO Auto-generated method stub
System.out.println("装扮的"+name);
}
}
class Finery extends Component{
protected Component component;
public void Decorate(Component component) {
this.component=component;
}
@Override
public void Show() {
// TODO Auto-generated method stub
if(component!=null) {
component.Show();
}
}
}
class TShirts extends Finery{
public void Show() {
System.out.println("大T恤"); super.Show();
}
}
class BigTrouser extends Finery{
public void Show() {
System.out.println("垮裤"); super.Show();
}
}
class Sneakers extends Finery{
public void Show() {
System.out.println("破球鞋"); super.Show();
}
}
class Suit extends Finery{
public void Show() {
System.out.println("西装"); super.Show();
}
}
class Tie extends Finery{
public void Show() {
System.out.println("领带"); super.Show();
}
}
class LeatherShoes extends Finery{
public void Show() {
System.out.println("皮鞋"); super.Show();
}
}
public class Main{
public static void main(String[] args){
Person xc=new Person("小菜");
System.out.println("\n第一种装扮");
Sneakers p=new Sneakers();
BigTrouser k=new BigTrouser();
TShirts d=new TShirts();
p.Decorate(xc);
k.Decorate(p);
d.Decorate(k);
d.Show();
System.out.println("\n第二种装扮");
LeatherShoes px=new LeatherShoes();
Tie l=new Tie();
Suit x=new Suit();
px.Decorate(xc);
l.Decorate(px);
x.Decorate(l);
x.Show();
}
}