设计模式简单小例子(二)结构型模式

简单小例子

源代码已经上传到了GitHub.

https://github.com/tanglonghui/DesignPatterns

设计模式简单小例子(一) 创建型模式:

https://blog.csdn.net/qq_40687864/article/details/81064917

设计模式简单小例子(三)行为型模式:

https://blog.csdn.net/qq_40687864/article/details/82148887

一、适配器模式

package StructuralPatterns.Adapter;



/*
 * 适配器模式(有时候也称包装样式或者包装)将一个类的接口适配成用户所期待的。
 * 一个适配允许通常因为接口不兼容而不能在一起工作的类工作在一起,
 * 做法是将类自己的接口包裹在一个已存在的类中。
 */
/*
 * 包含角色:
 * Target(目标抽象类)
 * Adapter(适配器类)
 * Adaptee(适配者类)
 * 简单说:
 * 使Target通过Adapter(适配器类)适配后可以自己类中使用 Adaptee(适配者类)中的方法
 * 我们假设target保存Stirng类型的数据的类;
 * 而Adaptee是用int保存
 */
class Test {
	public static void main(String[] args) {
		//
		Adaptee adaptee=new Adaptee();
		adaptee.msg=11;
		Target target=new Adapter(adaptee);
		target.show();
		System.out.println(""+target.msg.getClass());
		
	}
	

}
abstract class Target{
	 String msg;
	 void show() {
		 System.out.println("我展示的是:"+msg);
		 
	}
}
class Adaptee{
	int msg;
	void show() {
		System.out.println("我展示的是:"+msg);
	}
	
}
class Adapter extends  Target{
	Adaptee adaptee;
	public Adapter(Adaptee adaptee) {
		// TODO Auto-generated constructor stub
		this.adaptee=adaptee;
	}
	
	@Override
	void show() {
		// TODO Auto-generated method stub
		//适配器做操作将String类型数据变成 int 类型
		super.msg=String.valueOf(adaptee.msg);
		super.show();
	}
	
}

二、桥接模式

package StructuralPatterns.Bridge;
/*
 * 桥接模式是将抽象部分与它的实现部分分离,使它们都可以独立地变化。
 * 它是一种对象结构型模式,又称为柄体(Handle and Body)模式或接口(Interface)模式。
 */
/*
 * 包含角色:
 * Abstraction(抽象类) 抽象   Color
 * RefinedAbstraction(扩充抽象类) 实例   Red Yellow Green
 * Implementor(实现类接口)  抽象    Shape
 * ConcreteImplementor(具体实现类 ) 实例  A B C
 * 
 * 我们用形状和颜色来做个例子 形状我就假设ABC了
 */
public class Test {
	public static void main(String[] args) {
		Shape shape;
		shape=new A();
		shape.draw(new Red());
		shape.draw(new Green());
	}

}
interface Color{
	<T extends Shape> void paint(T t);
}
class Red implements Color{
   private String msg="red";

@Override
	public <T extends Shape> void paint(T t) {
		// TODO Auto-generated method stub
			System.out.println("形状:"+t.getShape()+"  颜色:"+msg);
	}
}
class Green implements Color{
	   private String msg="green";

	@Override
		public <T extends Shape> void paint(T t) {
			// TODO Auto-generated method stub
				System.out.println("形状:"+t.getShape()+"  颜色:"+msg);
		}
	}


interface  Shape{
	String getShape();
	void draw(Color color);
	
}
class A implements Shape{
	private Color color;
	private String shape="A";
	@Override
	public String getShape() {
		// TODO Auto-generated method stub
		return shape;
	}

	@Override
	public void draw(Color color) {
		// TODO Auto-generated method stub
		this.color=color;
		color.paint(this);
	}
	
}
class B implements Shape{
	private Color color;
	private String shape="A";
	@Override
	public String getShape() {
		// TODO Auto-generated method stub
		return shape;
	}

	@Override
	public void draw(Color color) {
		// TODO Auto-generated method stub
		this.color=color;
		color.paint(this);
	}
	
}



三、组合模式

package StructuralPatterns.Composite;

import java.util.ArrayList;

/*
 * 组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,
 * 组合模式使得用户对单个对象和组合对象的使用具有一致性。
 * 掌握组合模式的重点是要理解清楚 “部分/整体” 还有 ”单个对象“ 与 "组合对象" 的含义。
 */
/*
 * 包含角色:
 * Component(抽象构件)
 * Leaf(叶子构件)
 * Composite(容器构件)
 */
public class Test {
	public static void main(String[] args) {
		Conposite c1=new Conposite();
		Conposite c2=new Conposite();
		Conposite c3=new Conposite();
		Conposite c4=new Conposite();
		Leaf1 l1=new Leaf1();
		Leaf2 l2=new Leaf2();
		Leaf1 l3=new Leaf1();
		Leaf1 l4=new Leaf1();
		c1.add(c2);
		c1.add(c4);
		c1.add(l3);
		c2.add(c3);
		c3.add(l1);
		c3.add(l2);
		c4.add(l4);
		//c1.operation();
		c4.operation();
		
		
	}
}
interface Component{
	void add(Component component);
	void remove(Component component);
	Component getChild(int i);
	void operation();
}
class Conposite implements Component{
	private ArrayList<Component> list=new ArrayList<>();

	@Override
	public void add(Component component) {
		// TODO Auto-generated method stub
		list.add(component);
	}

	@Override
	public void remove(Component component) {
		// TODO Auto-generated method stub
		list.remove(component);
	}

	@Override
	public Component getChild(int i) {
		// TODO Auto-generated method stub
		return list.get(i);
	}

	@Override
	public void operation() {
		// TODO Auto-generated method stub
		for(Object object:list) {
			((Component)object).operation();
		}
	}
	
}
class Leaf1 implements Component{
// 因叶子构件不再包含子构件 ,所以前三个方法应该包含错误提示;
	@Override
	public void add(Component component) {
		// TODO Auto-generated method stub
		System.out.println("叶子构件无法添加子构件");
	}

	@Override
	public void remove(Component component) {
		// TODO Auto-generated method stub
		System.out.println("叶子构件无子构件");
	}

	@Override
	public Component getChild(int i) {
		// TODO Auto-generated method stub
		System.out.println("叶子构件无子构件");
		return null;
	}

	@Override
	public void operation() {
		// TODO Auto-generated method stub
		System.out.println("子构件1");
	}
	
}
class Leaf2 implements Component{
	// 因叶子构件不再包含子构件 ,所以前三个方法应该包含错误提示;
		@Override
		public void add(Component component) {
			// TODO Auto-generated method stub
			System.out.println("叶子构件无法添加子构件");
		}

		@Override
		public void remove(Component component) {
			// TODO Auto-generated method stub
			System.out.println("叶子构件无子构件");
		}

		@Override
		public Component getChild(int i) {
			// TODO Auto-generated method stub
			System.out.println("叶子构件无子构件");
			return null;
		}

		@Override
		public void operation() {
			// TODO Auto-generated method stub
			System.out.println("子构件2");
		}
		
	}

四、装饰模式

package StructuralPatterns.Decorator;
/*
 * 装饰模式指的是在不必改变原类文件和使用继承的情况下,
 * 动态地扩展一个对象的功能。
 * 它是通过创建一个包装对象,
 * 也就是装饰来包裹真实的对象。
 */
/*
 * 包含角色:
 * Component(抽象构件类)
 * ConcreteComponent(具体构件类)
 * Decorator(抽象装饰类)
 * ConcreteDecorator(具体装饰类)
 * 简单说:就是具体装饰类接收一个具体构件为其增加功能。
 */
public class Test {
public static void main(String[] args) {
	ConcreteComponent cc=new ConcreteComponent();
	ConcreteDecorator1 decorator=new ConcreteDecorator1(cc);
	decorator.functionA();
	decorator.functionB();
	ConcreteDecorator2 c2=new ConcreteDecorator2(decorator);
	c2.functionC();
}
}
interface Component{
	void functionA();
}
class ConcreteComponent implements Component{

	@Override
	public void functionA() {
		// TODO Auto-generated method stub
		System.out.println("我是基础功能A");
	}
	
}
abstract class Decorator implements Component{
	private Component component;
	public Decorator(Component component) {
		// TODO Auto-generated constructor stub
		this.component=component;
	}
	@Override
	public void functionA() {
		// TODO Auto-generated method stub
		component.functionA();
	}
	
}
class ConcreteDecorator1 extends Decorator{

	public ConcreteDecorator1(Component component) {
		super(component);
		// TODO Auto-generated constructor stub
	}
	void functionB() {
		System.out.println("我是新增功能B");
	}
}
class ConcreteDecorator2 extends Decorator{

	public ConcreteDecorator2(Component component) {
		super(component);
		// TODO Auto-generated constructor stub
	}
	void functionC() {
		System.out.println("我是新增功能C");
	}
}

五、外观模式

package StructuralPatterns.Facade;
/*
 * 外观模式(Facade),亦称“过程模式”。学校课程评价模式之一。
 * 美国教育学者斯泰克1967 年在所著《教育评价的外观》中提出。
*按照描述和判断资料来评价课程,关键的活动是在课程实施的全过程中进行观察和搜集意见,以了解人们对课程的不同看法。
*这种模式不限于检查教学的成果,重视描述和判断教学过程中各种复杂、动态的现象和事物。
*/
/*
 * 包含角色:
 * Facade(外观角色)
 * SubSystem(子系统角色)
 * 简单说:通过Facade 实现 子系统方法的调用,类似于总的控制器。
 * 
 */
public class Test {
public static void main(String[] args) {
	Facade facade=new Facade();
	facade.control1();
	facade.control2();
}
}
class Facade{
	SubSystem1 s1=new SubSystem1();
	SubSystem2 s2=new SubSystem2();
	//在这里可以自定义组合功能键
	void control1(){
		s1.on();
		s2.move();
	}
	void control2() {
		s1.off();
		s2.say();
	}
}
class SubSystem1{
	void on() {
		System.out.println("灯开了");
	}
	void off() {
		System.out.println("灯关了");
	}
}
class SubSystem2{
	void move() {
		System.out.println("移动");
	}
	void say() {
		System.out.println("说话");
	}
}

六、享元模式

package StructuralPatterns.Flyweight;


import java.util.HashMap;

/*
 * 享元模式(Flyweight Pattern)主要用于减少创建对象的数量,
 * 以减少内存占用和提高性能。这种类型的设计模式属于结构型模式,
 * 它提供了减少对象数量从而改善应用所需的对象结构的方式。
 */
/*
 * 包含模式:
 * Flyweight(抽象享元类)
 * ConcreteFlyweight(具体享元类)  单例设计
 * UnsharedConcreteFlyWeight(非共享具体享元类) 非单例设计
 * FlyweightFactory(享元工厂类)ps:可以定义ArrayList等 它是一个享员池
 * ps:这东西这么看上去这么像是设计数据库连接池的东东。
 *   简单说:享元工厂类中有一个享元池,外部对象可以通过他获得具体享员类的对象 若享元池有,直接给,若没有,
 *   新建一个给它并把这个对象加入享元池。
 *   内部状态,可以相同无所谓
 * 外部状态,例如分配的端口号不一样需要提取出来
 */
public class Test {
	public static void main(String[] args) {
		FlyweightFactory factory=new FlyweightFactory();
		System.out.println("路由器最大序列号:"+factory.getLyCount()); 
		System.out.println("交换机最大序列号:"+factory.getJhjCount());
		Flyweight flyweight=factory.getDevice("路由器", 0);
		Flyweight flyweight1=factory.getDevice("路由器", 2);
		Flyweight flyweight2=factory.getDevice("路由器", 10);
		Flyweight flyweight3=factory.getDevice("交换机", 0);
		Port p=new Port();
		p.setPort("1");
		flyweight.Say(p);
		p.setPort("2");
		flyweight1.Say(p);
		System.out.println("路由器最大序列号:"+factory.getLyCount()); 
		System.out.println("交换机最大序列号:"+factory.getJhjCount());
	}
}
interface Flyweight{
	void Say(Port port);
	void move();
}
class Port{
	//提取出来的外部状态 
	private  String port;
	public String getPort() {
		return port;
	}
	public void setPort(String port) {
		this.port = port;
	}
}
class ConcreteFlyweight1 implements Flyweight{
	private String type;//内部状态标识码
	public ConcreteFlyweight1(String type) {
		// TODO Auto-generated constructor stub
		this.type=type;
	}
	void jump() {
		System.out.println("专属跳跃");
	}

	@Override
	public void Say(Port port) {
		// TODO Auto-generated method stub
		System.out.println("网络连接,路由器type:"+this.type+"  端口port:"+port.getPort());
	}

	@Override
	public void move() {
		// TODO Auto-generated method stub
		System.out.println("通用移动");
	}
	
}
class ConcreteFlyweight2 implements Flyweight{
	private String type;//内部状态标识码
	public ConcreteFlyweight2(String type) {
		// TODO Auto-generated constructor stub
		this.type=type;
	}
	void fly() {
		System.out.println("专属飞翔");
	}

	@Override
	public void Say(Port port) {
		// TODO Auto-generated method stub
		System.out.println("网络连接,路由器type:"+this.type+"  端口port:"+port.getPort());
	}

	@Override
	public void move() {
		// TODO Auto-generated method stub
		System.out.println("通用移动");
	}
	
}
class FlyweightFactory{
	//private ArrayList<Flyweight> device=new ArrayList<>();
	/*
	 * 这里用map应该舒服一些
	 */
	private HashMap<String, Flyweight> device=new HashMap<>();
	//设置一个指针
	private int lyCount;
	private int jhjCount;
	private Flyweight flyweight;
	public int getLyCount() {
		return lyCount-1;
	}
	public int getJhjCount() {
		return jhjCount-1;
	}
	public FlyweightFactory() {
		// TODO Auto-generated constructor stub
		lyCount=0;
		jhjCount=0;
		ConcreteFlyweight1 c1=new ConcreteFlyweight1("路由器"+lyCount);
		device.put("路由器"+lyCount++, c1);
		ConcreteFlyweight2 c2=new ConcreteFlyweight2("交换机1"+jhjCount);
		device.put("交换机"+jhjCount++, c2);
	}
	public Flyweight getDevice(String type,int count) {
		if((flyweight=device.get(type+count))!=null) {
			System.out.println("已找到,为你返回");
			return flyweight;
		}else {
			switch (type) {
			case "路由器":
				
				flyweight=new ConcreteFlyweight1(type+lyCount);
				device.put("路由器"+lyCount++, flyweight);
				System.out.println("无你指定的设备已经为你新建,序列号为:"+lyCount);
				return flyweight;
			
			case "交换机":
				flyweight=new ConcreteFlyweight2(type+jhjCount);
				device.put("交换机"+jhjCount++, flyweight);
				System.out.println("无你指定的设备已经为你新建,序列号为:"+jhjCount);
				return flyweight;

			default:
				return null;
			}
		}
		
	
		
	}
}



七、代理模式

package StructuralPatterns.Proxy;
/*
 * 在代理模式(Proxy Pattern)中,一个类代表另一个类的功能。
 * 这种类型的设计模式属于结构型模式。
 * 在代理模式中,我们创建具有现有对象的对象,以便向外界提供功能接口。
 */
/*
 * 包含角色:
 * Subject(抽象主题角色)
 * Proxy(代理主题角色)
 * RealSubject(真实主题角色)
 * 简单说:
 * 通过 Proxy 可以在一定程度上的操作 RealSubject
 */
public class Test {
	public static void main(String[] args) {
		Proxy proxy=new Proxy();
		/*
		 * 正常情况下需要设置权限的限制Proxy的使用范围,这里省略了
		 */
		proxy.move();
		proxy.jump();
	}
}
interface Subject{
	void move();
	void jump();
}
class RealSubject implements Subject{

	@Override
	public void move() {
		// TODO Auto-generated method stub
		System.out.println("移动");
	}

	@Override
	public void jump() {
		// TODO Auto-generated method stub
		System.out.println("跳");
	}
}
class Proxy implements Subject{
	private RealSubject r=new RealSubject();

	@Override
	public void move() {
		// TODO Auto-generated method stub
		r.move();
	}

	@Override
	public void jump() {
		// TODO Auto-generated method stub
		r.jump();
	}
	
}

参考资料:

《设计模式》 刘伟主编

  菜鸟教程 :http://www.runoob.com/

   百度百科

猜你喜欢

转载自blog.csdn.net/qq_40687864/article/details/81094414