{"action":"opened","issue":{"url":"https://api.github.com/repos/mySoul8012/code/issues/47","repository_url":"https://api.github.com/repos/mySoul8012/code","labels_url":"https://api.github.com/repos/mySoul8012/code/issues/47/labels{/name}","comments_url":"https://api.github.com/repos/mySoul8012/code/issues/47/comments","events_url":"https://api.github.com/repos/mySoul8012/code/issues/47/events","html_url":"https://github.com/mySoul8012/code/issues/47","id":387788020,"node_id":"MDU6SXNzdWUzODc3ODgwMjA=","number":47,"title":"设计模式-创建型模式-工厂模式（工厂三兄弟）","user":{"login":"mySoul8012","id":34295098,"node_id":"MDQ6VXNlcjM0Mjk1MDk4","avatar_url":"https://avatars1.githubusercontent.com/u/34295098?v=4","gravatar_id":"","url":"https://api.github.com/users/mySoul8012","html_url":"https://github.com/mySoul8012","followers_url":"https://api.github.com/users/mySoul8012/followers","following_url":"https://api.github.com/users/mySoul8012/following{/other_user}","gists_url":"https://api.github.com/users/mySoul8012/gists{/gist_id}","starred_url":"https://api.github.com/users/mySoul8012/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mySoul8012/subscriptions","organizations_url":"https://api.github.com/users/mySoul8012/orgs","repos_url":"https://api.github.com/users/mySoul8012/repos","events_url":"https://api.github.com/users/mySoul8012/events{/privacy}","received_events_url":"https://api.github.com/users/mySoul8012/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-12-05T14:42:04Z","updated_at":"2018-12-05T14:42:04Z","closed_at":null,"author_association":"OWNER","body":"设计模式-创建型模式-工厂模式（工厂三兄弟）\r\n工厂模式分为简单工厂，工厂方法，抽象工厂。\r\n# 简单工厂模式\r\n一个接口，三个具体类，一个工厂，通过选择，生产出对应的对象。\r\n```\r\npackage demo2;\r\n\r\npublic class Circle implements Shape{\r\n\tpublic Circle() {\r\n\t\t\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void draw() {\t\t\r\n\t}\r\n\t\r\n}\r\n\r\n```\r\n```\r\npackage demo2;\r\n\r\npublic class Rectangle implements Shape{\r\n\tpublic Rectangle() {\r\n\t\t\r\n\t}\r\n\t\r\n\t@Override\r\n\tpublic void draw() {\t\t\r\n\t}\r\n\r\n}\r\n\r\n```\r\n```\r\npackage demo2;\r\n\r\npublic interface Shape {\r\n\tvoid draw();\r\n}\r\n\r\n```\r\n```\r\npackage demo2;\r\n\r\npublic class ShapeFactory {\r\n\tpublic static Shape getShape(String shapeType) {\r\n\t\tif(shapeType == null) {\r\n\t\t\treturn null;\r\n\t\t}\r\n\t\t\r\n\t\tif (shapeType == \"Circle\") {\r\n\t\t\treturn new Circle();\r\n\t\t}\r\n\t\t\r\n\t\tif (shapeType == \"Rectangle\") {\r\n\t\t\treturn new Rectangle();\r\n\t\t}\r\n\t\t\r\n\t\tif (shapeType == \"Square\") {\r\n\t\t\treturn new Square();\r\n\t\t}\r\n\t\treturn null;\r\n\t}\r\n}\r\n\r\n```\r\n```\r\npackage demo2;\r\n\r\npublic class Square implements Shape{\r\n\tpublic Square() {\r\n\t\t\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void draw() {\t\t\r\n\t}\r\n}\r\n\r\n```\r\n```\r\npackage demo2;\r\n\r\npublic class Test {\r\n\tpublic static void main(String[] agrs) {\r\n\t\tShape circle = new ShapeFactory().getShape(\"Circle\");\r\n\t\tcircle.draw();\r\n\t}\r\n}\r\n\r\n```\r\n# 利用反射改进工厂模式\r\n由于违反开放-封闭原则，每次都要去修改源代码，所以直接使用反射机制。\r\n将类传入，使用泛型+反射，生产出对象。\r\n```\r\npackage demo2;\r\n\r\npublic class ShapeFactory2 {\r\n\t@SuppressWarnings(\"deprecation\")\r\n\tpublic static Object getClass(Class<? extends Shape> clazz) {\t// 代表通配的是Shape的子类，即都为借口的子类\r\n\t\tObject obj = null;\r\n\t\t\r\n\t\t\ttry {\r\n\t\t\t\tobj = Class.forName(clazz.getName()).newInstance();\r\n\t\t\t} catch (InstantiationException e) {\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t} catch (IllegalAccessException e) {\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t} catch (ClassNotFoundException e) {\r\n\t\t\t\te.printStackTrace();\r\n\t\t\t}\r\n\t\treturn obj;\r\n\t\t\r\n\t}\r\n}\r\n\r\n```\r\n```\r\npackage demo2;\r\n\r\npublic class Test {\r\n\tpublic static void main(String[] agrs) {\r\n\t\tShape circle = (Circle)ShapeFactory2.getClass(Circle.class);\r\n\t\tcircle.draw();\r\n\t}\r\n}\r\n\r\n```\r\n# 工厂方法\r\n每个对象有对应的工厂，将原先的工厂拆分。\r\n```\r\npackage demo2;\r\n\r\npublic class CircleFactory implements Factory{\r\n\r\n\t@Override\r\n\tpublic Shape getShape() {\r\n\t\treturn new Circle();\r\n\t}\r\n\r\n}\r\n\r\n```\r\n\r\n```\r\npackage demo2;\r\n\r\npublic class RectangleFactory implements Factory{\r\n\r\n\t@Override\r\n\tpublic Shape getShape() {\r\n\t\treturn new Rectangle();\r\n\t}\r\n\r\n}\r\n\r\n```\r\n```\r\npackage demo2;\r\n\r\npublic class SquareFactory implements Factory{\r\n\r\n\t@Override\r\n\tpublic Shape getShape() {\r\n\t\treturn new Square();\r\n\t}\r\n\r\n}\r\n\r\n```\r\n```\r\npackage demo2;\r\n\r\npublic class Test {\r\n\tpublic static void main(String[] agrs) {\r\n\t\tFactory circleFactory = new CircleFactory();\r\n\t\tShape circle = circleFactory.getShape();\r\n\t\tcircle.draw();\r\n\t}\r\n}\r\n\r\n```\r\n\r\n# 抽象工厂\r\n两个抽象接口，一个为产品，一个为工厂，进行生产\r\n## 产品\r\n枪\r\n```\r\npublic interface Gun{\r\n\tpublic void shooting();\r\n}\r\n```\r\n子弹\r\n```\r\npublic interface Bullet{\r\n\tpublic void load();\r\n}\r\n```\r\nak类\r\n```\r\npublic class AK implements Gun{\r\n\t@Override\r\n\tpublic void shooting(){};\r\n}\r\n```\r\n对应子弹\r\n```\r\npublic class AK_Bullet implements Bullet{\r\n\t@Override\r\n\tpublic void load(){};\r\n}\r\n```\r\nm4\r\n```\r\npublic class M4 implements Gun{\r\n\t@Override\r\n\tpublic void shooting(){};\r\n}\r\n\r\n```\r\n对应子弹\r\n```\r\npublic class M4_Bullet implements Bullet{\r\n\t@Override\r\n\tpublic void load(){};\r\n}\r\n```\r\n## 工厂\r\n```\r\npublic interface Factory{\r\n\tpublic Gun produceGun();\r\n\tpublic Bullet produceBullet();\r\n}\r\n```\r\n具体工厂\r\nak\r\n```\r\npublic class AK_Factory implements Factory {\r\n\t@Override\r\n\tpublic Gun produceGun(){\r\n\t\treturn new AK();\r\n\t}\r\n\t@Override\r\n\tpublic Bullet produceBullet(){\r\n\t\treturn new AK_Bullete();\r\n\t}\r\n}\r\n```\r\nM4\r\n```\r\npublic class M4_Factory implements Factory{\r\n\t@Override\r\n\tpublic Gun produceGun(){\r\n\t\treturn new M4();\r\n\t}\r\n\t@Override\r\n\tpublic Bullet produceBullet(){\r\n\t\treturn new M4_Bullet();\r\n\t}\r\n}\r\n```\r\nwww.iming.info"}}
{"actor":{"display_login":"mySoul8012"}}