Last active
December 25, 2016 07:39
-
-
Save spin-glass/e25605a5298b5b1d37281a2ee890f465 to your computer and use it in GitHub Desktop.
This file contains 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
public class Client { | |
public static void main (String[] args) { | |
System.out.println("start"); | |
Factory myFactory; | |
myFactory = new Factory(); | |
Component myComponent = myFactory.getComponent(); | |
myComponent.prtTicket(); | |
System.out.println("end"); | |
} | |
} | |
abstract class Component { | |
abstract public void prtTicket(); | |
} | |
class SalesTicket extends Component { | |
public void prtTicket() { | |
System.out.println("売上伝票の印刷処理"); | |
} | |
} | |
abstract class TicketDecorator extends Component { | |
private Component myComponent; | |
public TicketDecorator(Component myComponent) { | |
this.myComponent = myComponent; | |
} | |
public void callTrailer() { | |
if (myComponent != null) | |
myComponent.prtTicket(); | |
} | |
} | |
class Header1 extends TicketDecorator { | |
public Header1(Component myComponent) { | |
super(myComponent); | |
} | |
public void prtTicket() { | |
System.out.println("ヘッダ1"); | |
callTrailer(); | |
} | |
} | |
class Header2 extends TicketDecorator { | |
public Header2(Component myComponent) { | |
super(myComponent); | |
} | |
public void prtTicket() { | |
callTrailer(); | |
System.out.println("ヘッダ2"); | |
} | |
} | |
class Footer1 extends TicketDecorator { | |
public Footer1(Component myComponent) { | |
super(myComponent); | |
} | |
public void prtTicket() { | |
callTrailer(); | |
System.out.println("フッタ1"); | |
} | |
} | |
class Footer2 extends TicketDecorator { | |
public Footer2(Component myComponent) { | |
super(myComponent); | |
} | |
public void prtTicket() { | |
callTrailer(); | |
System.out.println("フッタ2"); | |
} | |
} | |
class Factory { | |
public Component getComponent() { | |
Component myComponent; | |
myComponent = new SalesTicket(); | |
myComponent = new Footer2(myComponent); | |
myComponent = new Footer1(myComponent); | |
myComponent = new Header2(myComponent); | |
myComponent = new Header1(myComponent); | |
return myComponent; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment