欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

Factory Method Pattern

-----------------------------------------------------------------------

Everyday Example - Calendar
Factory Method Pattern

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

Factory Method Pattern

Factory Method Pattern

工厂static方法

Factory Method Pattern

Blog, Shop子类

Factory Method Pattern

Factory Method Pattern

Website抽象父类
Factory Method Pattern

ContactPage, CarPate子类

Factory Method Pattern

Page父类

Factory Method Pattern

执行结果:

Factory Method Pattern

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

相关标签: java 设计模式