design-patterns

How Can I Create method In Java With Same Type Parameter?

前提是你 提交于 2020-06-14 06:25:45
问题 My code looks like below: enum EnumType { CATEGORY, GROUP, MAIN } Methods: public void call(EnumType type){ switch(type): case CATEGORY: return methodForCategory(); case GROUP: return methodForGroup(); ... } public void methodForCategory(){ ... Operations according to EnumType.CATEGORY } public void methodForGroup(){ ... Operations according to EnumType.GROUP } public void methodForMain(){ ... Operations according to EnumType.MAIN } But I want to call it without switch/case like below; public

What is the difference between inheritance and composition?

此生再无相见时 提交于 2020-06-10 04:40:06
问题 As title says, the meaning of both eludes me. 回答1: Inheritance expresses a is-a relationship, while composition expresses a has-a relationship between the two classes. An example for composition is a polygon. It has a ordered sequence of Points. In C++ terms: struct Polygon { std::vector<Point> points; }; While an logic_error is a exception : struct logic_error : public exception { }; 回答2: As pmr pointed out, inheritence is a is-a relationship, composition is a has-a relationship. Composition

Usage of Proxy design pattern

痞子三分冷 提交于 2020-06-09 18:55:06
问题 I tried to understand proxy design pattern. But i could not understand the usage of the proxy design pattern. i got this code example from wikipedia interface Image { public void displayImage(); } //on System A class RealImage implements Image { private String filename = null; /** * Constructor * @param filename */ public RealImage(final String filename) { this.filename = filename; loadImageFromDisk(); } /** * Loads the image from the disk */ private void loadImageFromDisk() { System.out

When should I use builder Design Pattern? [closed]

百般思念 提交于 2020-06-09 16:54:06
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 5 years ago . I'm learning about Design Patterns and found the Builder design pattern. What are the benefits of this design pattern and when should I use it? I surf www.dofactory.com and www.blackwasp.com but still don't understand the benefits. By the by, I'm new to design patterns so

StackOverFlow in the implementation of the Dependency Inversion Principle for cross-refenced classes

做~自己de王妃 提交于 2020-06-01 07:03:40
问题 The Situation: I have three classes: Society, Father and Son. The Society has a list of Fathers. Each Father has one son. It is necessary in this application that each Father knows who his Son is and vice-versa each Son knows who his Father is. I have used direct cross referencing by assigning the Father as a property of the Son, and the Son as a property of the Father class, as below: public class Society : ISociety { public List<IFather> Fathers {get; set;} } public class Father : IFather {

Creating a parser of Class name + String value to a typed value

谁都会走 提交于 2020-05-30 08:12:11
问题 I am trying to write a method that can take in a String classname and a String value, and return the value represented as that String. Example inputs: parse("java.lang.String", "abc") -> String "ABC" parse("java.lang.Boolean", "FALSE") -> Boolean FALSE parse("java.lang.Integer", "123") -> Integer 123 parse("com.me.Color", "RED") -> enum Color.RED I have found that if I use an if block containing assignableFrom calls, I can achieve this. But would prefer writing something more extendable, so

Using the Command Pattern with Parameters

▼魔方 西西 提交于 2020-05-29 06:05:07
问题 I have a ReloadableWeapon class like this: public class ReloadableWeapon { //keeping the design really simple, took out weapon logic. private int numberofbullets; public ReloadableWeapon(int numberofbullets){ this.numberofbullets = numberofbullets; } public void attack(){ numberofbullets--; } public void reload(int reloadBullets){ this.numberofbullets += reloadBullets; } } with the following interface : public interface Command { void execute(); } and use it like so: public class

May __init__ be used as normal method for initialization, not as constructor?

♀尐吖头ヾ 提交于 2020-05-28 21:55:04
问题 Sometimes it looks reasonable to use __init__ as initialization method for already existing object, i.e.: class A(): def __init__(self, x): self.x = x def set_state_from_file(self, file): x = parse_file(file) self.__init__(x) As alternative to this implementation I see the following: class A(): def __init__(self, x): self.init(x) def init(self, x): self.x = x def set_state_from_file(self, file): x = parse_file(file) self.init(x) It seems to me as over-complication of code. Is there any

Expression evaluation using visitor pattern

笑着哭i 提交于 2020-05-28 04:51:52
问题 I have been trying to design an expression evaluator using visitor framework. Before going into my current design here are few problems that I am facing. Current design works well with arithmetic operators but when it comes to logical operators for eg 5 > 6 will output to false it doesn't work. As Visitor is a generic interface so if I say Visitor<Boolean> the return value will become boolean but participating values are integer hence I am not able to have integer as parameter. The design

What's the difference between the Service Locator and the Factory Design pattern?

淺唱寂寞╮ 提交于 2020-05-25 04:11:04
问题 I'm using unity and I'm creating a class that wrapps it and I dont' know how to call it, service locator or factory, both encapsulate the creation of the objects, so.... what's the difference? 回答1: A factory creates objects for you, when requested. Service locator returns objects that may already exist, that is services that may already exist somewhere for you. Just think about the meaning of the names: Factory: is a place where objects are created. Service: is something that can do something