[Soft test] upward transformation VS simple factory, soft test transformation vs Factory

Source: Internet
Author: User

[Soft test] upward transformation VS simple factory, soft test transformation vs Factory

Today we will not talk about concepts. Let's start with a program.

public class Test{public static void main(String args[]){Test test=new Test();Animal a=new Animal("name");Cat c=new Cat("catname","name");Dog d=new Dog("dogname","black");test.f(a);test.f(c);test.f(d);}public void f(Animal a){System.out.println("name:"+a.name);if(a instanceof Cat){Cat cat=(Cat)a;System.out.println(""+cat.eyescolor+"eyes");}else if(a instanceof Dog){Dog dog=(Dog)a;System.out.println(""+dog.furColor+"fur");}}}}


This is a simple piece of code. Object transformation instance. The main program instantiates an animal, a cat, and a dog. The best thing is that the f (Animal a) method encapsulates cat and dog changes. This allows the client to directly use the following statements to obtain desired results.

Test. f (a); test. f (c); test. f (d );

The key statement if (a instanceof Cat) is a specific example of object transformation. That is, if animal a instantiates a cat, it is the attributes of the cat. That is, the reference type variable of a base class can "point to" the object of its subclass.

 

However, this is not what I want to say.

Seeing the above program, I suddenly thought of the design pattern I learned. The example of a calculator in a simple project is similar to this one.

public class OperationFactory{public static Operation createOperate(string operate){Operation oper=null;switch(operate){case"+";oper=new OperationAdd();break;case"-";oper=new OperationSub();break;case"*";oper=new OperationMul();break;case"/";oper=new OperationDiv();break;}return oper;}}


Client

Operation oper;oper=OperationFactory.createOperate("+");oper.NumberA=1;oper.NumberB=2;double result=oper.GetResult();


 

Although one is a design model and the other is just a usage, this incomparable comparison may touch some ideas.

In common: a class is used to create an instance.

Difference: in a simple factory, only a few different situations are simply judged, that is, "If it is" + ", then .. . "The first example highlights the relationship between the base class and the subclass ." If the operator is a plus operator, then .. "

 

Then let's take a look at object transformation.

In general, subclass objects can be used by objects of the base class. Or the base class object can be used as a subclass object. Of course there will be some rules. For example, the reference type variable of the base class can "point to" the object of its subclass.

 

The two examples are indeed similar, but they are used to mobilize themselves. The real focus is still on how to learn. Basic comparison and accumulation.

 

 

 


Why can't Nanjing soft examination network be opened? The "China computer technology and software professional technical qualifications (level) Examination" website cannot be opened?

The website of "China computer technology and software professional technical qualification (level) Examination" can be opened. I went to see it in the morning!
Reference: Go to the soft exam Institute


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.