Factory Method Pattern
程序员文章站
2022-03-14 13:49:26
...
Concepts
- Doesn't expose instantiation logic
- Defer to subclasses
- Common interface
- Specified by architecture, implemented by user
- Examples:
Calendar, ResourceBundle, NumberFormat
-----------------------------------------------------------
Design
- Factory is responsible for lifecycle
- Common Interface
- Concrete Classes
- Parameterized create method
-----------------------------------------------------------------------
Everyday Example - Calendar
package com.pattern.factory;
import java.util.Calendar;
public class FactoryEverydayDemo {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println(cal);
System.out.println(cal.get(Calendar.DAY_OF_MONTH));
}
}
----------------------------------------------------
Exercise Factory
- Create Pages
- Create Website
- Create Concrete Classes
- Create Factory
- Enum
工厂static方法
Blog, Shop子类
Website抽象父类
ContactPage, CarPate子类
Page父类
执行结果:
package com.pattern.factory;
public class FactoryDemo {
public static void main(String[] args) {
Website site = WebsiteFactory.getWebsite(WebsiteType.BLOG);
System.out.println(site.getPages());
site = WebsiteFactory.getWebsite(WebsiteType.SHOP);
System.out.println(site.getPages());
}
}
package com.pattern.factory;
public class WebsiteFactory {
public static Website getWebsite(WebsiteType siteType) {
switch (siteType) {
case BLOG: {
return new Blog();
}
case SHOP: {
return new Shop();
}
default:
return null;
}
}
}
package com.pattern.factory;
public class Blog extends Website {
@Override
public void createWebsite() {
pages.add(new ContactPage());
}
}
package com.pattern.factory;
import java.util.ArrayList;
import java.util.List;
public abstract class Website {
protected List<Page> pages = new ArrayList<>();
public List<Page> getPages() {
return pages;
}
public Website() {
this.createWebsite();
}
public abstract void createWebsite();
}
-------------------------------------------------
Pitfalls
- Complexity
- Creation in subclass
- Refactoring
----------------------------------------------------
Contrast
Singleton
- Returns same instance(One constructor method - no args)
- No Interface
- No subclasses
Factory
- Returns various instances(Multiple constructors)
- Interface driven
- Subclasses
- Adaptable to environment more easily
--------------------------------------------------------------
Factory Summary
- Parameter Driven
- Solves complex creation
- A little complex
- Opposite of a Singleton
-----------------------------------------------------------
AbstractFactory Pattern
https://blog.csdn.net/u012596785/article/details/79704008
推荐阅读
-
php采用curl访问域名返回405 method not allowed提示的解决方法
-
一些常用的HTML5模式(pattern) 总结
-
PHP设计模式之工厂模式(Factory)入门与应用详解
-
PHP设计模式之中介者模式(Mediator Pattern)入门与应用案例详解
-
php示例详解Constructor Prototype Pattern 原型模式
-
php判断某个方法是否存在函数function_exists (),method_exists()与is_callable()区别与用法解析
-
innodb_flush_method取值方法(实例讲解)
-
C#基于Extension Method(扩展方法)获得文件大小的方法
-
PHP设计模式(一)工厂模式Factory实例详解【创建型】
-
golang基础--method方法