除了洗衣服,这次加上看电视的功能了
1.洗衣服有IWash接口和两个实现类
2.看电视也有ITv接口和两个实现类
3.工厂方面首先是一个工厂接口,然后定义一个实现类,通过读取xml文件反射生成类,这样以后洗衣服和看电视具体实现改变时无须修改工厂类,只需要修改xml文件
java代码如下
beans.xml
<?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="滚筒洗衣机" class="com.liu.pattern3.GTWasher"></bean> <bean id="波轮洗衣机" class="com.liu.pattern3.BLWasher"></bean> <bean id="国产电视" class="com.liu.pattern3.GCTv"></bean> <bean id="进口电视" class="com.liu.pattern3.JKTv"></bean> </beans>IFactory
package com.liu.pattern3;//抽象工厂public interface IFactory { public IWasher createWasher(String name); public ITv createTv(String name);}MyFactory
package com.liu.pattern3;public class MyFactory implements IFactory { PRivate ClassPathXmlLoader loader = new ClassPathXmlLoader(); @Override public IWasher createWasher(String name) { return (IWasher)loader.getBean(name); } @Override public ITv createTv(String name) { return (ITv)loader.getBean(name); }}ClassPathXmlLoader
package com.liu.pattern3;import java.io.IOException;import java.util.HashMap;import java.util.List;import org.jdom2.Document;import org.jdom2.Element;import org.jdom2.JDOMException;import org.jdom2.input.SAXBuilder;public class ClassPathXmlLoader { private HashMap<String,Object> beans; public Object getBean(String name){ return beans.get(name); } public ClassPathXmlLoader(){ //Load xml File SAXBuilder builder = new SAXBuilder(); beans = new HashMap<String, Object>(); try { Document doc = builder.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); Element root = doc.getRootElement(); List<Element> list = root.getChildren("bean"); for(int i=0;i<list.size();i++){ Element bean = list.get(i); String id = bean.getAttributeValue("id"); String name = bean.getAttributeValue("class"); Object o = Class.forName(name).newInstance(); beans.put(id,o); } } catch (Exception e) { e.printStackTrace(); } }}IWasher
package com.liu.pattern3;public interface IWasher { public void wash();}BLWasher
package com.liu.pattern3;public class BLWasher implements IWasher { @Override public void wash() { System.out.println("波轮洗衣机洗衣服了"); }}GTWasher
package com.liu.pattern3;public class GTWasher implements IWasher { @Override public void wash() { System.out.println("滚筒洗衣机洗衣服了"); }}ITv
package com.liu.pattern3;public interface ITv { public void see();}JKTv
package com.liu.pattern3;public class JKTv implements ITv { @Override public void see() { System.out.println("看进口电视"); }}GCTv
package com.liu.pattern3;public class GCTv implements ITv { @Override public void see() { System.out.println("看国产电视"); }}Test
package com.liu.pattern3;public class Test { public static void main(String[] args) { IFactory factory = new MyFactory(); IWasher washer = factory.createWasher("波轮洗衣机"); washer.wash(); ITv tv = factory.createTv("国产电视"); tv.see(); }}
C#实现代码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml;using System.Collections;using System.Reflection;namespace com.liu.pattern3{ public class ClassPathXmlLoader { private Hashtable beans; public ClassPathXmlLoader(){ beans = new Hashtable(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load("beans.xml"); XmlNode root = xmlDoc.SelectSingleNode("beans"); XmlNodeList list = root.SelectNodes("bean"); for (int i = 0; i < list.Count; i++) { XmlNode node = list.Item(i); String id = node.Attributes["id"].Value; String name = node.Attributes["class"].Value; Object o = Assembly.Load("AbstractFactory").CreateInstance(name); beans.Add(id, o); } } public Object GetBean(String name){ return beans[name]; } } public interface IWasher { void Wash(); } public class GTWasher : IWasher { public void Wash() { System.Console.WriteLine("滚筒洗衣机洗衣服了"); } } public class BLWasher : IWasher { public void Wash() { System.Console.WriteLine("波轮洗衣机洗衣服了"); } } public interface ITv { void See(); } public class GCTv:ITv { public void See() { System.Console.WriteLine("看国产电视"); } } public class JKTv:ITv { public void See() { System.Console.WriteLine("看进口电视"); } } public interface IFactory { IWasher CreateWasher(String name); ITv CreateTv(String name); } public class MyFactory : IFactory { private ClassPathXmlLoader loader = new ClassPathXmlLoader(); public IWasher CreateWasher(String name) { return (IWasher)loader.GetBean(name); } public ITv CreateTv(String name) { return (ITv)loader.GetBean(name); } } class Test { static void Main(string[] args) { MyFactory factory = new MyFactory(); IWasher washer = factory.CreateWasher("滚筒洗衣机"); washer.Wash(); ITv tv = factory.CreateTv("进口电视"); tv.See(); System.Console.ReadKey(); } }}
新闻热点
疑难解答