package lq.test;
import java.io.*;
import java.util.*;
//********* ***************
//factory method 1
//1 , 2
interface Product {
}
// ,
abstract class Factory {
abstract public Product fmd();
// , FactoryMethod
// , ,
// , FactoryMethod ,
// , ,
// , , ,
// FactoryMethod
// TemplateMethod
public Product creat() {
Product pd = null;
System.out.println("before operation");
pd = fmd();
System.out.println("end operation");
return pd;
}
}
class Product1 implements Product {
}
class Factory1 extends Factory {
public Product fmd() {
Product pd = new Product1();
return pd;
}
}
//FactroyMethod 2
//
interface Producta {
}
interface Factorya {
Producta create();
}
class Producta1 implements Producta {}
class Factorya1 implements Factorya {
public Producta create() {
Producta pda = null;
pda = new Producta1();
return pda;
}
}
//AbstractFactory
//AbstractFactory FactoryMethod AbstractFactory
//
//
interface Apda {}
interface Apdb {}
interface Afactory {
Apda createA();
Apdb createB();
}
class Apda1 implements Apda {}
class Apdb1 implements Apdb {}
//
class Afactory1 implements Afactory {
public Apda createA() {
Apda apda = null;
apda = new Apda1();
return apda;
}
public Apdb createB() {
Apdb apdb = null;
apdb = new Apdb1();
return apdb;
}
}
//Builder
// ,
// , , TemplateMethod
interface Cpda {}
class Cpda1 implements Cpda {}
interface BuilderI {
void buildPart1();
void buildPart2();
void initPd();
Cpda getPd();
}
abstract class BuilderA implements BuilderI {
Cpda cpda;
public Cpda getPd() {
initPd();
//
buildPart1();
buildPart2();
return cpda;
}
}
class Builder extends BuilderA {
public void buildPart1() {
System.out.println(cpda);
}
public void buildPart2() {
System.out.println(cpda);
}
public void initPd() {
cpda = new Cpda1();
}
}
//
//1
abstract class Fy {
public abstract void med1();
static class Fy1 extends Fy {
public void med1() {
}
}
public static Fy getInstance() {
Fy fy = new Fy1();
return fy;
//Fy fy = new Fy1() {// !!
//public void med1() {
//}
//};
//return fy;
}
}
//2
interface Pdd {}
class Pdd1 implements Pdd {}
abstract class Fya {
public static Pdd getPd() {
Pdd pdd = new Pdd1();
return pdd;
}
}
//Prototype java clone,
class CloneObja {
public CloneObja MyClone() {
return new CloneObja();
}
}
class CloneObjb {
public CloneObjb MyClone() throws Throwable {
CloneObjb cobj = null;
cobj = (CloneObjb) pcl(this);
return cobj;
}
//
private Object pcl(Object obj) throws Throwable {
ByteArrayOutputStream bao = new ByteArrayOutputStream(1000);
ObjectOutputStream objo = new ObjectOutputStream(bao);
objo.writeObject(obj);
ByteArrayInputStream bai = new ByteArrayInputStream(bao.toByteArray());
ObjectInputStream obji = new ObjectInputStream(bai);
Object objr = obji.readObject();
return objr;
}
}
//Singleton
// , , cache
class Singleton1 {
public static Singleton1 instance = new Singleton1();
private Singleton1() {
}
public static Singleton1 getInstance() {
return instance;
}
}
class Singleton2 {
public static Singleton2 instance;
private Singleton2() {
}
//public static Singleton2 getInstance() {
//if (instance == null) {
//instance = new Singleton2();
//}
//
//return instance;
//}
public static Singleton2 getInstance() {
synchronized(Singleton2.class) {
if (instance == null) {
instance = new Singleton2();
}
}
return instance;
}
}
//********** **********
//Adapter
// ,
// , ,
// ,
// , ( ),
// ( , )
//
class Adapteea {
public void kk() {}
}
interface Targeta {
String vv(int i, int k);
}
class Adaptera implements Targeta{
Adapteea ade;
public Adaptera(Adapteea ade) {
this.ade = ade;
}
public String vv(int i, int k) {
// Adaptee ,
//
//
ade.kk();
return null;
}
}
//
class Adapteeb {
public void kk() {}
}
interface Targetb {
String vv(int i, int k);
}
class Adapterb extends Adapteeb implements Targetb {
public String vv(int i, int k) {
//
kk();
return null;
}
}
//Proxy
interface Subject {
void request();
}
class realSubject implements Subject {
public void request() {
//do the real business
}
}
class Proxy implements Subject {
Subject subject;
public Proxy(Subject subject) {
this.subject = subject;
}
public void request() {
System.out.println("do something");
subject.request();
System.out.println("do something");
}
}
//Bridge
//
interface Imp {
void operation();
}
class Cimp1 implements Imp {
public void operation() {
System.out.println("1");
}
}
class Cimp2 implements Imp {
public void operation() {
System.out.println("2");
}
}
class Invoker {
Imp imp = new Cimp1();
public void invoke() {
imp.operation();
}
}
//Composite
interface Component {
void operation();
void add(Component component);
void remove(Component component);
}
class Leaf implements Component {
public void operation() {
System.out.println("an operation");
}
public void add(Component component) {
throw new UnsupportedOperationException();
}
public void remove(Component component) {
throw new UnsupportedOperationException();
}
}
class Composite implements Component {
List components = new ArrayList();
public void operation() {
Component component = null;
Iterator it = components.iterator();
while (it.hasNext()) {
// component leaf composite,
// leaf , composite
component = (Component) it.next();
component.operation();
}
}
public void add(Component component) {
components.add(component);
}
public void remove(Component component) {
components.remove(component);
}
}
//Decorator
// , , ,
// , ,
// , ,
// concrete new ,
// decorator decorator
// ,
//Decorator
interface Componenta {
void operation();
}
class ConcreteComponent implements Componenta {
public void operation() {
System.out.println("do something");
}
}
class Decorator implements Componenta {
private Componenta component;
public Decorator(Componenta component) {
this.component = component;
}
public void operation() {
//do something before
component.operation();
//do something after
}
}
//Facade
// ,
class Obj1 {
public void ope1() {}
public void ope2() {}
}
class Obj2 {
public void ope1() {}
public void ope2() {}
}
class Facade {
//
public void fdMethod() {
Obj1 obj1 = new Obj1();
Obj2 obj2 = new Obj2();
obj1.ope1();
obj2.ope2();
}
}
//Flyweight
//
//********** *************
//Chain of Responsibility
// Decorator ,
//Decorator ,
//Chain
// if , , ,
// , ,
// ,
interface Handler {
void handRequest(int signal);
}
class CHandler1 implements Handler {
private Handler handler;
public CHandler1(Handler handler) {
this.handler = handler;
}
public void handRequest(int signal) {
if (signal == 1) {
System.out.println("handle signal 1");
}
else {
handler.handRequest(signal);
}
}
}
class CHandler2 implements Handler {
private Handler handler;
public CHandler2(Handler handler) {
this.handler = handler;
}
public void handRequest(int signal) {
if (signal == 2) {
System.out.println("handle signal 2");
}
else {
handler.handRequest(signal);
}
}
}
class CHandler3 implements Handler {
public void handRequest(int signal) {
if (signal == 3) {
System.out.println("handle signal 3");
}
else {
throw new Error("can't handle signal");
}
}
}
class ChainClient {
public static void main(String[] args) {
Handler h3 = new CHandler3();
Handler h2 = new CHandler2(h3);
Handler h1 = new CHandler1(h2);
h1.handRequest(2);
}
}
//Interpreter
// Composite ,
//Template Method
abstract class TemplateMethod {
abstract void amd1();
abstract void amd2();
// Template Method
public void tmd() {
amd1();
amd2();
}
}
//State
//
//
class Contexta {
private State st;
public Contexta(int nst) {
changeStfromNum(nst);
}
public void changeStfromNum(int nst) {
if (nst == 1) {
st = new CStatea1();
}
else if (nst == 2) {
st = new CStatea2();
}
throw new Error("bad state");
}
void request() {
st.handle(this);
}
}
interface State {
void handle(Contexta context);
}
class CStatea1 implements State {
public void handle(Contexta context) {
System.out.println("state 1");
// ,
//context.changeStfromNum(2);
}
}
class CStatea2 implements State {
public void handle(Contexta context) {
System.out.println("state 2");
}
}
//
// state
//class StateFactory {
//public static State getStateInstance(int num) {
//State st = null;
//
//if (num == 1) {
//st = new CStatea1();
//}
//else if (num == 2) {
//st = new CStatea2();
//}
//
//return st;
//}
//}
//Strategy
// Bridge ,
//Visitor
// , ,
interface Visitor {
void visitElement(Elementd element);
}
class CVisitor implements Visitor {
public void visitElement(Elementd element) {
element.operation();
}
}
interface Elementd {
void accept(Visitor visitor);
void operation();
}
class CElementd implements Elementd {
public void accept(Visitor visitor) {
visitor.visitElement(this);
}
public void operation() {
//
}
}
class Clientd {
public static void main() {
Elementd elm = new CElementd();
Visitor vis = new CVisitor();
vis.visitElement(elm);
}
}
//Iteraotr
//
interface Structure {
interface Iteratora {
void first();
boolean hasElement();
Object next();
}
}
class Structure1 implements Structure {
Object[] objs = new Object[100];
// Struture1
class Iteratora1 implements Iteratora {
int index = 0;
public void first() {
index = 0;
}
public boolean hasElement() {
return index < 100;
}
public Object next() {
Object obj = null;
if (hasElement()) {
obj = objs[index];
index++;
}
return obj;
}
}
}
//Meditor
class A1 {
public void operation1() {}
public void operation2() {}
}
class A2 {
public void operation1() {}
public void operation2() {}
}
class Mediator {
A1 a1;
A2 a2;
public Mediator(A1 a1, A2 a2) {
this.a1 = a1;
this.a2 = a2;
}
// A1
// , A1 A2 ,
// Mediator
public void mmed1() {
a1.operation1();
a2.operation2();
}
public void mmed2() {
a2.operation1();
a1.operation2();
}
}
//Command
//
class Receiver {
public void action1() {}
public void action2() {}
}
interface Command {
void Execute();
}
class CCommand1 implements Command {
private Receiver receiver;
public CCommand1(Receiver receiver) {
this.receiver = receiver;
}
public void Execute() {
receiver.action1();
}
}
class CCommand2 implements Command {
private Receiver receiver;
public CCommand2(Receiver receiver) {
this.receiver = receiver;
}
public void Execute() {
receiver.action2();
}
}
//Observer
//
// Subject, Subject
// , Observer , ,
//Observer Visitor ,
//Subject Observer
interface Subjectb {
void attach(Observer observer);
void detach(Observer observer);
void mynotify();
int getState();
void setState(int state);
}
class Subjectb1 implements Subjectb {
List observers = new ArrayList();
int state;
public void attach(Observer observer) {
observers.add(observer);
}
public void detach(Observer observer) {
observers.remove(observer);
}
public void mynotify() {
Observer observer = null;
Iterator it = observers.iterator();
while (it.hasNext()) {
observer = (Observer) it.next();
observer.Update();
}
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
interface Observer {
void Update();
}
class Observer1 implements Observer {
Subjectb subject;
int state;
public Observer1(Subjectb subject) {
this.subject = subject;
}
public void Update() {
this.state = subject.getState();
}
public void operation() {
// state
}
}
//Memento
//
class Memento {
int state;
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
class Originator {
int state;
public void setMemento(Memento memento) {
state = memento.getState();
}
public Memento createMemento() {
Memento memento = new Memento();
memento.setState(1);
return memento;
}
public int getState() {
return state;
}
public void setState(int state) {
this.state = state;
}
}
class careTaker {
Memento memento;
public void saverMemento(Memento memento) {
this.memento = memento;
}
public Memento retrieveMemento() {
return memento;
}
}
// ,
// ,
// ,
// , ,
// , ,
// ,
public class tt1 {
public static void main(String[] args) {
}
}