策略模式、代理模式、工厂方法模式、门面模式

策略模式

/**
 * @Desc 1、妙计接口
 */
public interface IStrategy {
	
	void operate();
}
/**
 * @Desc 2、第一个锦囊妙计
 */
public class FirstStrategy implements IStrategy{

	@Override
	public void operate() {
		System.out.println("打开第一个锦囊妙计");
	}
}
/**
 * @Desc 3、第二个锦囊妙计
 */
public class SecondStrategy implements IStrategy{

	@Override
	public void operate() {
		System.out.println("打开第二个锦囊妙计");
	}
}
/**
 * @Desc 4、第三个锦囊妙计
 */
public class ThirdStrategy implements IStrategy{
	
	@Override
	public void operate() {
		System.out.println("打开第三个锦囊妙计");
	}
}
/**
 * @Desc 5、锦囊
 */
public class Context {
	
	private IStrategy strategy;
	
	public Context(IStrategy strategy) {
		this.strategy = strategy;
	}
	
	public void operate() {
		strategy.operate();
	}
}
/**
 * @Desc 6、执行者
 *
 * 模拟逻辑: 锦囊装有妙计,执行人执行妙计
 */
public class Executor {

	public static void main(String[] args) {
		Context context = null;

		context = new Context(new FirstStrategy());
		context.operate(); // 打开第一个锦囊妙计

		context = new Context(new SecondStrategy());
		context.operate(); // 打开第二个锦囊妙计

		context = new Context(new ThirdStrategy());
		context.operate(); // 打开第三个锦囊妙计
	}
}

代理模式

/**
 * @Desc 1、公司接口
 */
public interface Company {

	void developSoftware();
}
/**
 * @Desc 2、产品公司
 */
public class ProductCompany implements Company {

	private Company compay;

	public ProductCompany(Company company) {
		this.compay = company;
	}

	@Override
	public void developSoftware() {
		this.compay.developSoftware();
	}
}
/**
 * @Desc 3、软件公司
 */
public class SoftwareCompany implements Company {

	@Override
	public void developSoftware() {
		System.out.println("软件公司开发软件。。。");
	}

}

/**
 * @Desc 4、客户
 *
 * 模拟逻辑:客户找软件公司(SoftwareCompany)开发软件,但软件公司不会设计,需要产品公司(ProductCompany)代理。
 */
public class Customer {

	public static void main(String[] args) {
		ProductCompany productCompany = new ProductCompany(new SoftwareCompany());
		productCompany.developSoftware(); // 软件公司开发软件。。。
	}
}

工厂方法模式

/**
 * 1、人类
 */
public interface Human {
    /**
     * 人类会笑
     */
    void laugh();
    /**
     * 人类会哭
     */
    void cry();
}
/**
 * 2、黄种人
 */
public class YellowHuman implements Human {

    @Override
    public void laugh() {
        System.out.println("黄种人会笑");
    }

    @Override
    public void cry() {
        System.out.println("黄种人会哭");
    }
}
/**
 * 3、白种人
 */
public class WhiteHuman implements Human {

    @Override
    public void laugh() {
        System.out.println("白种人会笑");
    }

    @Override
    public void cry() {
        System.out.println("白种人会哭");
    }
}
/**
 * 4、女娲造人工厂
 */
public class HumanFactory {
    public static Human createHuman(Class c) {
        Human human = null;
        try {
            // 产生一个人类
            human = (Human) Class.forName(c.getName()).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return human;
    }
}

/**
 * 5、女娲造人
 *
 * 模拟逻辑:女娲造人
 */
public class NvWa {

    public static void main(String[] args) {
        Human human = HumanFactory.createHuman(YellowHuman.class);
        human.laugh(); // 黄种人会笑
        human.cry(); // 黄种人会哭

        human = HumanFactory.createHuman(WhiteHuman.class);
        human.laugh(); // 白种人会笑
        human.cry(); // 白种人会哭
    }
}

门面模式

/**
 * @Desc 1、研究员
 */
public class Researcher {
	/**
	 * 写论文
	 */
	public void writeThesis() {
		System.out.println("写论文......");
	}
	/**
	 * 发表论文
	 */
	public void publishThesis() {
		System.out.println("发表论文......");
	}
}
/**
 * @Desc 2、指导老师
 */
public class Supervisor {
	/**
	 * 指导写论文
	 */
	public void guideThesis() {
		System.out.println("指导写论文......");
	}
	/**
	 * 检查论文
	 */
	public void checkThesis() {
		System.out.println("检查论文......");
	}
}

/**
 * @Desc 3、将论文发表过程封装
 */
public class ThesisProcess {

	private Researcher researcher = new Researcher();
	
	private Supervisor supervisor = new Supervisor();
	
	public void publishThesis() {
		// 研究员写论文
		researcher.writeThesis();
		// 指导老师指导写论文
		supervisor.guideThesis();
		// 指导老师指检查论文
		supervisor.checkThesis();
		// 研究员发表论文
		researcher.publishThesis();
	}
}
/**
 * @Desc 4、学院发表论文
 *
 *  模拟逻辑:论文发表过程。学院发表论文有4个步骤,还要知道这四个步骤的顺序,整个过程比较复杂。
 *  将发布论文的过程封装,新增了一个门面,直接调用门面的方法就可以,不用了解具体的实现方法以及相关的业务顺序。
 */
public class College {

	public static void main(String[] args) {
		ThesisProcess thesisProcess = new ThesisProcess();
		thesisProcess.publishThesis(); 
		// 写论文......
		// 指导写论文......
		// 检查论文......
		// 发表论文......
	}
}

观察者模式

/**
 * @Desc 1、李斯,观察者
 */
public class LiSi implements Observer {

	@Override
	public void update(Observable o, Object arg) {
		System.out.println("LiSi汇报:" + arg);
	}
}
/**
 * @Desc 2、赵高,观察者
 */
public class ZhaoGao implements Observer {

	@Override
	public void update(Observable o, Object arg) {
		System.out.println("ZhaoGao报告:" + arg);
	}
}
/**
 * @Desc 3、韩非子,被观察者
 */
public class HanFeiZi extends Observable {

	public void activity(String name) {
		super.setChanged();
		super.notifyObservers("HanFeiZi去了" + name + "!");
	}
}
/**
 * @Desc 4、观察者模式
 */
public class Client {

	public static void main(String[] args) {
		HanFeiZi hfz = new HanFeiZi();

		hfz.addObserver(new LiSi());
		hfz.addObserver(new ZhaoGao());

		hfz.activity("紫兰轩");
		// ZhaoGao报告:HanFeiZi去了紫兰轩!
		// LiSi汇报:HanFeiZi去了紫兰轩!
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值