关注的胡子太多了怎么办 怎么办?

我与上级没有个人冲突,但上级对我的工作关注的比较细比较多。该如何解决?_百度知道
我与上级没有个人冲突,但上级对我的工作关注的比较细比较多。该如何解决?
李名较优秀服务顾问起初工作优秀近反驳级工作积极性差两冲突级李工作关注比较比较细问1.阐述述现象认识理解该何问题2.请线员工角度叙述现面现象作同事帮助李处理问题
领导与部属工同格绝高低贵贱;际关系应该形种相互尊重、相互理解、相互关、相互支持浓厚氛围攻克、战胜宝1.换位思考相互理解般情况言领导都想部属工作、进步提供些帮助;部属都想尽力做领导交待任务尽量让领导满意现实尽意情况经发领导领导难处部属部属困难要换位置想想产相互惊解效2.诚待相互关南海北同志能够坐起工作种缘家工作同事朋友领导遇困难部属要主帮助排忧解难;部属难处领导要及伸温暖手要家都能诚相待换极增强单位凝聚力3.各尽所能相互支持领导要及答复部属请示问题交任务且要教尽努力优化部属工作环境;部属要高标准高质量做领导交办事情干工作领导支持4.荣辱与共相依命领导与部属既矛盾着两面命运共同体乘条船荣辱与共明智领导部属应该严加管理、精培养部属看自绩部属失误看自错聪明部属应竭尽全力支持领导工作维护领导威信领导错误看自耻辱领导进步看自光荣才能形战胜强合力
其他类似问题
按默认排序
其他1条回答
您可能关注的推广回答者:
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁播放列表加载中...
正在载入...
分享视频:
嵌入代码:
拍下二维码,随时随地看视频
03如何设置关注自动回复多图文
上 传 者:
内容介绍:
03如何设置关注自动回复多图文
Channel Me 精选
我来说点啥
版权所有 CopyRight
| 京网文[0号 |
| 京公网安备:
互联网药品信息服务资格证:(京)-非经营性- | 广播电视节目制作经营许可证:(京)字第403号
<img src="" width="34" height="34"/>
<img src=""/>
<li data-vid="">
<img src=""/><i data-vid="" class="ckl_plays">
<li data-vid="">
<img src=""/><i data-vid="" class="ckl_plays">
<img src="///img/blank.png" data-src=""/>
<img src="///img/blank.png" data-src="http://"/>
<li data-vid="" class="cfix">
src="///img/blank.png" data-src=""/>
<i data-vid="" class="ckl_plays">
<li data-vid="" class="cfix">
src="///img/blank.png" data-src=""/><i data-vid="" class="ckl_plays">
没有数据!
{upload_level_name}
粉丝 {fans_count}
{video_count}
{description}Java方法参数太多怎么办—Part3—Builder模式 - ImportNew
| 分类: ,
本文是这个系列的第三篇文章,介绍了通过Builder模式应对参数过多的问题。如果你也希望参与类似的系列文章翻译,可以加入我们的 和
在前两篇文章中,我分别使用了自定义类型和参数对象方法来减少构造器或方法调用传入的参数数量。本文关注的是。我认为它不仅能够帮助构造函数“瘦身”,甚至可以对非构造函数也能发挥同样的作用。
在第二版中,在就提到使用Builder模式处理需要很多参数的构造函数。他不仅展示了Builder的使用,也描述了相这种方法相对使用带很多参数的构造函数带来的好处。在本文的结尾我将进一步总结Builde模式的优点。需要指出的是Josh Bloch已经在他的书本贯穿了这一思想。
为了说明这种方法的优点,我会继续使用Person类。需要指出的是,为了更好地聚焦于类构造本身我只添加了个别方法。
Person.java (非Builder模式)
package dustin.
* Person class used as part of too many parameters demonstration.
* @author Dustin
public class Person
private final String lastN
private final String firstN
private final String middleN
private final S
private final S
private final String streetA
private final S
private final S
private final boolean isF
private final boolean isE
private final boolean isHomewO
public Person(
final String newLastName,
final String newFirstName,
final String newMiddleName,
final String newSalutation,
final String newSuffix,
final String newStreetAddress,
final String newCity,
final String newState,
final boolean newIsFemale,
final boolean newIsEmployed,
final boolean newIsHomeOwner)
this.lastName = newLastN
this.firstName = newFirstN
this.middleName = newMiddleN
this.salutation = newS
this.suffix = newS
this.streetAddress = newStreetA
this.city = newC
this.state = newS
this.isFemale = newIsF
this.isEmployed = newIsE
this.isHomewOwner = newIsHomeO
这样写的类构造器也无可厚非,但当有许多参数时编写客户端代码会很困难。这时,如果使用会有效提高代码的可读性。在中曾写过使用能方便地实现代码。下面就是使用NetBeans重构过的代码。
PersonBuilder.java
package dustin.
public class PersonBuilder
private String newLastN
private String newFirstN
private String newMiddleN
private String newS
private String newS
private String newStreetA
private String newC
private String newS
private boolean newIsF
private boolean newIsE
private boolean newIsHomeO
public PersonBuilder()
public PersonBuilder setNewLastName(String newLastName) {
this.newLastName = newLastN
public PersonBuilder setNewFirstName(String newFirstName) {
this.newFirstName = newFirstN
public PersonBuilder setNewMiddleName(String newMiddleName) {
this.newMiddleName = newMiddleN
public PersonBuilder setNewSalutation(String newSalutation) {
this.newSalutation = newS
public PersonBuilder setNewSuffix(String newSuffix) {
this.newSuffix = newS
public PersonBuilder setNewStreetAddress(String newStreetAddress) {
this.newStreetAddress = newStreetA
public PersonBuilder setNewCity(String newCity) {
this.newCity = newC
public PersonBuilder setNewState(String newState) {
this.newState = newS
public PersonBuilder setNewIsFemale(boolean newIsFemale) {
this.newIsFemale = newIsF
public PersonBuilder setNewIsEmployed(boolean newIsEmployed) {
this.newIsEmployed = newIsE
public PersonBuilder setNewIsHomeOwner(boolean newIsHomeOwner) {
this.newIsHomeOwner = newIsHomeO
public Person createPerson() {
return new Person(newLastName, newFirstName, newMiddleName, newSalutation, newSuffix, newStreetAddress, newCity, newState, newIsFemale, newIsEmployed, newIsHomeOwner);
相对于之前的写法,我更趋向于将作为它构建类的静态成员类(嵌套类)。即便是NetBeans自动生成的Builder使用起来也很方便。另一个区别在于,我希望Builder实现一个包含了必要字段的构造函数,而不是一个无参构造函数。
下面显示的是改进后的代码。
Person.java(Person.Builder嵌套类)
package dustin.
* Person class used as part of too many parameters demonstration.
* @author Dustin
public class Person
private final String lastN
private final String firstN
private final String middleN
private final S
private final S
private final String streetA
private final S
private final S
private final boolean isF
private final boolean isE
private final boolean isHomewO
public Person(
final String newLastName,
final String newFirstName,
final String newMiddleName,
final String newSalutation,
final String newSuffix,
final String newStreetAddress,
final String newCity,
final String newState,
final boolean newIsFemale,
final boolean newIsEmployed,
final boolean newIsHomeOwner)
this.lastName = newLastN
this.firstName = newFirstN
this.middleName = newMiddleN
this.salutation = newS
this.suffix = newS
this.streetAddress = newStreetA
this.city = newC
this.state = newS
this.isFemale = newIsF
this.isEmployed = newIsE
this.isHomewOwner = newIsHomeO
public static class PersonBuilder
private String nestedLastN
private String nestedFirstN
private String nestedMiddleN
private String nestedS
private String nestedS
private String nestedStreetA
private String nestedC
private String nestedS
private boolean nestedIsF
private boolean nestedIsE
private boolean nestedIsHomeO
public PersonBuilder(
final String newFirstName,
final String newCity,
final String newState)
this.nestedFirstName = newFirstN
this.nestedCity = newC
this.nestedState = newS
public PersonBuilder lastName(String newLastName)
this.nestedLastName = newLastN
public PersonBuilder firstName(String newFirstName)
this.nestedFirstName = newFirstN
public PersonBuilder middleName(String newMiddleName)
this.nestedMiddleName = newMiddleN
public PersonBuilder salutation(String newSalutation)
this.nestedSalutation = newS
public PersonBuilder suffix(String newSuffix)
this.nestedSuffix = newS
public PersonBuilder streetAddress(String newStreetAddress)
this.nestedStreetAddress = newStreetA
public PersonBuilder city(String newCity)
this.nestedCity = newC
public PersonBuilder state(String newState)
this.nestedState = newS
public PersonBuilder isFemale(boolean newIsFemale)
this.nestedIsFemale = newIsF
public PersonBuilder isEmployed(boolean newIsEmployed)
this.nestedIsEmployed = newIsE
public PersonBuilder isHomeOwner(boolean newIsHomeOwner)
this.nestedIsHomeOwner = newIsHomeO
public Person createPerson()
return new Person(
nestedLastName, nestedFirstName, nestedMiddleName,
nestedSalutation, nestedSuffix,
nestedStreetAddress, nestedCity, nestedState,
nestedIsFemale, nestedIsEmployed, nestedIsHomeOwner);
使用Builder模式时,如果结合我前两篇文章介绍的自定义类型和参数对象方法一起使用效果会更好。示例代码如下。
Person.java (嵌套类Builder,自定义类型加参数对象)
package dustin.
* Person class used as part of too many parameters demonstration.
* @author Dustin
public class Person
private final FullN
private final A
private final G
private final EmploymentS
private final HomeownerStatus homeOwnerS
* Parameterized constructor can be private because only my internal builder
* needs to call me to provide an instance to clients.
* @param newName Name of this person.
* @param newAddress Address of this person.
* @param newGender Gender of this person.
* @param newEmployment Employment status of this person.
* @param newHomeOwner Home ownership status of this person.
private Person(
final FullName newName, final Address newAddress,
final Gender newGender, final EmploymentStatus newEmployment,
final HomeownerStatus newHomeOwner)
this.name = newN
this.address = newA
this.gender = newG
this.employment = newE
this.homeOwnerStatus = newHomeO
public FullName getName()
return this.
public Address getAddress()
return this.
public Gender getGender()
return this.
public EmploymentStatus getEmployment()
return this.
public HomeownerStatus getHomeOwnerStatus()
return this.homeOwnerS
* Builder class as outlined in the Second Edition of Joshua Bloch&#039;s
* &em&&/em& that is used to build a {@link Person} instance.
public static class PersonBuilder
private FullName nestedN
private Address nestedA
private Gender nestedG
private EmploymentStatus nestedEmploymentS
private HomeownerStatus nestedHomeOwnerS
public PersonBuilder(
final FullName newFullName,
final Address newAddress)
this.nestedName = newFullN
this.nestedAddress = newA
public PersonBuilder name(final FullName newName)
this.nestedName = newN
public PersonBuilder address(final Address newAddress)
this.nestedAddress = newA
public PersonBuilder gender(final Gender newGender)
this.nestedGender = newG
public PersonBuilder employment(final EmploymentStatus newEmploymentStatus)
this.nestedEmploymentStatus = newEmploymentS
public PersonBuilder homeOwner(final HomeownerStatus newHomeOwnerStatus)
this.nestedHomeOwnerStatus = newHomeOwnerS
public Person createPerson()
return new Person(
nestedName, nestedAddress, nestedGender,
nestedEmploymentStatus, nestedHomeOwnerStatus);
最后2段代码列表展示了Builder构建对象的常见方法。实际上,《在Effective Java 第二版》第2条中就是用Builder来创建和销毁对象。此外,Builder能更简单地通过参数对象传递间接实现了一个非构造函数方法。在上面最后一段代码中,这些方法都接收了一些参数对象。诚然,即使使用了Builder构建参数对象还是比较枯燥的。但每一次Builder构建都是间接让非构造函数方法受益,因为参数对象使用确实有效减少了方法参数的数量。
下面重新定义的FullName和Address类既能做参数本身也使用了Builder模式。
FullName.java(使用Builder)
package dustin.
* Full name of a person.
* @author Dustin
public final class FullName
private final Name lastN
private final Name firstN
private final Name middleN
private final S
private final S
private FullName(
final Name newLastName,
final Name newFirstName,
final Name newMiddleName,
final Salutation newSalutation,
final Suffix newSuffix)
this.lastName = newLastN
this.firstName = newFirstN
this.middleName = newMiddleN
this.salutation = newS
this.suffix = newS
public Name getLastName()
return this.lastN
public Name getFirstName()
return this.firstN
public Name getMiddleName()
return this.middleN
public Salutation getSalutation()
return this.
public Suffix getSuffix()
return this.
public String toString()
this.salutation + & & + this.firstName + & & + this.middleName
+ this.lastName + &, & + this.
public static class FullNameBuilder
private final Name nestedLastN
private final Name nestedFirstN
private Name nestedMiddleN
private Salutation nestedS
private Suffix nestedS
public FullNameBuilder(
final Name newLastName, final Name newFirstName)
this.nestedLastName = newLastN
this.nestedFirstName = newFirstN
public FullNameBuilder middleName(final Name newMiddleName)
this.nestedMiddleName = newMiddleN
public FullNameBuilder salutation(final Salutation newSalutation)
this.nestedSalutation = newS
public FullNameBuilder suffix(final Suffix newSuffix)
this.nestedSuffix = newS
public FullName createFullName()
return new FullName(
nestedLastName, nestedFirstName, nestedMiddleName,
nestedSalutation, nestedSuffix);
Address.java (使用Builder)
package dustin.
* Representation of a United States address.
* @author Dustin
public final class Address
private final StreetAddress streetA
private final C
private final S
private Address(final StreetAddress newStreetAddress, final City newCity, final State newState)
this.streetAddress = newStreetA
this.city = newC
this.state = newS
public StreetAddress getStreetAddress()
return this.streetA
public City getCity()
return this.
public State getState()
return this.
public String toString()
return this.streetAddress + &, & + this.city + &, & + this.
public static class AddressBuilder
private StreetAddress nestedStreetA
private final City nestedC
private final State nestedS
public AddressBuilder(final City newCity, final State newState)
this.nestedCity = newC
this.nestedState = newS
public AddressBuilder streetAddress(final StreetAddress newStreetAddress)
this.nestedStreetAddress = newStreetA
public Address createAddress()
return new Address(nestedStreetAddress, nestedCity, nestedState);
由于用了Builder,新的Person实例可以通过下面这种方式创建。当然为了比较,也给出了传统的Person实例化方法。
final Person person1 = new Person.PersonBuilder(
new FullName.FullNameBuilder(
new Name(&Dynamite&), new Name(&Napoleon&)).createFullName(),
new Address.AddressBuilder(
new City(&Preston&), State.ID).createAddress()).createPerson();
final Person person2 = new Person.PersonBuilder(
new FullName.FullNameBuilder(
new Name(&Coltrane&), new Name(&Rosco&)).middleName(new Name(&Purvis&)).createFullName(),
new Address.AddressBuilder(
new City(&Hazzard&), State.GA).createAddress())
.gender(Gender.MALE).employment(EmploymentStatus.EMPLOYED).createPerson();
实例化Person(不使用Builder)
final person = new Person(&#8220;Coltrane&#8221;, &#8220;Rosco&#8221;, &#8220;Purvis&#8221;, null, &#8220;Hazzard&#8221;, &#8220;Georgia&#8221;, false, true, true);
看完代码高下立判,调用传统JAVA构造器的客户端代码的可读性远不如使用了Builder的客户端代码。同一类型的变量和空放置在一起被调用将会导致一些微妙的错误。(试想,如果客户端不小心颠倒了其中的几个参数顺序,编译不会出错但在运行时肯定出错)
Builder模式好处和优点
使用必然会导致写两遍相关属性的代码和SETTER方法,看起来有点吃力不讨好。然而需要看到的是,客户端代码的可用性和可读性得到了大大提高。与此同时,构造函数的参数数量明显减少调用起来非常直观。
另外一个优势在于,单个builder构建多个对象时Builder参数可在创建期间进行调整,还可以根据对象不同而进行改变。这就像我越来越推崇的。特别适合那些属性个数很多的类,没有必要给那些本不需要设置值传递参数()。
在提高代码可读性的同时,使用IDE提供的代码补全功能也更加容易。在与构造函数一起使用带来的更大优势在Josh Bloch的Effective Java第二版第2条中有详细阐述。
Builder模式的代价和缺点
使用是肯定会增加代码量的。此外,尽管客户端的代码可读性明显改善,但随之而来的客户端代码变得更加冗长。我还是坚持这一观点:相比较参数数量的增加,相同类型的参数混在一起,可选参数的增加而言,改善代码可读性更有价值。
Builder会增加个类代码,这也意味着开发者在给类增加属性时有时会忘记给该属性添加支持的builder。为了克服这个问题,通常我会将builder嵌套到类中,这样可以很容易地发现哪个相关的builder需要更新。尽管忘记的风险依旧存在,但是这风险就像忘记给类的新属性增加 、 、 或其他类基于是所有属性的方法一样。
在我的实现中,我会用Builder的构造函数而不是set方法传递客户需要的属性。这样做的好处在于,对象总是能被一次完整的实例化,而不是靠开发人员调用时用set方法补充额外的属性完成实例化。这也体现了不可变性带来的好处。然而,相应地也会造成自己设定的属性方法可读性降低。
正如它名字表示的那样,Builder只是一个替代构造器的选择不能直接用于降低非构造函数方法的参数数量,但是结合参数对象的使用就能达到这一点。有关更多反对用Builder来进行对象构建的讨论可以参见 上的。
构建对象时,如果碰到类有很多参数——其中很多参数类型相同而且很多参数可以为空时,我更喜欢Builder模式来完成。当参数数量不多、类型不同而且都是必须出现时,通过增加代码实现Builder往往无法体现它的优势。在这种情况下,理想的方法是调用传统的构造函数。再者,如果不需要保持不变,那么就使用无参构造函数调用相应的set方法吧。
原文链接:
- 译文链接: [ 转载请保留原文出处、译者和译文链接。]
关于作者:
(新浪微博:)
微信关注: ImportNew
分享Java相关的技术文章、工具资源和热点资讯。扫描加关注,碎片时间提高Java开发技能!
谢谢楼主的贡献,也谢谢原作者详细的讲解!
关于ImportNew
ImportNew 专注于 Java 技术分享。于日 11:11正式上线。是的,这是一个很特别的时刻 :)
ImportNew 由两个 Java 关键字 import 和 new 组成,意指:Java 开发者学习新知识的网站。 import 可认为是学习和吸收, new 则可认为是新知识、新技术圈子和新朋友……
&#8211; 写了文章?看干货?去头条!
&#8211; 为IT单身男女服务的征婚传播平台
&#8211; 优秀的工具资源导航
&#8211; 活跃 &#038; 专业的翻译小组
&#8211; 国内外的精选博客文章
&#8211; JavaScript, HTML5, CSS
&#8211; 专注Android技术分享
&#8211; 专注iOS技术分享
&#8211; 专注Java技术分享
&#8211; 专注Python技术分享
新浪微博:
微信号:importnew
反馈建议:@
广告与商务合作QQ:
& 2015 ImportNew

我要回帖

更多关于 胡子太多了怎么办 的文章

 

随机推荐