package methodOfFactory;
public class People
{
private String name;
private String sex;
public People(){}
public People(String name,String sex)
{
this.name = name;
this.sex = sex;
}
public void show(){}
public String toString()
{
return "Name: "+name+" Sex: "+sex;
}
}
package methodOfFactory;
public class Boy extends People {
public Boy(){}
public Boy(String name)
{
super(name,"male");
}
public void show(){}
}
package methodOfFactory;
public class ZhangSan extends Boy {
public ZhangSan()
{
super("zhang san");
}
public void show()
{
System.out.println("zhang san is my dota friend!");
}
}
package methodOfFactory;
public class Girl extends People {
public Girl(){}
public Girl(String name)
{
super(name,"male");
}
public void show(){}
}
package methodOfFactory;
public class LiSi extends Girl {
public LiSi()
{
super("li si");
}
public void show()
{
System.out.println("li si is my girl friend!");
}
}
package methodOfFactory;
public class WangWu extends Girl
{
public WangWu()
{
super("wang wu");
}
public void show()
{
System.out.println("wang wu is my wife!");
}
}
package methodOfFactory;
public interface Factory {
public abstract People getPeople(String name);
}
package methodOfFactory;
public class BoyFactory implements Factory {
@Override
public People getPeople(String name) {
if(name.equalsIgnoreCase("zhangsan"))
{
return new ZhangSan();
}
return null;
}
}
package methodOfFactory;
public class GirlFactory implements Factory
{
@Override
public People getPeople(String name)
{
if(name.equalsIgnoreCase("lisi"))
{
return new LiSi();
}else if(name.equalsIgnoreCase("wangwu"))
{
return new WangWu();
}
return null;
}
}
package methodOfFactory;
public class Main
{
public static void main(String[] args)
{
Factory f = new BoyFactory();
Boy b = (Boy) f.getPeople("zhangsan");
System.out.println(b);
b.show();
Factory fa = new GirlFactory();
Girl g = (Girl) fa.getPeople("lisi");
System.out.println(g);
g.show();
Girl gg = (Girl) fa.getPeople("wangwu");
System.out.println(gg);
gg.show();
}
}
/*
*工厂方法模型就要解决的是有多个工厂,每一个工厂生产特定产品,都是在工厂里面产生对象
*以后凡是要增加其他类型的产品的时候就可以新建一个工厂继承父工厂,然后在新建产品类就
*行,这样很好的体现了Java的一个设计原则,开闭原则
* */
分享到:
相关推荐
The following class diagram represents a design in factory method pattern to query the features of different types of auto insurances. See the source code for the implementation of the following class...
Factory Method Abstract Factory Builder Prototype Singleton 结构型 Adapter Bridge Composite Decorator Facade Flyweight Proxy 行为型 Interpreter Template Method Chain of Responsibility ...
如:Singleton、AbstractFactory、Factory Method、Builder、Prototype。 行为型模式涉及到类和对象如何交互及分配职责。如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、...
• Factory Method • Prototype • Singleton • Discussion of Creational Patterns Structural Patterns • Adapter • Bridge • Composite • Decorator • Facade • Flyweight • Proxy • ...
Factory Method Creates an instance of several derived classes Prototype A fully initialized instance to be copied or cloned Singleton A class of which only a single instance can exist ...
It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on ...
如:Singleton、AbstractFactory、Factory Method、Builder、Prototype。 行为型模式涉及到类和对象如何交互及分配职责。如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、...
如:Singleton、AbstractFactory、Factory Method、Builder、Prototype。 行为型模式涉及到类和对象如何交互及分配职责。如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、...
★第1章至第11章陆续介绍了设计模式:Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、TemplatMethod、Iterator、Composite、State、Proxy。 ★第12章介绍...
Chapter 4: Factory Method Chapter 5: Abstract Factory Chapter 6: Builder Chapter 7: Singleton Chapter 8: Adapter Chapter 9: Bridge Chapter 9: Bridge Chapter 11: Mediator Chapter 12: Observer Chapter ...
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.interceptor.TransactionInterceptor#0': Error setting property values; nested ...
■Chapter 3: Creational Patterns: Singleton, Factory Method, and Prototype ■Chapter 4: Creational Patterns: Abstract Factory and Builder ■Chapter 5: Structural Patterns: Adapter, Bridge, Composite,...
It starts with a general introduction to all types of programming patterns and goes on to describe 10 of the most popular design patterns in detail: Singleton, Iterator, Adapter, Decorator, State, ...
Design Pattern: Factory Method 模式 Design Pattern: Prototype 模式 Design Pattern: Singleton 模式 Design Pattern: Registry of Singleton 模式 Design Pattern: Default Adapter 模式 31 Design Pattern:...
Factory Method Object Pool Prototype Singleton Structural patterns Adapter Bridge Composite Decorator Facade Flyweight Private Class Data Proxy Behavioral patterns Chain of Responsibility Command ...
FactoryMethod ( 工厂方法 ) Singleton ( 单态模式 ) Builder ( 建造者模式 ) Prototype ( 原型模式 ) Adapter ( 适配器模式 ) Bridge ( 桥接模式 ) Composite ( 组合模式 ) Decorator ( 装饰模式 ) Facade ...
Chapter 12: Factory Method Patterns Chapter 13: Memento Patterns Chapter 14: State Patterns Chapter 15: Builder Patterns Chapter 16: Flyweight Patterns Chapter 17: Abstract Factory Patterns Chapter 18...
1. 面向对象综述 ...18. Strategy、State、Template Method、Command、Chain of Responsibility、Observer、Mediator 19. ,Interpreter、Iterator、Memento、Visitor、Collecting Parameter 20. 总结
Each example is treated in depth, with the whys and wherefores of the chosen method of solution critically examined. Part of the book is devoted to designing re-usable components that are then put ...
Invalid property 'error_view' of bean class [com.demo.controller.action.AuthorAction]: Bean property 'error_view' is not writable or has an invalid setter method. Does the parameter type of the ...