`
kinglun
  • 浏览: 40932 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

method of factory

阅读更多
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的一个设计原则,开闭原则
* */
分享到:
评论

相关推荐

    Homework 2.1-1.doc

    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...

    设计模式迷你手册.chm

    Factory Method Abstract Factory Builder Prototype Singleton 结构型 Adapter Bridge Composite Decorator Facade Flyweight Proxy 行为型 Interpreter Template Method Chain of Responsibility ...

    Head First Of Design高清中文版 1/3

    如:Singleton、AbstractFactory、Factory Method、Builder、Prototype。 行为型模式涉及到类和对象如何交互及分配职责。如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、...

    Design Patterns Elements of Reusable Object-Oriented Software

    • Factory Method • Prototype • Singleton • Discussion of Creational Patterns Structural Patterns • Adapter • Bridge • Composite • Decorator • Facade • Flyweight • Proxy • ...

    设计模式C++代码示例-含VC工程

    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 ...

    apache-tomcat-9.0.17.rar

    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 ...

    Head First Of Design高清中文版 2/3

    如:Singleton、AbstractFactory、Factory Method、Builder、Prototype。 行为型模式涉及到类和对象如何交互及分配职责。如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、...

    Head First Of Design中文高清版 3/3

    如:Singleton、AbstractFactory、Factory Method、Builder、Prototype。 行为型模式涉及到类和对象如何交互及分配职责。如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、...

    design patterns elements of reusable object-oriented software

    ★第1章至第11章陆续介绍了设计模式:Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、TemplatMethod、Iterator、Composite、State、Proxy。 ★第12章介绍...

    Objective C 设计模式(英文版)

    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.transaction-3.1.2.RELEASE.zip

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.interceptor.TransactionInterceptor#0': Error setting property values; nested ...

    Beginning SOLID Principles and Design Patterns for ASP.NET Developers.pdf

    ■Chapter 3: Creational Patterns: Singleton, Factory Method, and Prototype ■Chapter 4: Creational Patterns: Abstract Factory and Builder ■Chapter 5: Structural Patterns: Adapter, Bridge, Composite,...

    Programming.in.the.Large.with.Design.Patterns

    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, ...

    36种最新设计模式整理

    Design Pattern: Factory Method 模式 Design Pattern: Prototype 模式 Design Pattern: Singleton 模式 Design Pattern: Registry of Singleton 模式 Design Pattern: Default Adapter 模式 31 Design Pattern:...

    Design.Patterns.Explained.Simply

    Factory Method Object Pool Prototype Singleton Structural patterns Adapter Bridge Composite Decorator Facade Flyweight Private Class Data Proxy Behavioral patterns Chain of Responsibility Command ...

    java设计模式教程+源代码

    FactoryMethod ( 工厂方法 ) Singleton ( 单态模式 ) Builder ( 建造者模式 ) Prototype ( 原型模式 ) Adapter ( 适配器模式 ) Bridge ( 桥接模式 ) Composite ( 组合模式 ) Decorator ( 装饰模式 ) Facade ...

    《Java Design Patterns》高清完整英文PDF版

    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. 总结

    C++.Design.Patterns.and.Derivatives.Pricing

    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 ...

    org.springframework.web.servlet-3.0.1.RELEASE-A.jar

    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 ...

Global site tag (gtag.js) - Google Analytics