-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8bd24d4
commit 94efb9b
Showing
15 changed files
with
122 additions
and
2 deletions.
There are no files selected for viewing
Empty file.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
------------------------------- | ||
命令模式 | ||
------------------------------- | ||
# 命令模式 | ||
// 命令接口 | ||
interface Command { | ||
void execute(); | ||
} | ||
class Car implements Command{ // 车 | ||
public void run() { | ||
System.out.println("car is runing..."); | ||
} | ||
@Override | ||
public void execute() { | ||
this.run(); | ||
} | ||
} | ||
|
||
class Control { // 控制器 | ||
private Command command; | ||
public Control(Command command) { | ||
this.command = command; | ||
} | ||
public void control() { | ||
this.command.execute(); | ||
} | ||
} | ||
|
||
public class Main { | ||
public static void main(String[] args) throws InterruptedException { | ||
Control control = new Control(new Car()); | ||
control.control(); // car is runing... | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,73 @@ | ||
---------------------------- | ||
装饰者设计模式 | | ||
---------------------------- | ||
* 没啥好说的,简单 | ||
# 没啥好说的,简单 | ||
abstract class Beverage { | ||
// 饮料 | ||
private static final String defaultName = "unknow"; | ||
public String getName() { | ||
return defaultName; | ||
} | ||
abstract int cost(); | ||
} | ||
abstract class CondimentDecorator extends Beverage { // 调料 | ||
public abstract String getName(); | ||
} | ||
|
||
class Coffee extends Beverage { // 咖啡 | ||
@Override | ||
public String getName() { | ||
return "咖啡"; | ||
} | ||
@Override | ||
public int cost() { | ||
return 15; | ||
} | ||
} | ||
class Cola extends Beverage { // 可乐 | ||
@Override | ||
public String getName() { | ||
return "可乐"; | ||
} | ||
@Override | ||
public int cost() { | ||
return 10; | ||
} | ||
} | ||
class Coriander extends CondimentDecorator { // 香菜 | ||
private Beverage beverage; | ||
public Coriander(Beverage beverage) { | ||
this.beverage = beverage; | ||
} | ||
@Override | ||
public String getName() { | ||
return "香菜"; | ||
} | ||
@Override | ||
public int cost() { | ||
return 2 + this.beverage.cost(); | ||
} | ||
} | ||
class Mustard extends CondimentDecorator { // 芥末 | ||
private Beverage beverage; | ||
public Mustard(Beverage beverage) { | ||
this.beverage = beverage; | ||
} | ||
@Override | ||
public String getName() { | ||
return "芥末"; | ||
} | ||
@Override | ||
public int cost() { | ||
return 1 + this.beverage.cost(); | ||
} | ||
} | ||
public class Main { | ||
public static void main(String[] args) throws InterruptedException { | ||
Beverage beverage = new Coffee(); // 咖啡 | ||
beverage = new Coriander(beverage); // 加 香菜 | ||
beverage = new Mustard(beverage); // 加 芥末 | ||
System.out.println(beverage.cost()); // 18块钱 | ||
} | ||
} | ||
|