3种常见的设计模式
工厂模式:
public interface Animal{public int sale();}
●定义三个具体实现类(数目随意)
//养猪public class Pig implements Animal{int price = 10;//价格int weight = 200;//重量public int sale(){return price * weight;}}//养鸡public class Chicken implements Animal{int price = 5;//价格int weight = 20;//重量int egg = 20;//鸡蛋public int sale(){return price * weight + egg;}}//养羊public class Sheep implements Animal{int price = 10;//价格int weight = 100;//重量int wool = 50;//羊毛public int sale(){return price * weight + wool;}}
public class Farm1{public Animal produce(String type){if("pig".equals(type)){return new Pig();}else if("chicken".equals(type)){return new Chicken();}else{return new Sheep();}}}
public class Farm2{public Animal producePig(){return new Pig();}public Animal produceChicken(){return new Chicken();}public Animal produceSheep(){return new Sheep();}}
public class Farm3{public static Animal producePig(){return new Pig();}public static Animal produceChicken(){return new Chicken();}public static Animal produceSheep(){return new Sheep();}}
/*** 单例模式-饿汉式单例 */public class EagerSingleton {public static void main(String[] args) {// 下面来判断一下有没有达到单例效果(系统运行的时候只出来一个空例)EagerSingleton eagerSingleton = EagerSingleton.getInstance();EagerSingleton eagerSingleton1 = EagerSingleton.getInstance();if (eagerSingleton == eagerSingleton1) {System.out.println("同一个对象实例");} else {System.out.println("不是同一个对象实例");}}/** 私有的(private)唯一(static final)实例成员,在类加载的时候就创建好了单例对象 */private static final EagerSingleton m_instance = new EagerSingleton();/*** 私有构造方法,避免外部创建实例 */private EagerSingleton() {} // 提供了一个空的构造方法/*** 静态工厂方法,返回此类的唯一实例.* @return EagerSingleton */public static EagerSingleton getInstance() {return m_instance;}}
import java.util.HashMap;import java.util.Map;/*** 单例模式- 登记式单例 */public class RegSingleton {/*** 登记薄,用来存放所有登记的实例 */private static Map<String, RegSingleton> m_registry = new HashMap();// 在类加载的时候添加一个实例到登记薄static {RegSingleton x = new RegSingleton();m_registry.put(x.getClass().getName(), x);}/*** 受保护的默认构造方法 */protected RegSingleton() {}/*** * 静态工厂方法,返回指定登记对象的唯一实例; 对于已登记的直接取出返回,对于还未登记的,先登记,然后取出返回 * * @param name * @return RegSingleton */public static RegSingleton getInstance(String name) {if (name == null) {name = "RegSingleton";}if (m_registry.get(name) == null) {try {m_registry.put(name, (RegSingleton) Class.forName(name).newInstance());} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}}return m_registry.get(name);}/** * 一个示意性的商业方法 * @return String */public String about() {return "Hello,I am RegSingleton!";}}
public class RealSubject extends Subject { public RealSubject() { } public void request() { System.out.println("From real subject."); } }
public class ProxySubject extends Subject { private RealSubject realSubject;//以真实角色作为代理角色的属性 public ProxySubject() { } //该方法封装了真实对象的request方法public void request() { preRequest(); if( realSubject == null ) { realSubject = new RealSubject(); } realSubject.request(); //此处执行真实对象的request方法 postRequest(); }private void preRequest() { //something you want to do before requesting } private void postRequest() { //something you want to do after requesting } } 客户端调用: Subject sub=new ProxySubject(); Sub.request();