admin 管理员组文章数量: 887021
2023年12月19日发(作者:此电脑管理快捷键)
public class Computer {
private CPU cpu;
private Memory memory;
private Disk disk;
public Computer(){
cpu = new CPU();
memory = new Memory();
disk = new Disk();
}
public void startup(){
n(“start the computer!”);
p();
p();
p();
n(“start computer finished!”);
}
public void shutdown(){
n(“begin to close the computer!”);
wn();
wn();
wn();
n(“computer closed!”);
}
}
User类如下:public class User {
public static void main(String[] args) {
Computer computer = new Computer();
p();
wn();
}
}
输出:
arrayInt[0] = nt(array[0]);
arrayInt[1] = nt(array[1]);
return arrayInt;
}
}
三个实现类:public class Plus extends AbstractCalculator implements ICalculator {
@Override
public int calculate(String exp) {
int arrayInt[] = split(exp,”+”);
return arrayInt[0]+arrayInt[1];
}
}
public class Minus extends AbstractCalculator implements ICalculator {
@Override
public int calculate(String exp) {
int arrayInt[] = split(exp,”-“);
return arrayInt[0]-arrayInt[1];
}
}
public class Multiply extends AbstractCalculator implements ICalculator {
@Override
public int calculate(String exp) {
int arrayInt[] = split(exp,”*”);
return arrayInt[0]*arrayInt[1];
}
}
简单的测试类:public class StrategyTest {
public static void main(String[] args) {
String exp = “2+8″;
ICalculator cal = new Plus();
int result = ate(exp);
n(result);
}
public MyIterator(Collection collection){
tion = collection;
}
@Override
public Object previous() {
if(pos > 0){
pos–;
}
return (pos);
}
@Override
public Object next() {
if(pos<()-1){
pos++;
}
return (pos);
}
@Override
public boolean hasNext() {
if(pos<()-1){
return true;
}else{
return false;
}
}
@Override
public Object first() {
pos = 0;
return (pos);
}
}
测试类:public class Test {
public static void main(String[] args) {
Collection collection = new MyCollection();
Iterator it = or();
public class MyCommand implements Command {
private Receiver receiver;
public MyCommand(Receiver receiver) {
er = receiver;
}
@Override
public void exe() {
();
}
}
public class Receiver {
public void action(){
n(“command received!”);
}
}
public class Invoker {
private Command command;
public Invoker(Command command) {
d = command;
}
public void action(){
();
}
}
public class Test {
public static void main(String[] args) {
Receiver receiver = new Receiver();
Command cmd = new MyCommand(receiver);
Invoker invoker = new Invoker(cmd);
();
}
}
}
}
public class Memento {
private String value;
public Memento(String value) {
= value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
= value;
}
}
public class Storage {
private Memento memento;
public Storage(Memento memento) {
o = memento;
}
public Memento getMemento() {
return memento;
}
public void setMemento(Memento memento) {
o = memento;
}
}
测试类:public class Test {
public static void main(String[] args) {
// 创建原始类
Original origi = new Original(“egg”);
public void setValue(String value) {
= value;
}
public void method1(){
n(“execute the first opt!”);
}
public void method2(){
n(“execute the second opt!”);
}
}
package ;
/**
* 状态模式的切换类 2012-12-1
* @author erqing
*
*/
public class Context {
private State state;
public Context(State state) {
= state;
}
public State getState() {
return state;
}
public void setState(State state) {
= state;
}
public void method() {
if (ue().equals(“state1″)) {
1();
} else if (ue().equals(“state2″)) {
2();
}
private Mediator mediator;
public Mediator getMediator(){
return mediator;
}
public User(Mediator mediator) {
or = mediator;
}
public abstract void work();
}
public class User1 extends User {
public User1(Mediator mediator){
super(mediator);
}
@Override
public void work() {
n(“user1 exe!”);
}
}
public class User2 extends User {
public User2(Mediator mediator){
super(mediator);
}
@Override
public void work() {
n(“user2 exe!”);
}
}
测试类:public class Test {
public static void main(String[] args) {
Mediator mediator = new MyMediator();
Mediator();
l();
return num1;
}
public void setNum1(int num1) {
1 = num1;
}
public int getNum2() {
return num2;
}
public void setNum2(int num2) {
2 = num2;
}
}
public class Test {
public static void main(String[] args) {
// 计算9+2-8的值
int result = new Minus().interpret((new Context(new Plus()
.interpret(new Context(9, 2)), 8)));
n(result);
}
}
最后输出正确的结果:3。
版权声明:本文标题:最经典的java23种设计模式及具体例子 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1702953820h437242.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论