标签:事件对象 list sig span stat ffffff == tin round
1.单例模式
是一种常用的软件设计模式,在它的核心结构中值包含一个被称为单例的特殊类。一个类只有一个实例,即一个类只有一个对象实例。
private static Singleton singleton;
public static Singleton getInstance() {
synchronized (Singleton.class) {
=============================================
2.工厂模式
package design.pattern;
public class MyFactory {
public static final int TYPE_MI = 1;// 大米
public static final int TYPE_YU = 2;// 油
public static final int TYPE_SC = 3;// 蔬菜
public static Food getFoods(int foodType) {
switch (foodType) {
case TYPE_MI:
return new DaMi();
case TYPE_YU:
return new You();
case TYPE_SC:
default:
return new ShuCai();
}
}
}
abstract class Food {
}
class DaMi extends Food {
}
class You extends Food {
}
class ShuCai extends Food {
}
=============================================
3.适配器模式
将一个类的接口转换成客户希望的另外一个接口。通俗地讲,就是在2个类之间做了一个衔接。比如你在调用A类的doSomething方法,实际上内部实现调用了B类的doSomething方法。
package design.pattern;
public class MyAdapter {
private MyAdapterImpl adapterImpl;
public MyAdapter(MyAdapterImpl myAdapterImpl) {
this.adapterImpl = myAdapterImpl;
}
public void doSomething() {
adapterImpl.doSomething();
}
public static void main(String args[]) {
new MyAdapter(new MyAdapterImpl()).doSomething();
}
}
class MyAdapterImpl {
public void doSomething() {
}
}
=============================================
4.代理模式
它的定义是:代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用。Java的反射机制,很多地方就用了代理模式来实现。
package design.pattern;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class DynamicProxyMain {
public static void main(String[] args) {
Person smallPerson = new Child();
Person proxyBuyHouse = (Person) Proxy.newProxyInstance(Person.class.getClassLoader(),new Class[] { Person.class }, new DynamicProxyHandler(smallPerson));
proxyBuyHouse.eat();
}
}
interface Person {
void eat();
}
class Child implements Person {
@Override
public void eat() {
// 小孩吃什么
System.out.println("eating");
}
}
class DynamicProxyHandler implements InvocationHandler {
private Object object;
public DynamicProxyHandler(final Object object) {
this.object = object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("调用前日志监控");
Object result = method.invoke(object, args);
System.out.println("调用后日志监控");
return result;
}
}
=============================================
5.监听模式
当事件源触发某种行为,会自动执行事件监听器里面相应的方法。Java监听模式右三个部分组成:事件源、事件对象、事件监听器。
package design.pattern;
public class ListenerTest {
public static void main(String[] args) {
MyListener myListener = new MyListener();
MyPerson person = new MyPerson();
person.addEatListener(myListener);
person.eat();
}
}
//事件源
class MyPerson{
private Listener listener;//监听器引用
//给事件源添加监听器
public void addEatListener(Listener listener) {
this.listener = listener;
}
public void eat() {
//吃了很多
if(listener != null) {
Event event=new Event() ;
event.setPerson(this);
listener.isThinking(event);//监听吃饭时,想了什么
}
}
}
//事件
class Event {
private MyPerson person;// 事件源的引用
public MyPerson getPerson() {
return person;
}
public void setPerson(MyPerson person) {
this.person = person;
}
}
interface Listener {
public void isThinking(Event event);
}
class MyListener implements Listener {
@Override
public void isThinking(Event event) {
System.out.println(" is isThinking many things !");
}
}
=============================================
6.装饰器模式
简单说就是不改变现有类的结构前提下,扩展它的功能。用别的类来增加原有类的功能
package design.pattern;
public class MyDecorator {
public static void main(String[] args) {
Animal animal= new SmallDog(new Pet());
animal.eat();
}
}
interface Animal {
public void eat();
}
class Pet implements Animal {
@Override
public void eat() {
System.out.println("eat food");
}
}
class Dog implements Animal{
protected Animal animal;
public Dog(Animal animal) {
this.animal = animal;
}
@Override
public void eat() {
this.animal.eat();
}
}
class SmallDog extends Dog{
public SmallDog(Animal animal) {
super(animal);
}
public void eat() {
System.out.println("eat other food ");
this.animal.eat();
}
}
标签:事件对象 list sig span stat ffffff == tin round
原文地址:https://www.cnblogs.com/-liwu/p/10836493.html